diff --git a/tests/conftest.py b/tests/conftest.py index 0bf1813..8dfa76f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,7 +12,19 @@ import pytest from applitools.selenium import * -from selenium.webdriver import Chrome, ChromeOptions +from applitools.selenium.runner import EyesRunner +from selenium.webdriver import Chrome, ChromeOptions, Remote + + +# -------------------------------------------------------------------------------- +# Runner Settings +# These could be set by environment variables or other input mechanisms. +# They are hard-coded here to keep the example project simple. +# -------------------------------------------------------------------------------- + +USE_ULTRAFAST_GRID = True +USE_EXECUTION_CLOUD = False +HEADLESS = False # -------------------------------------------------------------------------------- @@ -29,27 +41,21 @@ def api_key(): return os.getenv('APPLITOOLS_API_KEY') -@pytest.fixture(scope='session') -def headless(): - """ - Reads the headless mode setting from an environment variable. - Uses headless mode for Continuous Integration (CI) execution. - Uses headed mode for local development. - """ - h = os.getenv('HEADLESS', default='false') - return h.lower() == 'true' - - @pytest.fixture(scope='session') def runner(): """ - Creates the runner for the Ultrafast Grid. - Concurrency refers to the number of visual checkpoints Applitools will perform in parallel. + Creates the runner for either the Ultrafast Grid or the Classic runner (local execution). + For UFG, concurrency refers to the number of visual checkpoints Applitools will perform in parallel. Warning: If you have a free account, then concurrency will be limited to 1. After the test suite finishes execution, closes the batch and report visual differences to the console. Note that it forces pytest to wait synchronously for all visual checkpoints to complete. """ - run = VisualGridRunner(RunnerOptions().test_concurrency(5)) + + if USE_ULTRAFAST_GRID: + run = VisualGridRunner(RunnerOptions().test_concurrency(5)) + else: + run = ClassicRunner() + yield run print(run.get_all_test_results()) @@ -81,16 +87,19 @@ def configuration(api_key: str, batch_info: BatchInfo): # then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it. config.set_api_key(api_key) - # Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid. - # Other browsers are also available, like Edge and IE. - config.add_browser(800, 600, BrowserType.CHROME) - config.add_browser(1600, 1200, BrowserType.FIREFOX) - config.add_browser(1024, 768, BrowserType.SAFARI) + # If running tests on the Ultrafast Grid, configure browsers. + if USE_ULTRAFAST_GRID: - # Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid. - # Other mobile devices are available, including iOS. - config.add_device_emulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT) - config.add_device_emulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE) + # Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid. + # Other browsers are also available, like Edge and IE. + config.add_browser(800, 600, BrowserType.CHROME) + config.add_browser(1600, 1200, BrowserType.FIREFOX) + config.add_browser(1024, 768, BrowserType.SAFARI) + + # Add 2 mobile browsers with different orientations for cross-browser testing in the Ultrafast Grid. + # Other mobile devices are available. + config.add_browser(IosDeviceInfo(IosDeviceName.iPhone_11, ScreenOrientation.PORTRAIT)) + config.add_browser(ChromeEmulationInfo(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE)) # Return the configuration object return config @@ -103,28 +112,34 @@ def configuration(api_key: str, batch_info: BatchInfo): # -------------------------------------------------------------------------------- @pytest.fixture(scope='function') -def webdriver(headless: bool): +def webdriver(): """ Creates a WebDriver object for Chrome. - Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid, - it still needs to run the test one time locally to capture snapshots. After the test function finishes execution, quits the browser. """ + options = ChromeOptions() - options.headless = headless - driver = Chrome(options=options) + options.headless = HEADLESS + + if USE_EXECUTION_CLOUD: + driver = Remote( + command_executor=Eyes.get_execution_cloud_url(), + options=options) + else: + driver = Chrome(options=options) + yield driver driver.quit() @pytest.fixture(scope='function') def eyes( - runner: VisualGridRunner, + runner: EyesRunner, configuration: Configuration, - webdriver: Chrome, + webdriver: Remote, request: pytest.FixtureRequest): """ - Creates the Applitools Eyes object connected to the VisualGridRunner and set its configuration. + Creates the Applitools Eyes object connected to the runner and set its configuration. Then, opens Eyes to start visual testing before the test, and closes Eyes at the end of the test. Opening Eyes requires 4 arguments: