11"""Tests for Robot Framework library integration.
22
3- These tests import robot_library directly (not via percy.__init__) to
4- avoid triggering the playwright import in percy.screenshot at module level.
5- The keyword methods use lazy imports so playwright is only needed at call time.
3+ These tests mock playwright at module level so they can run without
4+ playwright installed, since percy.screenshot imports it at top level.
65"""
76import sys
87from unittest .mock import MagicMock , patch
98
10- import pytest
11-
12-
13- # Mock playwright before any percy imports so screenshot.py can load
9+ # Mock playwright before any percy imports
1410_mock_playwright = MagicMock ()
1511_mock_playwright ._repo_version .version = "1.50.0"
1612_mock_playwright .sync_api .Error = Exception
1915sys .modules .setdefault ("playwright._repo_version" , _mock_playwright ._repo_version )
2016sys .modules .setdefault ("playwright.sync_api" , _mock_playwright .sync_api )
2117
18+ from percy .robot_library import ( # noqa: E402 pylint: disable=wrong-import-position
19+ PercyLibrary ,
20+ _parse_bool ,
21+ _parse_csv ,
22+ _parse_json ,
23+ _parse_widths ,
24+ )
25+
2226
2327class TestParseHelpers :
2428 def test_parse_bool_none (self ):
25- from percy .robot_library import _parse_bool
2629 assert _parse_bool (None ) is None
2730
2831 def test_parse_bool_true (self ):
29- from percy .robot_library import _parse_bool
3032 assert _parse_bool ("True" ) is True
3133 assert _parse_bool ("true" ) is True
3234 assert _parse_bool ("1" ) is True
3335
3436 def test_parse_bool_false (self ):
35- from percy .robot_library import _parse_bool
3637 assert _parse_bool ("False" ) is False
3738 assert _parse_bool ("no" ) is False
3839
3940 def test_parse_widths_string (self ):
40- from percy .robot_library import _parse_widths
4141 assert _parse_widths ("375,768,1280" ) == [375 , 768 , 1280 ]
4242
4343 def test_parse_widths_none (self ):
44- from percy .robot_library import _parse_widths
4544 assert _parse_widths (None ) is None
4645
4746 def test_parse_csv_string (self ):
48- from percy .robot_library import _parse_csv
4947 assert _parse_csv ("regression, homepage" ) == ["regression" , "homepage" ]
5048
5149 def test_parse_json_string (self ):
52- from percy .robot_library import _parse_json
5350 assert _parse_json ('{"key": true}' ) == {"key" : True }
5451
5552 def test_parse_json_none (self ):
56- from percy .robot_library import _parse_json
5753 assert _parse_json (None ) is None
5854
5955
6056class TestPercyLibraryKeywords :
6157 def test_import_succeeds (self ):
62- from percy .robot_library import PercyLibrary
6358 assert PercyLibrary is not None
6459
6560 @patch ("percy.robot_library._get_screenshot_module" )
6661 @patch ("percy.robot_library.BuiltIn" )
6762 def test_percy_snapshot_keyword (self , mock_builtin , mock_get_mod ):
68- from percy .robot_library import PercyLibrary
69-
7063 mock_mod = MagicMock ()
7164 mock_get_mod .return_value = mock_mod
7265 mock_builtin .return_value .get_library_instance .return_value = MagicMock ()
@@ -75,28 +68,24 @@ def test_percy_snapshot_keyword(self, mock_builtin, mock_get_mod):
7568 lib .percy_snapshot_keyword ("Homepage" , widths = "375,1280" , labels = "regression,v2" )
7669
7770 mock_mod .percy_snapshot .assert_called_once ()
78- _ , kwargs = mock_mod .percy_snapshot .call_args
79- assert kwargs ["widths" ] == [375 , 1280 ]
80- assert kwargs ["labels" ] == ["regression" , "v2" ]
71+ call_kwargs = mock_mod .percy_snapshot .call_args [ 1 ]
72+ assert call_kwargs ["widths" ] == [375 , 1280 ]
73+ assert call_kwargs ["labels" ] == ["regression" , "v2" ]
8174
8275 @patch ("percy.robot_library._get_screenshot_module" )
8376 def test_percy_is_running (self , mock_get_mod ):
84- from percy .robot_library import PercyLibrary
85-
8677 mock_mod = MagicMock ()
8778 mock_get_mod .return_value = mock_mod
8879
89- mock_mod ._is_percy_enabled .return_value = {"session_type" : "web" }
80+ mock_mod ._is_percy_enabled .return_value = {"session_type" : "web" } # pylint: disable=protected-access
9081 lib = PercyLibrary ()
9182 assert lib .percy_is_running_keyword () is True
9283
93- mock_mod ._is_percy_enabled .return_value = False
84+ mock_mod ._is_percy_enabled .return_value = False # pylint: disable=protected-access
9485 assert lib .percy_is_running_keyword () is False
9586
9687 @patch ("percy.robot_library._get_screenshot_module" )
9788 def test_create_region (self , mock_get_mod ):
98- from percy .robot_library import PercyLibrary
99-
10089 mock_mod = MagicMock ()
10190 mock_mod .create_region .return_value = {"algorithm" : "ignore" , "elementSelector" : {"elementCSS" : ".ad" }}
10291 mock_get_mod .return_value = mock_mod
0 commit comments