def test_report_decorator(driver): driver.get("https://example.testproject.io/web/") driver.find_element_by_css_selector("#name").send_keys("John Smith") driver.find_element_by_css_selector("#password").send_keys("12345") driver.find_element_by_css_selector("#login").click() textfield_phone = (By.CSS_SELECTOR, "#phone") # The TypeRandomPhoneAction addon generates a unique phone number # and types that in the specified textfield # Inverting result using the StepSettings. with DriverStepSettings( driver, StepSettings( invert_result=True, screenshot_condition=TakeScreenshotConditionType.Failure, always_pass=True)): driver.addons().execute(TypeRandomPhoneAction("1", 10), *textfield_phone) with DriverStepSettings( driver, StepSettings( invert_result=True, screenshot_condition=TakeScreenshotConditionType.Failure)): driver.addons().execute(TypeRandomPhoneAction("1", 10), *textfield_phone)
def __init__(self, driver, step_settings: StepSettings): """Initializes the 'with' statement.""" self.previous_settings = driver.command_executor.settings self.driver = driver # If inherit take the previous step settings. if step_settings.sleep_timing_type and step_settings.sleep_timing_type is SleepTimingType.Inherit: step_settings.sleep_timing_type = self.previous_settings.sleep_timing_type if step_settings.screenshot_condition \ and step_settings.screenshot_condition is TakeScreenshotConditionType.Inherit: step_settings.screenshot_condition = self.previous_settings.screenshot_condition self.step_settings = step_settings
def test_wait_with_ec_invisible(driver, wait): # Case 1 - Should report passed. LoginPage(driver).open().login_as("John Smith", "12345") # Check successful login. assert ProfilePage(driver).greetings_are_displayed() is True ProfilePage(driver).logout() # Greeting label shouldn't be shown anymore after logout. textlabel_greetings = (By.CSS_SELECTOR, "#greetings") element_not_present = wait.until(ec.invisibility_of_element_located(textlabel_greetings)) assert element_not_present # Case 2 - Should report failed. try: wait.until(ec.title_is("Title that is definitely not this one.")) except TimeoutException: pass # Case 3 - Report with StepSettings # Inverting result and taking picture on success. # Wait should timeout and report fail which will be inverted to passed and include picture. with DriverStepSettings(driver=wait.driver, step_settings=StepSettings(invert_result=True, screenshot_condition=TakeScreenshotConditionType.Failure)): try: wait.until_not(ec.url_contains("TestProject")) except TimeoutException: pass
def execute(self, function_name, method, message): """Executes the function (until/until_not) silently (without reports/settings). This execution will take into account the defined step settings and will handle them only once before silently executing the function. Based on the function's result and given method, it will report this Step with all the needed information. Returns the result of the executed function. """ timeout_exception = None result = None step_helper = self.driver.command_executor.step_helper step_settings = self.driver.command_executor.settings # Save current disable_reports value and disable reports before executing the wait function. reports_disabled = self._driver.command_executor.disable_reports self._driver.report().disable_reports(True) # Handle driver timeout step_helper.handle_timeout(timeout=step_settings.timeout) # Handle sleep before step_helper.handle_sleep(sleep_timing_type=step_settings.sleep_timing_type, sleep_time=step_settings.sleep_time) # Execute the function with default StepSettings. with DriverStepSettings(self._driver, StepSettings()): try: result = getattr(super(), function_name)(method, message) passed = True if result else False except TimeoutException as e: passed = False timeout_exception = e # Handle sleep after step_helper.handle_sleep(sleep_timing_type=step_settings.sleep_timing_type, sleep_time=step_settings.sleep_time, step_executed=True) # Handle result passed, step_message = step_helper.handle_step_result(step_result=passed, invert_result=step_settings.invert_result, always_pass=step_settings.always_pass) # Handle screenshot condition screenshot = step_helper.take_screenshot(step_settings.screenshot_condition, passed) # Set the previous value of disable_reports. self._driver.report().disable_reports(reports_disabled) # Inferring function name - until / until not function_name = ' '.join(function_name.split('_')) # Getting all additional step information. step_name, step_attributes = self.get_report_details(method) self._driver.report().step(description=f'Wait {function_name} {step_name}', message=f'{step_message}{os.linesep}', passed=passed, inputs=step_attributes, screenshot=screenshot) # Always pass ignore result and thrown exception. if not result and step_settings.always_pass: return True # Raise exception if there was one. if timeout_exception: raise timeout_exception return result
def __init__(self, agent_client: AgentClient, command_executor): self._agent_client = agent_client self._command_executor = command_executor self._disable_reports = False self._disable_auto_test_reports = False self._disable_command_reports = False self._disable_redaction = False self._stashed_command = None self._latest_known_test_name = ReportHelper.infer_test_name() self._excluded_test_names = list() self._settings = StepSettings()
def driver(): driver = webdriver.Chrome( token="kJCeheoppFyo8uaZ17k0JQyBck1qLIf5ZrynbI6t7Fk1", project_name="Ran's Project", job_name="Python Test Check") step_settings = StepSettings( timeout=15000, screenshot_condition=TakeScreenshotConditionType.Failure) with DriverStepSettings(driver, step_settings): yield driver driver.quit()
def driver(): driver = webdriver.Chrome( token="MSlM0L0EXHkqZ0CLf98m9Qn4YRcBoPaaxsMEDsH6FrQ1", project_name="Demo Project", job_name="AirBnb Test") step_settings = StepSettings( timeout=15000, screenshot_condition=TakeScreenshotConditionType.Failure) with DriverStepSettings(driver, step_settings): yield driver driver.quit()
def driver(): driver = webdriver.Chrome(project_name="Workshop Python", job_name="Air BNB Test") step_settings = StepSettings( timeout=15000, screenshot_condition=TakeScreenshotConditionType.Always, sleep_time=2000, sleep_timing_type=SleepTimingType.Before) with DriverStepSettings(driver, step_settings): yield driver driver.quit()
def test_basic_login_with_step_settings(driver): step_settings = StepSettings( timeout=15000, screenshot_condition=TakeScreenshotConditionType.Success, sleep_timing_type=SleepTimingType.Before, sleep_time=1000) start_time = time() with DriverStepSettings(driver, step_settings): LoginPage(driver).open().login_as( "John Smith", "12345") # Has about 7 driver commands. end_time = time() assert end_time - start_time >= 7.0
def driver(): os.environ["TP_AGENT_URL"] = "http://localhost:8787" driver = webdriver.Firefox( token="MSlM0L0EXHkqZ0CLf98m9Qn4YRcBoPaaxsMEDsH6FrQ1", project_name="Demo Project", job_name="AirBnb Test") step_settings = StepSettings( timeout=15000, screenshot_condition=TakeScreenshotConditionType.Failure) with DriverStepSettings(driver, step_settings): yield driver driver.quit()
def driver(): # Define a different port for the 2nd container os.environ["TP_AGENT_URL"] = "http://localhost:8787" driver = webdriver.Firefox( token="kJCeheoppFyo8uaZ17k0JQyBck1qLIf5ZrynbI6t7Fk1", project_name="Ran's Project", job_name="Python Test Check") step_settings = StepSettings( timeout=15000, screenshot_condition=TakeScreenshotConditionType.Failure) with DriverStepSettings(driver, step_settings): yield driver driver.quit()
def __init__(self, agent_client: AgentClient, command_executor, remote_connection): self._agent_client = agent_client self._command_executor = command_executor self._disable_reports = False self._disable_auto_test_reports = False self._disable_command_reports = False self._disable_redaction = False self._stashed_command = None self._latest_known_test_name = ReportHelper.infer_test_name() self._excluded_test_names = list() self._step_helper = StepHelper( remote_connection, agent_client.agent_session.dialect == "W3C", agent_client.agent_session.session_id) self._settings = StepSettings()