From 2fcd3b4b0776058df42a9c92e61eaf3e19ad9da7 Mon Sep 17 00:00:00 2001 From: John Rofrano Date: Wed, 27 Nov 2024 16:54:14 +0000 Subject: [PATCH] Added ability to save screenshots --- captures/.gitkeep | 0 features/steps/web_steps.py | 49 +++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 captures/.gitkeep diff --git a/captures/.gitkeep b/captures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/features/steps/web_steps.py b/features/steps/web_steps.py index 654a035..179d77e 100644 --- a/features/steps/web_steps.py +++ b/features/steps/web_steps.py @@ -24,7 +24,9 @@ For information on Waiting until elements are present in the HTML see: https://selenium-python.readthedocs.io/waits.html """ +import re import logging +from typing import Any from behave import when, then # pylint: disable=no-name-in-module from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select, WebDriverWait @@ -32,29 +34,42 @@ ID_PREFIX = "pet_" +def save_screenshot(context: Any, filename: str) -> None: + """Takes a snapshot of the web page for debugging and validation + + Args: + context (Any): The session context + filename (str): The message that you are looking for + """ + # Remove all non-word characters (everything except numbers and letters) + filename = re.sub(r"[^\w\s]", "", filename) + # Replace all runs of whitespace with a single dash + filename = re.sub(r"\s+", "-", filename) + context.driver.save_screenshot(f"./captures/{filename}.png") + @when('I visit the "Home Page"') -def step_impl(context): +def step_impl(context: Any) -> None: """Make a call to the base URL""" context.driver.get(context.base_url) # Uncomment next line to take a screenshot of the web page - # context.driver.save_screenshot('home_page.png') + # save_screenshot(context, 'Home Page') @then('I should see "{message}" in the title') -def step_impl(context, message): +def step_impl(context: Any, message: str) -> None: """Check the document title for a message""" assert message in context.driver.title @then('I should not see "{text_string}"') -def step_impl(context, text_string): +def step_impl(context: Any, text_string: str) -> None: element = context.driver.find_element(By.TAG_NAME, "body") assert text_string not in element.text @when('I set the "{element_name}" to "{text_string}"') -def step_impl(context, element_name, text_string): +def step_impl(context: Any, element_name: str, text_string: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = context.driver.find_element(By.ID, element_id) element.clear() @@ -62,21 +77,21 @@ def step_impl(context, element_name, text_string): @when('I select "{text}" in the "{element_name}" dropdown') -def step_impl(context, text, element_name): +def step_impl(context: Any, text: str, element_name: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = Select(context.driver.find_element(By.ID, element_id)) element.select_by_visible_text(text) @then('I should see "{text}" in the "{element_name}" dropdown') -def step_impl(context, text, element_name): +def step_impl(context: Any, text: str, element_name: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = Select(context.driver.find_element(By.ID, element_id)) assert element.first_selected_option.text == text @then('the "{element_name}" field should be empty') -def step_impl(context, element_name): +def step_impl(context: Any, element_name: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = context.driver.find_element(By.ID, element_id) assert element.get_attribute("value") == "" @@ -86,7 +101,7 @@ def step_impl(context, element_name): # These two function simulate copy and paste ################################################################## @when('I copy the "{element_name}" field') -def step_impl(context, element_name): +def step_impl(context: Any, element_name: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = WebDriverWait(context.driver, context.wait_seconds).until( expected_conditions.presence_of_element_located((By.ID, element_id)) @@ -96,7 +111,7 @@ def step_impl(context, element_name): @when('I paste the "{element_name}" field') -def step_impl(context, element_name): +def step_impl(context: Any, element_name: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = WebDriverWait(context.driver, context.wait_seconds).until( expected_conditions.presence_of_element_located((By.ID, element_id)) @@ -115,13 +130,13 @@ def step_impl(context, element_name): @when('I press the "{button}" button') -def step_impl(context, button): +def step_impl(context: Any, button: str) -> None: button_id = button.lower().replace(" ", "_") + "-btn" context.driver.find_element(By.ID, button_id).click() @then('I should see "{name}" in the results') -def step_impl(context, name): +def step_impl(context: Any, name: str) -> None: found = WebDriverWait(context.driver, context.wait_seconds).until( expected_conditions.text_to_be_present_in_element( (By.ID, "search_results"), name @@ -131,13 +146,15 @@ def step_impl(context, name): @then('I should not see "{name}" in the results') -def step_impl(context, name): +def step_impl(context: Any, name: str) -> None: element = context.driver.find_element(By.ID, "search_results") assert name not in element.text @then('I should see the message "{message}"') -def step_impl(context, message): +def step_impl(context: Any, message: str) -> None: + # Uncomment next line to take a screenshot of the web page for debugging + # save_screenshot(context, message) found = WebDriverWait(context.driver, context.wait_seconds).until( expected_conditions.text_to_be_present_in_element( (By.ID, "flash_message"), message @@ -155,7 +172,7 @@ def step_impl(context, message): @then('I should see "{text_string}" in the "{element_name}" field') -def step_impl(context, text_string, element_name): +def step_impl(context: Any, text_string: str, element_name: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") found = WebDriverWait(context.driver, context.wait_seconds).until( expected_conditions.text_to_be_present_in_element_value( @@ -166,7 +183,7 @@ def step_impl(context, text_string, element_name): @when('I change "{element_name}" to "{text_string}"') -def step_impl(context, element_name, text_string): +def step_impl(context: Any, element_name: str, text_string: str) -> None: element_id = ID_PREFIX + element_name.lower().replace(" ", "_") element = WebDriverWait(context.driver, context.wait_seconds).until( expected_conditions.presence_of_element_located((By.ID, element_id))