werefox-cafe/tests/test_suite/shared_methods.py

78 lines
2.6 KiB
Python
Executable File

#!/usr/bin/python
# Necessary imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from robot.api.logger import info, error
from yaml import load, Loader
def standardize_url(url):
# Make sure there are no leading or trailing characters so we can check urls are identical
if (len(url) > 1):
if (url[-1:] == '/'):
url = url[:-1]
if (len(url) > 8):
if (url[:8] == 'https://'):
url = url[8:]
if (url[:7] == 'http://'):
url = url[7:]
return url
def load_data(filepaths):
# Load the data that *should* be on a templated page
data = {}
for filepath in filepaths:
try:
with open(filepath) as yaml_doc:
data.update(load(yaml_doc, Loader=Loader))
with open('/data/homepage/other.yml') as yaml_doc:
data.update(load(yaml_doc, Loader=Loader))
except:
error("Couldn't open or parse the yaml document.")
return {}
return data
def check_buttons(driver, data, button_elements):
# Iterate through buttons based on imported data
wait = WebDriverWait(driver, 10)
main_tab = driver.current_window_handle
for d in data:
try:
try:
url = standardize_url(data[d]['url'])
except:
error(f'Some error in grabbing the url from: {d}')
return False
try:
element = [e for e in button_elements if e.text == d][0]
info(element.text)
except:
error(
f'Button text does not match data file - data: {d} | elements: {[e.text for e in button_elements]}')
return False
element.click()
wait.until(expected_conditions.number_of_windows_to_be(2))
for window_handle in driver.window_handles:
if window_handle != main_tab:
driver.switch_to.window(window_handle)
wait.until(
expected_conditions.presence_of_all_elements_located)
if (url != standardize_url(driver.current_url)):
info(
f'Opened URL does not match data file - button_url: #{url}# | current_url: #{standardize_url(driver.current_url)}#')
return False
driver.close()
driver.switch_to.window(main_tab)
except:
error("Couldn't find the element somewhere...")
driver.close()
driver.quit()
return False
return True