def start_browser(self, headless=False, resolution="maximum", mobileEmulation=""): """ Start the browser in a grid with proxy setup :param bool headless: run browser in headless mode :param str resolution: maximum or width * height :param str mobileEmulation: chrome emulation mode e.g. iPhone X, Samsung Galaxy S5 etc :return: Framework driver wrapper """ proxy_server_arg = "--proxy-server={0}".format(self._client.proxy) options = { "highlight": False, "headless": headless, "resolution": resolution, "mobileEmulation": mobileEmulation } self._capabilities["goog:chromeOptions"] = { "args": [proxy_server_arg, '--ignore-certificate-errors', '--test-type'] } DriverRegistry.register_driver(DriverTypes.CHROME, capabilities=self._capabilities, local=False, options=options, grid_url=self._grid_url) self._driver = DriverRegistry.get_driver()
def stop_browser(self): """ Stop the browser instance """ if DriverRegistry.get_driver(): DriverRegistry.deregister_driver() self._driver = None
def after_scenario(context, scenario): """ Close app instance if not reused :param context: behave.runner.Context :param scenario: behave.model.Scenario """ if context.driver is not None: if scenario.status == Status.failed: _screenshot = '{}/{}_fail.png'.format( context.log, scenario.name.replace(' ', '_')) # Take screen shot on a failure try: context.driver.take_screenshot_current_window(_screenshot) except Exception: logger.debug('Failed to take screenshot to: {}'.format( context.log)) pass # https://github.com/zalando/zalenium/blob/master/docs/usage_examples.md#marking-the-test-as-passed-or-failed if context.zalenium: try: context.driver.add_cookie({ 'name': 'zaleniumTestPassed', 'value': 'false' }) except Exception: logger.debug( 'Failed to set failed cookie for scenario in Zalenium') pass # https://github.com/zalando/zalenium/blob/master/docs/usage_examples.md#marking-the-test-as-passed-or-failed if scenario.status == Status.passed and context.zalenium: try: context.driver.add_cookie({ 'name': 'zaleniumTestPassed', 'value': 'true' }) except Exception: logger.debug( 'Failed to set passed cookie for scenario in Zalenium') pass if context.driver: try: DriverRegistry.deregister_driver() context.driver = None except Exception: logger.exception('Failed to stop driver instance') raise logger.info('End of test: {}. Status {} !!!\n\n\n'.format( scenario.name, scenario.status.name.upper()))
def take_screenshot(self): DriverRegistry.register_driver(DriverTypes.CHROMEHEADLESS, local=False) driver = DriverRegistry.get_driver() for url in self.urls_json["urls"]: driver.goto_url(url["url"], absolute=True) for device in self._devices_list: driver.set_window_size( self.devices_json[device]["w"], self.devices_json[device]["h"]) filename = "%s_%s_%s.png" % (url["name"].replace(" ", ""), device, datetime.now().strftime("%Y%m%d-%H%M%S")) driver.take_screenshot_current_window( self._folder + "/%s" % filename) DriverRegistry.deregister_driver()
def test_remote_chrome_with_custom_capabilities_chomeoptions_and_with_options( self): capabilities = { "browserName": "chrome", "version": "ANY", "platform": "ANY", "goog:chromeOptions": { "args": ["--disable-gpu", "--no-sandbox"], "extensions": [], "prefs": {} } } option = { "highlight": True, "headless": True, "resolution": "maximum", "mobileEmulation": "iPhone X" } DriverRegistry.register_driver(local=False, capabilities=capabilities, options=option) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.CHROME) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def test_default_chrome_local_driver_creation(self): DriverRegistry.register_driver() self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.CHROME) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def test_remote_ff_driver_registration(self): DriverRegistry.register_driver(DriverTypes.FIREFOX, local=False) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.FIREFOX) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def driver(self): # type: () -> Union[WebDriver, None] """ Get the underlying selenium/appium webdriver :return: """ return DriverRegistry.get_webdriver()
def test_default_chrome_headless_local_driver_creation(self): option = {"highlight": True, "headless": True, "resolution": "maximum"} DriverRegistry.register_driver(options=option) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.CHROME) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def test_ff_headless_local_driver_registration(self): option = {"highlight": True, "headless": True, "resolution": "maximum"} DriverRegistry.register_driver("firefox", options=option) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.FIREFOX) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def test_remote_chrome_headless_driver_registration_with_capabilities( self): option = {"highlight": True, "headless": True, "resolution": "maximum"} DriverRegistry.register_driver(DriverTypes.CHROME, local=False, options=option) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.CHROME) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def setUpClass(cls): cap = { 'platformName': 'Android', 'version': '7.1.1', 'platform': 'Android', 'deviceName': 'samsung_galaxy_s6_7.1.1', 'browserName': 'chrome' } cls.driver = DriverRegistry.register_driver( DriverTypes.ANDROIDWEB, capabilities=cap, grid_url=DOCKER_SELENIUM_URL)
def test_registering_multiple_drivers(self): DriverRegistry.register_driver(DriverTypes.CHROME, local=False) current_driver = DriverRegistry.get_driver() new_driver = DriverRegistry.register_driver(DriverTypes.FIREFOX) self.assertIsNotNone( new_driver, "Error: Should return previously registered driver") self.assertEqual( current_driver, new_driver, "Error: Should return same driver instance created before") DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration') self.assertIsNone(DriverRegistry.get_webdriver(), 'Error: Webdriver found without registration')
def test_custom_local_driver_registration(self): test_capabilities = { "browserName": "firefox", "platform": 'LINUX', "version": '', "javascriptEnabled": True } DriverRegistry.register_driver(DriverTypes.FIREFOX, capabilities=test_capabilities) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.FIREFOX) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def validate_all(urls): DriverRegistry.register_driver(DriverTypes.CHROMEHEADLESS, local=False) errors = [] list_urls = urls.split(',') for url in list_urls: DriverRegistry.get_webdriver().get(url) errors.append( _validate_images_on_url(url, DriverRegistry.get_webdriver(), open_url=False)) errors.append( _validate_links_on_url(url, DriverRegistry.get_webdriver(), open_url=False)) errors.append( _validate_js_error_on_url(url, DriverRegistry.get_webdriver(), open_url=False)) DriverRegistry.deregister_driver() return list(itertools.chain.from_iterable(errors))
def start_browser(self, headless=False, resolution="maximum", mobileEmulation=""): """ Starts local chrome browser with proxy configured :param bool headless: run browser in headless mode :param str resolution: maximum or width * height :param str mobileEmulation: chrome emulation mode e.g. iPhone X, Samsung Galaxy S5 etc :return: Framework driver wrapper """ proxy_server_arg = "--proxy-server={0}".format(self._client.proxy) capabilities = { "browserName": "chrome", "version": "ANY", "platform": "ANY", "goog:chromeOptions": { "args": [ proxy_server_arg, '--ignore-certificate-errors', '--test-type' ], "extensions": [], "prefs": {} } } options = { "highlight": False, "headless": headless, "resolution": resolution, "mobileEmulation": mobileEmulation } DriverRegistry.register_driver(DriverTypes.CHROME, capabilities=capabilities, options=options, local=True) self._driver = DriverRegistry.get_driver()
def setUpClass(cls): cap = { 'platformName': 'Android', 'platformVersion': '7.1.1', 'platform': 'Android', 'deviceName': 'nexus_5_7.1.1', 'app': '/root/tmp/app-debug.apk', "fullReset": "true", "appPackage": "nzme.test.skynettestapp", "appActivity": ".MainActivity" } cls.driver = DriverRegistry.register_driver( DriverTypes.ANDROID, capabilities=cap, grid_url=DOCKER_SELENIUM_URL)
def test_remote_chrome_with_capabilities_and_with_options(self): capabilities = { "browserName": "chrome", "version": "ANY", "platform": "ANY" } option = { "highlight": True, "headless": True, "resolution": "maximum", "mobileEmulation": "iPhone X" } DriverRegistry.register_driver(local=False, capabilities=capabilities, options=option) self.assertIsInstance(DriverRegistry.get_driver(), BrowserDriver) self.assertIsInstance(DriverRegistry.get_webdriver(), WebDriver) self.assertEqual(DriverRegistry.get_driver().name, DriverTypes.CHROME) DriverRegistry.deregister_driver() self.assertIsNone(DriverRegistry.get_driver(), 'Error: Driver found without registration')
def setUpClass(cls): DriverRegistry.register_driver(DriverTypes.CHROME, local=False) cls.driver = DriverRegistry.get_driver() cls.driver.goto_url(cls.TEST_URL, absolute=True)
def tearDownClass(cls): DriverRegistry.deregister_driver()
def driver(self): # type: () -> BaseDriver return DriverRegistry.get_driver()
def setUp(self): DriverRegistry.register_driver(driver_type=DriverTypes.CHROME, local=False)
def test_unsupported_local_driver_registration(self): with self.assertRaises(Exception) as context: DriverRegistry.register_driver(driver_type="random") self.assertTrue( 'Empty or Unknown driver type, valid options: chrome, firefox, safari, ie android, ios', context.exception)
def tearDown(self): DriverRegistry.deregister_driver()
def test_no_driver_deregistration_exception(self): self.assertIsNone(DriverRegistry.deregister_driver(), 'Error: Driver found without registration')
def validate_links(url): DriverRegistry.register_driver(DriverTypes.CHROMEHEADLESS, local=False) broken_links_list = _validate_links_on_url(url, DriverRegistry.get_webdriver()) DriverRegistry.deregister_driver() return broken_links_list
def validate_js_error(url): DriverRegistry.register_driver(DriverTypes.CHROMEHEADLESS, local=False) js_errors = _validate_js_error_on_url(url, DriverRegistry.get_webdriver()) DriverRegistry.deregister_driver() return js_errors
def driver_setup(request): DriverRegistry.register_driver(driver_type=request.param, local=False) driver = DriverRegistry.get_driver() yield driver DriverRegistry.deregister_driver()
def before_scenario(context, scenario): """ Open app instance form a browser :param context: behave.runner.Context :param scenario: behave.model.Scenario """ Logger.create_test_folder(scenario.name) context.test_name = scenario.name # cleanup app state for new test if context.driver is not None: try: DriverRegistry.deregister_driver() context.driver = None except Exception: logger.exception('Failed to stop browser instance') raise tags = str(context.config.tags) try: logger.debug("Building driver..") if 'api' not in tags: if 'android' in tags or 'ios' in tags: # Mobile tests if 'android-browser' in tags: context.driver = DriverRegistry.register_driver( DriverTypes.ANDROIDWEB, capabilities=context.android_capabilities, grid_url=context.selenium_grid_hub) elif 'ios-browser' in tags: context.driver = DriverRegistry.register_driver( DriverTypes.IOSWEB, capabilities=context.ios_capabilities, grid_url=context.selenium_grid_hub) elif 'android-app' in tags: context.driver = DriverRegistry.register_driver( DriverTypes.ANDROID, capabilities=context.android_capabilities, grid_url=context.selenium_grid_hub) elif 'ios-app' in tags: context.driver = DriverRegistry.register_driver( DriverTypes.IOS, capabilities=context.ios_capabilities, grid_url=context.selenium_grid_hub) else: logger.exception( "Only supports tags @android-app, @android-browser, @ios-app, @ios-browser" ) raise Exception( "Only supports tags @android-app, @android-browser, @ios-app, @ios-browser" ) else: # Desktop browser tests # Add Feature and Scenario name for grouping Zalenium Test # https://github.com/zalando/zalenium/blob/master/docs/usage_examples.md#test-name if not context.local and context.zalenium: context.browser_capabilities['name'] = context.test_name DriverRegistry.register_driver( driver_type=context.browser_capabilities['browserName'], capabilities=context.browser_capabilities, local=context.local, grid_url=context.selenium_grid_hub, options=context.options) context.driver = DriverRegistry.get_driver() context.driver.baseurl = context.testurl except Exception: logger.exception("Failed building the driver") raise logger.info('Start of Scenario: {}'.format(scenario.name))