|
| 1 | +import os |
| 2 | +import pytest |
| 3 | + |
| 4 | +from applitools.selenium import * |
| 5 | +from selenium.webdriver import Chrome, ChromeOptions |
| 6 | +from webdriver_manager.chrome import ChromeDriverManager |
| 7 | +from selenium.webdriver.chrome.service import Service |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope='session') |
| 11 | +def api_key(): |
| 12 | + return os.getenv('APPLITOOLS_API_KEY') |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope='session') |
| 16 | +def headless(): |
| 17 | + h = os.getenv('HEADLESS', default='false') |
| 18 | + return h.lower() == 'true' |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope='session') |
| 22 | +def runner(): |
| 23 | + run = VisualGridRunner(RunnerOptions().test_concurrency(5)) |
| 24 | + yield run |
| 25 | + print(run.get_all_test_results()) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope='session') |
| 29 | +def batch_info(): |
| 30 | + return BatchInfo("Example: Selenium Python pytest with the Ultrafast Grid") |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope='session') |
| 34 | +def configuration(api_key: str, batch_info: BatchInfo): |
| 35 | + config = Configuration() |
| 36 | + config.set_batch(batch_info) |
| 37 | + config.set_api_key(api_key) |
| 38 | + config.add_browser(800, 600, BrowserType.CHROME) |
| 39 | + config.add_browser(1600, 1200, BrowserType.FIREFOX) |
| 40 | + config.add_browser(1024, 768, BrowserType.SAFARI) |
| 41 | + config.add_device_emulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT) |
| 42 | + config.add_device_emulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE) |
| 43 | + return config |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope='session') |
| 47 | +def chromedriver_service(): |
| 48 | + path = ChromeDriverManager().install() |
| 49 | + return Service(path) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope='function') |
| 53 | +def webdriver(headless: bool, chromedriver_service: Service): |
| 54 | + options = ChromeOptions() |
| 55 | + options.headless = headless |
| 56 | + driver = Chrome(service=chromedriver_service, options=options) |
| 57 | + yield driver |
| 58 | + driver.quit() |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture(scope='function') |
| 62 | +def eyes( |
| 63 | + runner: VisualGridRunner, |
| 64 | + configuration: Configuration, |
| 65 | + webdriver: Chrome, |
| 66 | + request: pytest.FixtureRequest): |
| 67 | + |
| 68 | + eyes = Eyes(runner) |
| 69 | + eyes.set_configuration(configuration) |
| 70 | + eyes.open( |
| 71 | + driver=webdriver, |
| 72 | + app_name='ACME Bank Web App', |
| 73 | + test_name=request.node.name, |
| 74 | + viewport_size=RectangleSize(1024, 768)) |
| 75 | + |
| 76 | + yield eyes |
| 77 | + eyes.close_async() |
0 commit comments