Skip to content

Commit

Permalink
Added ability to save screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
rofrano committed Nov 27, 2024
1 parent 675679f commit 2fcd3b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
Empty file added captures/.gitkeep
Empty file.
49 changes: 33 additions & 16 deletions features/steps/web_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,74 @@
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
from selenium.webdriver.support import expected_conditions

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()
element.send_keys(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") == ""
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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))
Expand Down

0 comments on commit 2fcd3b4

Please sign in to comment.