def init_driver(read_config_file, config_platform, config_browser, request): if config_platform.upper() == SupportedPlatforms.GRID.name: driver = webdriver.Remote( command_executor=read_config_file["remoteUrl"], desired_capabilities={ "browserName": config_browser, "selenoid:options": { "enableVNC": True } }) else: browser = BrowserFactory.get_browser(config_browser) driver = browser.init_browser() logger.info("Driver was initialized") driver.maximize_window() logger.info("Browser's window was maximized") driver.get(TestBase.get_data('baseUrl')) logger.info(f"Navigated to {TestBase.get_data('baseUrl')}") yield driver if driver: if request.node.rep_call.failed: allure.attach(driver.get_screenshot_as_png(), name=f"screenshot_{request.node.name}") logger.info(f"Screenshot was taken for test {request.node.name}") driver.quit() logger.info("Driver was closed")
def type(self, element, text): try: self.wait_for_element(element).send_keys(text) logger.info(f'Typed {text} in element {element}') except Exception: message = f"Typing in element {element} failed" logger.exception(message) raise Exception(message)
def click(self, element): try: self.wait_for_element(element).click() logger.info(f'Clicked on element {element}') except Exception: message = f"Clicking on element {element} failed" logger.exception(message) raise Exception(message)
def js_click(self, element): web_element = self.wait_for_element(element) try: self.driver.execute_script("arguments[0].click();", web_element) logger.info(f'Clicked on element using javascript {element}') except Exception: message = f"js clicking on element {element} failed" logger.exception(message) raise Exception(message)
def config_browser(read_config_file): # Validate and return the browser choice from the config data if 'browser' not in read_config_file: error = 'The config file does not contain "browser"' logger.error(error) raise Exception(error) elif read_config_file['browser'] not in SupportedBrowsers.__members__: error = f'"{read_config_file["browser"]}" is not a supported browser' logger.error(error) raise Exception(error) logger.info(f"{read_config_file['browser']} browser configured") return read_config_file['browser']
def config_platform(read_config_file): # Validate and return the platform choice (grid or local) from the config file if 'platform' not in read_config_file: error = 'The config file does not contain "platform"' logger.error(error) raise Exception(error) elif read_config_file['platform'].upper( ) not in SupportedPlatforms.__members__: error = f'"{read_config_file["platform"]}" is not a supported platform' logger.error(error) raise Exception(error) logger.info(f"{read_config_file['platform']} platform configured") return read_config_file['platform']
def get_page_url(self): logger.info(f'Page URL is "{self.driver.current_url}"') return self.driver.current_url
def get_page_title(self): logger.info(f'Page title is "{self.driver.title}"') return self.driver.title
def read_config_file(): with open(CONFIG_PATH) as config_file: data = json.load(config_file) logger.info("Read configuration file") return data