werefox-cafe/tests/test_suite/shared_methods.py

131 lines
4.5 KiB
Python
Executable File

#!/usr/bin/python
# Necessary imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from robot.api.logger import info, error
from yaml import load, Loader
import time
import identity_block
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:
if (filepath == '/data/identities.yml'):
data.update(identity_block.load_data())
else:
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 grab_button_elements(driver):
sites = []
# Try to grab button elements of maintained subdomains
try:
site_lists = driver.find_elements(
By.TAG_NAME, "nav")
for l in site_lists:
sites += l.find_elements(By.XPATH, ".//a")
site_lists = driver.find_elements(
By.TAG_NAME, "footer")
for l in site_lists:
sites += l.find_elements(By.XPATH, ".//a")
except:
error("Couldn't find the element.")
return []
return sites
def check_buttons(driver, data):
# Iterate through buttons based on imported data
wait = WebDriverWait(driver, 10)
main_tab = driver.current_window_handle
original_page = standardize_url(driver.current_url)
for d in data:
try:
button_elements = grab_button_elements(driver)
try:
url = standardize_url(data[d]['url'])
except:
error(f'Some error in grabbing the url from: {d}')
return False
if (url == original_page):
info(
f'link does not go anywhere, passing. {url} {original_page}')
pass
try:
element = [e for e in button_elements if e.text == d][0]
info(f'Trying to open link labeled: "{element.text}"')
except:
error(
f'Button text does not match data file - data: {d} | elements: {[e.text for e in button_elements]}')
return False
try:
target = element.get_attribute('target')
info(f'target: {target}')
except:
error("Couldnt't find attribute 'target'.")
element.click()
if (url != original_page):
if (target == '_blank'):
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)
else:
try:
wait.until(
expected_conditions.url_changes(url))
driver.back()
wait.until(
expected_conditions.presence_of_all_elements_located)
except:
error('Could not load page before timeout, I assume.')
return False
except:
error("Couldn't find the element somewhere...")
driver.close()
driver.quit()
return False
return True