1+ """
2+ This module provides fixtures for test setup.
3+ You can use these fixtures for all tests in your suite.
4+ You could also copy-paste this module into your own test project to provide Applitools setup for your tests.
5+ """
6+
7+ # --------------------------------------------------------------------------------
8+ # Imports
9+ # --------------------------------------------------------------------------------
10+
111import os
212import pytest
313
717from selenium .webdriver .chrome .service import Service
818
919
20+ # --------------------------------------------------------------------------------
21+ # Session-Scope Fixtures
22+ # These fixtures run one time for the whole test suite.
23+ # Subsequent calls use the value cached from the first execution.
24+ # --------------------------------------------------------------------------------
25+
1026@pytest .fixture (scope = 'session' )
1127def api_key ():
28+ """
29+ Reads the Applitools API key from an environment variable.
30+ """
1231 return os .getenv ('APPLITOOLS_API_KEY' )
1332
1433
1534@pytest .fixture (scope = 'session' )
1635def headless ():
36+ """
37+ Reads the headless mode setting from an environment variable.
38+ Uses headless mode for Continuous Integration (CI) execution.
39+ Uses headed mode for local development.
40+ """
1741 h = os .getenv ('HEADLESS' , default = 'false' )
1842 return h .lower () == 'true'
1943
2044
2145@pytest .fixture (scope = 'session' )
2246def runner ():
47+ """
48+ Creates the runner for the Ultrafast Grid.
49+ Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
50+ Warning: If you have a free account, then concurrency will be limited to 1.
51+ After the test suite finishes execution, closes the batch and report visual differences to the console.
52+ Note that it forces pytest to wait synchronously for all visual checkpoints to complete.
53+ """
2354 run = VisualGridRunner (RunnerOptions ().test_concurrency (5 ))
2455 yield run
2556 print (run .get_all_test_results ())
2657
2758
2859@pytest .fixture (scope = 'session' )
2960def batch_info ():
61+ """
62+ Creates a new batch for tests.
63+ A batch is the collection of visual checkpoints for a test suite.
64+ Batches are displayed in the dashboard, so use meaningful names.
65+ """
3066 return BatchInfo ("Example: Selenium Python pytest with the Ultrafast Grid" )
3167
3268
3369@pytest .fixture (scope = 'session' )
3470def configuration (api_key : str , batch_info : BatchInfo ):
71+ """
72+ Creates a configuration for Applitools Eyes.
73+ """
74+
75+ # Construct the object
3576 config = Configuration ()
77+
78+ # Set the batch for the config.
3679 config .set_batch (batch_info )
80+
81+ # Set the Applitools API key so test results are uploaded to your account.
82+ # If you don't explicitly set the API key with this call,
83+ # then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
3784 config .set_api_key (api_key )
85+
86+ # Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
87+ # Other browsers are also available, like Edge and IE.
3888 config .add_browser (800 , 600 , BrowserType .CHROME )
3989 config .add_browser (1600 , 1200 , BrowserType .FIREFOX )
4090 config .add_browser (1024 , 768 , BrowserType .SAFARI )
91+
92+ # Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
93+ # Other mobile devices are available, including iOS.
4194 config .add_device_emulation (DeviceName .Pixel_2 , ScreenOrientation .PORTRAIT )
4295 config .add_device_emulation (DeviceName .Nexus_10 , ScreenOrientation .LANDSCAPE )
96+
97+ # Return the configuration object
4398 return config
4499
45100
46101@pytest .fixture (scope = 'session' )
47102def chromedriver_service ():
103+ """
104+ Sets up ChromeDriver and returns a Service object for initializing WebDriver objects.
105+ """
48106 path = ChromeDriverManager ().install ()
49107 return Service (path )
50108
51109
110+ # --------------------------------------------------------------------------------
111+ # Function-Scope Fixtures
112+ # These fixtures run one time before each test that calls them.
113+ # Returned values are not cached and reused across different tests.
114+ # --------------------------------------------------------------------------------
115+
52116@pytest .fixture (scope = 'function' )
53117def webdriver (headless : bool , chromedriver_service : Service ):
118+ """
119+ Creates a WebDriver object for Chrome.
120+ Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
121+ it still needs to run the test one time locally to capture snapshots.
122+ After the test function finishes execution, quits the browser.
123+ """
54124 options = ChromeOptions ()
55125 options .headless = headless
56126 driver = Chrome (service = chromedriver_service , options = options )
@@ -64,9 +134,14 @@ def eyes(
64134 configuration : Configuration ,
65135 webdriver : Chrome ,
66136 request : pytest .FixtureRequest ):
137+ """
138+ Creates the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
139+ Then, opens Eyes to start visual testing before the test, and closes Eyes at the end of the test.
140+ """
67141
68142 eyes = Eyes (runner )
69143 eyes .set_configuration (configuration )
144+
70145 eyes .open (
71146 driver = webdriver ,
72147 app_name = 'ACME Bank Web App' ,
0 commit comments