2424For information on Waiting until elements are present in the HTML see:
2525 https://selenium-python.readthedocs.io/waits.html
2626"""
27+ import re
2728import logging
29+ from typing import Any
2830from behave import when , then # pylint: disable=no-name-in-module
2931from selenium .webdriver .common .by import By
3032from selenium .webdriver .support .ui import Select , WebDriverWait
3133from selenium .webdriver .support import expected_conditions
3234
3335ID_PREFIX = "pet_"
3436
37+ def save_screenshot (context : Any , filename : str ) -> None :
38+ """Takes a snapshot of the web page for debugging and validation
39+
40+ Args:
41+ context (Any): The session context
42+ filename (str): The message that you are looking for
43+ """
44+ # Remove all non-word characters (everything except numbers and letters)
45+ filename = re .sub (r"[^\w\s]" , "" , filename )
46+ # Replace all runs of whitespace with a single dash
47+ filename = re .sub (r"\s+" , "-" , filename )
48+ context .driver .save_screenshot (f"./captures/{ filename } .png" )
49+
3550
3651@when ('I visit the "Home Page"' )
37- def step_impl (context ) :
52+ def step_impl (context : Any ) -> None :
3853 """Make a call to the base URL"""
3954 context .driver .get (context .base_url )
4055 # Uncomment next line to take a screenshot of the web page
41- # context.driver. save_screenshot('home_page.png ')
56+ # save_screenshot(context, 'Home Page ')
4257
4358
4459@then ('I should see "{message}" in the title' )
45- def step_impl (context , message ) :
60+ def step_impl (context : Any , message : str ) -> None :
4661 """Check the document title for a message"""
4762 assert message in context .driver .title
4863
4964
5065@then ('I should not see "{text_string}"' )
51- def step_impl (context , text_string ) :
66+ def step_impl (context : Any , text_string : str ) -> None :
5267 element = context .driver .find_element (By .TAG_NAME , "body" )
5368 assert text_string not in element .text
5469
5570
5671@when ('I set the "{element_name}" to "{text_string}"' )
57- def step_impl (context , element_name , text_string ) :
72+ def step_impl (context : Any , element_name : str , text_string : str ) -> None :
5873 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
5974 element = context .driver .find_element (By .ID , element_id )
6075 element .clear ()
6176 element .send_keys (text_string )
6277
6378
6479@when ('I select "{text}" in the "{element_name}" dropdown' )
65- def step_impl (context , text , element_name ) :
80+ def step_impl (context : Any , text : str , element_name : str ) -> None :
6681 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
6782 element = Select (context .driver .find_element (By .ID , element_id ))
6883 element .select_by_visible_text (text )
6984
7085
7186@then ('I should see "{text}" in the "{element_name}" dropdown' )
72- def step_impl (context , text , element_name ) :
87+ def step_impl (context : Any , text : str , element_name : str ) -> None :
7388 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
7489 element = Select (context .driver .find_element (By .ID , element_id ))
7590 assert element .first_selected_option .text == text
7691
7792
7893@then ('the "{element_name}" field should be empty' )
79- def step_impl (context , element_name ) :
94+ def step_impl (context : Any , element_name : str ) -> None :
8095 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
8196 element = context .driver .find_element (By .ID , element_id )
8297 assert element .get_attribute ("value" ) == ""
@@ -86,7 +101,7 @@ def step_impl(context, element_name):
86101# These two function simulate copy and paste
87102##################################################################
88103@when ('I copy the "{element_name}" field' )
89- def step_impl (context , element_name ) :
104+ def step_impl (context : Any , element_name : str ) -> None :
90105 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
91106 element = WebDriverWait (context .driver , context .wait_seconds ).until (
92107 expected_conditions .presence_of_element_located ((By .ID , element_id ))
@@ -96,7 +111,7 @@ def step_impl(context, element_name):
96111
97112
98113@when ('I paste the "{element_name}" field' )
99- def step_impl (context , element_name ) :
114+ def step_impl (context : Any , element_name : str ) -> None :
100115 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
101116 element = WebDriverWait (context .driver , context .wait_seconds ).until (
102117 expected_conditions .presence_of_element_located ((By .ID , element_id ))
@@ -115,13 +130,13 @@ def step_impl(context, element_name):
115130
116131
117132@when ('I press the "{button}" button' )
118- def step_impl (context , button ) :
133+ def step_impl (context : Any , button : str ) -> None :
119134 button_id = button .lower ().replace (" " , "_" ) + "-btn"
120135 context .driver .find_element (By .ID , button_id ).click ()
121136
122137
123138@then ('I should see "{name}" in the results' )
124- def step_impl (context , name ) :
139+ def step_impl (context : Any , name : str ) -> None :
125140 found = WebDriverWait (context .driver , context .wait_seconds ).until (
126141 expected_conditions .text_to_be_present_in_element (
127142 (By .ID , "search_results" ), name
@@ -131,13 +146,15 @@ def step_impl(context, name):
131146
132147
133148@then ('I should not see "{name}" in the results' )
134- def step_impl (context , name ) :
149+ def step_impl (context : Any , name : str ) -> None :
135150 element = context .driver .find_element (By .ID , "search_results" )
136151 assert name not in element .text
137152
138153
139154@then ('I should see the message "{message}"' )
140- def step_impl (context , message ):
155+ def step_impl (context : Any , message : str ) -> None :
156+ # Uncomment next line to take a screenshot of the web page for debugging
157+ # save_screenshot(context, message)
141158 found = WebDriverWait (context .driver , context .wait_seconds ).until (
142159 expected_conditions .text_to_be_present_in_element (
143160 (By .ID , "flash_message" ), message
@@ -155,7 +172,7 @@ def step_impl(context, message):
155172
156173
157174@then ('I should see "{text_string}" in the "{element_name}" field' )
158- def step_impl (context , text_string , element_name ) :
175+ def step_impl (context : Any , text_string : str , element_name : str ) -> None :
159176 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
160177 found = WebDriverWait (context .driver , context .wait_seconds ).until (
161178 expected_conditions .text_to_be_present_in_element_value (
@@ -166,7 +183,7 @@ def step_impl(context, text_string, element_name):
166183
167184
168185@when ('I change "{element_name}" to "{text_string}"' )
169- def step_impl (context , element_name , text_string ) :
186+ def step_impl (context : Any , element_name : str , text_string : str ) -> None :
170187 element_id = ID_PREFIX + element_name .lower ().replace (" " , "_" )
171188 element = WebDriverWait (context .driver , context .wait_seconds ).until (
172189 expected_conditions .presence_of_element_located ((By .ID , element_id ))
0 commit comments