def pytest_runtest_makereport(item, call): pytest_html = item.config.pluginmanager.getplugin('html') outcome = yield report = outcome.get_result() extra = getattr(report, 'extra', []) if report.when in ('call', 'setup'): xfail = hasattr(report, 'wasxfail') if ((report.passed or report.skipped) and xfail) or (report.failed and not xfail): # only add additional html on failure try: if type(factory.get_shared_driver()) is not NoneObject: screen = take_screenshot( webdriver=factory.get_shared_driver(), filename=item.function.__name__) extra.append(pytest_html.extras.image(screen)) allure.attach.file(screen, item.function.__name__, allure.attachment_type.PNG) except Exception as e: print(f"Could not create a screenshot:\n{e}") if xfail: extra.append( pytest_html.extras.html( '<div style="color: orange">XFailed reason: {}</div><br />' .format(report.wasxfail))) report.extra = extra
def test_can_get_set_shared_driver(): selene.driver._shared_web_driver_source.driver = NoneObject("NoneObject") shared_driver = factory.get_shared_driver() assert isinstance(shared_driver, NoneObject) factory.set_shared_driver(get_test_driver()) shared_driver = factory.get_shared_driver() assert shared_driver.name == "firefox" shared_driver.quit()
def check_if_browser_log_has_errors(): full_log = factory.get_shared_driver().get_log('browser') errors = [x for x in full_log if x.get('level') == 'SEVERE'] if errors: for error in errors: logger.error(f'JS ERROR:\n{error}\n') return errors else: return False
def pytest_exception_interact(node, call, report): """Attach screenshot if test failed""" if report.failed: shutil.rmtree(TEST_REPORTS_DIR, ignore_errors=True) shutil.rmtree(os.path.join(TEST_REPORTS_DIR, 'screenshots'), ignore_errors=True) os.makedirs(TEST_REPORTS_DIR) try: check_if_browser_log_has_errors() factory.get_shared_driver().save_screenshot(SCREEN_PATH) with open(SCREEN_PATH, 'rb') as screen: allure.attach(screen.read(), 'screen', allure.attachment_type.PNG) factory.get_shared_driver().quit() except Exception: logger.info( f'failed to make screenshot for exception:\n{report.longrepr}')
def set_attribute(self, value): return factory.get_shared_driver().execute_script( f"arguments[0].setAttribute('value', '{value}')", self.get_actual_webelement())
def js_click(self): return factory.get_shared_driver().execute_script( "arguments[0].click();", self.get_actual_webelement())
def navigate(self): factory.get_shared_driver().get(f'{self.path}{self.url}') return self
def scroll_into_view(_web_element): factory.get_shared_driver().execute_script( 'arguments[0].scrollIntoView();', _web_element.get_actual_webelement()) return _web_element