2023-01-26 19:36:12 -06:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Necessary imports
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions
|
2023-01-27 15:53:20 -06:00
|
|
|
from selenium.webdriver.common.by import By
|
2023-01-26 19:36:12 -06:00
|
|
|
from robot.api.logger import info, error
|
|
|
|
from yaml import load, Loader
|
2023-01-27 15:53:20 -06:00
|
|
|
import time
|
2023-01-28 12:26:26 -06:00
|
|
|
import identity_block
|
2023-01-26 19:36:12 -06:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2023-01-28 12:26:26 -06:00
|
|
|
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))
|
2023-01-26 19:36:12 -06:00
|
|
|
except:
|
|
|
|
error("Couldn't open or parse the yaml document.")
|
|
|
|
return {}
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2023-01-27 15:53:20 -06:00
|
|
|
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):
|
2023-01-26 19:36:12 -06:00
|
|
|
# Iterate through buttons based on imported data
|
|
|
|
wait = WebDriverWait(driver, 10)
|
|
|
|
main_tab = driver.current_window_handle
|
2023-01-27 15:53:20 -06:00
|
|
|
original_page = standardize_url(driver.current_url)
|
2023-01-26 19:36:12 -06:00
|
|
|
for d in data:
|
|
|
|
try:
|
2023-01-27 15:53:20 -06:00
|
|
|
button_elements = grab_button_elements(driver)
|
2023-01-26 19:36:12 -06:00
|
|
|
try:
|
|
|
|
url = standardize_url(data[d]['url'])
|
|
|
|
except:
|
|
|
|
error(f'Some error in grabbing the url from: {d}')
|
|
|
|
return False
|
|
|
|
|
2023-01-27 15:53:20 -06:00
|
|
|
if (url == original_page):
|
|
|
|
info(
|
|
|
|
f'link does not go anywhere, passing. {url} {original_page}')
|
|
|
|
pass
|
|
|
|
|
2023-01-26 19:36:12 -06:00
|
|
|
try:
|
|
|
|
element = [e for e in button_elements if e.text == d][0]
|
2023-01-27 15:53:20 -06:00
|
|
|
info(f'Trying to open link labeled: "{element.text}"')
|
2023-01-26 19:36:12 -06:00
|
|
|
except:
|
|
|
|
error(
|
|
|
|
f'Button text does not match data file - data: {d} | elements: {[e.text for e in button_elements]}')
|
|
|
|
return False
|
2023-01-27 15:53:20 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
target = element.get_attribute('target')
|
|
|
|
info(f'target: {target}')
|
|
|
|
except:
|
|
|
|
error("Couldnt't find attribute 'target'.")
|
|
|
|
|
2023-01-26 19:36:12 -06:00
|
|
|
element.click()
|
2023-01-27 15:53:20 -06:00
|
|
|
|
|
|
|
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.')
|
2023-01-26 19:36:12 -06:00
|
|
|
return False
|
|
|
|
except:
|
|
|
|
error("Couldn't find the element somewhere...")
|
|
|
|
driver.close()
|
|
|
|
driver.quit()
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|