Add UI tests for logs page and account page (finished the pages)

This commit is contained in:
Théophile Diot 2024-02-13 13:32:50 +01:00
parent 0ce18a7486
commit ea3c12c263
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
3 changed files with 440 additions and 1907 deletions

301
tests/ui/account_page.py Normal file
View file

@ -0,0 +1,301 @@
from logging import info as log_info, exception as log_exception, error as log_error
from time import sleep
from pyotp import TOTP
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from wizard import DRIVER, UI_PASSWORD
from utils import access_page, assert_button_click, safe_get_element
exit_code = 0
try:
log_info("Navigating to the logs page ...")
access_page(DRIVER, "/html/body/aside[1]/div[1]/div[2]/a", "account")
username_input = safe_get_element(DRIVER, By.ID, "admin_username")
assert isinstance(username_input, WebElement), "The username input is not an instance of WebElement"
if username_input.get_attribute("value") != "admin":
log_error("The username is not correct, exiting ...")
exit(1)
username_input.clear()
username_input.send_keys("admin2")
password_input = safe_get_element(DRIVER, By.ID, "curr_password")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
if password_input.get_attribute("value") != "":
log_error("The current password is not empty, exiting ...")
exit(1)
password_input.send_keys(UI_PASSWORD)
assert_button_click(DRIVER, "//button[@id='username-button' and @class='edit-btn']")
try:
title = safe_get_element(DRIVER, By.XPATH, "/html/body/main/div[1]/div/h1", error=True)
assert isinstance(title, WebElement), "The title is not an instance of WebElement"
if title.text != "Log in":
log_error("Didn't get redirected to login page, exiting ...")
exit(1)
except TimeoutException:
log_exception("Login page didn't load in time, exiting ...")
exit(1)
log_info("Successfully changed username, trying to log in with new username ...")
username_input = safe_get_element(DRIVER, By.ID, "username")
assert isinstance(username_input, WebElement), "The username input is not an instance of WebElement"
username_input.send_keys("admin2")
password_input = safe_get_element(DRIVER, By.ID, "password")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
password_input.send_keys(UI_PASSWORD)
access_page(DRIVER, "//button[@value='login']", "home")
access_page(DRIVER, "/html/body/aside[1]/div[1]/div[2]/a", "account")
username_input = safe_get_element(DRIVER, By.ID, "admin_username")
assert isinstance(username_input, WebElement), "The username input is not an instance of WebElement"
if username_input.get_attribute("value") != "admin2":
log_error("The username is not correct, exiting ...")
exit(1)
log_info("Successfully logged in with new username, trying to change password ...")
assert_button_click(DRIVER, "//button[@data-tab-handler='password']")
password_input = safe_get_element(DRIVER, By.XPATH, "//form[@data-plugin-item='password']//input[@id='curr_password']")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
if password_input.get_attribute("value") != "":
log_error("The current password is not empty, exiting ...")
exit(1)
password_input.send_keys(UI_PASSWORD)
new_password_input = safe_get_element(DRIVER, By.ID, "admin_password")
assert isinstance(new_password_input, WebElement), "The new password input is not an instance of WebElement"
if new_password_input.get_attribute("value") != "":
log_error("The new password is not empty, exiting ...")
exit(1)
new_password_input.send_keys("P@ssw0rd")
new_password_check_input = safe_get_element(DRIVER, By.ID, "admin_password_check")
assert isinstance(new_password_check_input, WebElement), "The new password check input is not an instance of WebElement"
if new_password_check_input.get_attribute("value") != "":
log_error("The new password check is not empty, exiting ...")
exit(1)
new_password_check_input.send_keys("P@ssw0rd")
assert_button_click(DRIVER, "//button[@id='pw-button' and @class='edit-btn']")
try:
title = safe_get_element(DRIVER, By.XPATH, "/html/body/main/div[1]/div/h1", error=True)
assert isinstance(title, WebElement), "The title is not an instance of WebElement"
if title.text != "Log in":
log_error("Didn't get redirected to login page, exiting ...")
exit(1)
except TimeoutException:
log_exception("Login page didn't load in time, exiting ...")
exit(1)
log_info("Successfully changed username, trying to log in with new password ...")
username_input = safe_get_element(DRIVER, By.ID, "username")
assert isinstance(username_input, WebElement), "The username input is not an instance of WebElement"
username_input.send_keys("admin2")
password_input = safe_get_element(DRIVER, By.ID, "password")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
password_input.send_keys("P@ssw0rd")
access_page(DRIVER, "//button[@value='login']", "home")
access_page(DRIVER, "/html/body/aside[1]/div[1]/div[2]/a", "account")
log_info("Successfully logged in with new password, trying 2FA ...")
assert_button_click(DRIVER, "//button[@data-tab-handler='totp']")
secret_token_input = safe_get_element(DRIVER, By.ID, "secret_token")
assert isinstance(secret_token_input, WebElement), "The secret token input is not an instance of WebElement"
secret_token = secret_token_input.get_attribute("value")
DRIVER.refresh()
WebDriverWait(DRIVER, 45).until(EC.presence_of_element_located((By.XPATH, "/html/body/div/header/div/nav/h6")))
assert_button_click(DRIVER, "//button[@data-tab-handler='totp']")
secret_token_input = safe_get_element(DRIVER, By.ID, "secret_token")
assert isinstance(secret_token_input, WebElement), "The secret token input is not an instance of WebElement"
new_secret_token = secret_token_input.get_attribute("value")
if new_secret_token == secret_token:
log_error("The secret token hasn't been changed, exiting ...")
exit(1)
assert new_secret_token, "The new secret token is empty"
log_info("The secret token has been changed, trying to activate 2FA ...")
totp = TOTP(new_secret_token)
totp_input = safe_get_element(DRIVER, By.ID, "totp_token")
assert isinstance(totp_input, WebElement), "The TOTP input is not an instance of WebElement"
totp_input.send_keys(totp.now())
password_input = safe_get_element(DRIVER, By.XPATH, "//form[@data-plugin-item='totp']//input[@id='curr_password']")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
if password_input.get_attribute("value") != "":
log_error("The new password check is not empty, exiting ...")
exit(1)
password_input.send_keys("P@ssw0rd")
access_page(DRIVER, "//button[@id='totp-button' and @class='valid-btn']", "account")
assert_button_click(DRIVER, "//button[@data-tab-handler='totp']")
try:
totp_state = safe_get_element(DRIVER, By.XPATH, "/html/body/main/div/div/form[2]/h5")
assert isinstance(totp_state, WebElement), "The TOTP state is not an instance of WebElement"
if totp_state.text != "TOTP IS CURRENTLY ON":
log_error("TOTP is not activated, exiting ...")
exit(1)
except TimeoutException:
log_exception("TOTP has not been activated, exiting ...")
exit(1)
log_info("2FA has been activated, trying to log out ...")
assert_button_click(DRIVER, "//a[@href='logout']")
try:
title = safe_get_element(DRIVER, By.XPATH, "/html/body/main/div[1]/div/h1", error=True)
assert isinstance(title, WebElement), "The title is not an instance of WebElement"
if title.text != "Log in":
log_error("Didn't get redirected to login page, exiting ...")
exit(1)
except TimeoutException:
log_exception("Login page didn't load in time, exiting ...")
exit(1)
log_info("Successfully logged out, trying to log in with 2FA ...")
username_input = safe_get_element(DRIVER, By.ID, "username")
assert isinstance(username_input, WebElement), "The username input is not an instance of WebElement"
username_input.send_keys("admin2")
password_input = safe_get_element(DRIVER, By.ID, "password")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
password_input.send_keys("P@ssw0rd")
assert_button_click(DRIVER, "//button[@value='login']")
try:
totp_input = safe_get_element(DRIVER, By.ID, "totp_token")
assert isinstance(totp_input, WebElement), "The TOTP input is not an instance of WebElement"
except TimeoutException:
log_error("Didn't get redirected to 2FA page, exiting ...")
exit(1)
totp_input.send_keys("0000000")
assert_button_click(DRIVER, "//button[@value='login']")
sleep(5)
if not DRIVER.current_url.endswith("/totp"):
log_error("Didn't get redirected back to 2FA page, exiting ...")
exit(1)
totp_input = safe_get_element(DRIVER, By.ID, "totp_token")
assert isinstance(totp_input, WebElement), "The TOTP input is not an instance of WebElement"
totp_input.send_keys(totp.now())
access_page(DRIVER, "//button[@value='login']", "home")
log_info("Successfully logged in with 2FA, trying to deactivate 2FA ...")
access_page(DRIVER, "/html/body/aside[1]/div[1]/div[2]/a", "account")
assert_button_click(DRIVER, "//button[@data-tab-handler='totp']")
totp_input = safe_get_element(DRIVER, By.ID, "totp_token")
assert isinstance(totp_input, WebElement), "The TOTP input is not an instance of WebElement"
totp_input.send_keys(totp.now())
password_input = safe_get_element(DRIVER, By.XPATH, "//form[@data-plugin-item='totp']//input[@id='curr_password']")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
password_input.send_keys("P@ssw0rd")
access_page(DRIVER, "//button[@id='totp-button' and @class='delete-btn']", "account")
assert_button_click(DRIVER, "//button[@data-tab-handler='totp']")
try:
totp_state = safe_get_element(DRIVER, By.XPATH, "/html/body/main/div/div/form[2]/h5")
assert isinstance(totp_state, WebElement), "The TOTP state is not an instance of WebElement"
if totp_state.text != "TOTP IS CURRENTLY OFF":
log_error("TOTP is not deactivated, exiting ...")
exit(1)
except TimeoutException:
log_exception("TOTP has not been deactivated, exiting ...")
exit(1)
log_info("2FA has been deactivated, trying to log out ...")
assert_button_click(DRIVER, "//a[@href='logout']")
try:
title = safe_get_element(DRIVER, By.XPATH, "/html/body/main/div[1]/div/h1", error=True)
assert isinstance(title, WebElement), "The title is not an instance of WebElement"
if title.text != "Log in":
log_error("Didn't get redirected to login page, exiting ...")
exit(1)
except TimeoutException:
log_exception("Login page didn't load in time, exiting ...")
exit(1)
log_info("Successfully logged out, trying to log in without 2FA ...")
username_input = safe_get_element(DRIVER, By.ID, "username")
assert isinstance(username_input, WebElement), "The username input is not an instance of WebElement"
username_input.send_keys("admin2")
password_input = safe_get_element(DRIVER, By.ID, "password")
assert isinstance(password_input, WebElement), "The password input is not an instance of WebElement"
password_input.send_keys("P@ssw0rd")
access_page(DRIVER, "//button[@value='login']", "home")
log_info("Successfully logged in without 2FA")
log_info("✅ Account page tests finished successfully")
except SystemExit as e:
exit_code = e.code
except KeyboardInterrupt:
exit_code = 1
except:
log_exception("Something went wrong, exiting ...")
DRIVER.save_screenshot("error.png")
exit_code = 1
finally:
DRIVER.quit()
exit(exit_code)

139
tests/ui/logs_page.py Normal file
View file

@ -0,0 +1,139 @@
from datetime import datetime, timedelta
from logging import info as log_info, exception as log_exception, error as log_error
from time import sleep
from requests import get
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from wizard import DRIVER, UI_URL
from utils import access_page, assert_button_click, safe_get_element
exit_code = 0
try:
log_info("Navigating to the logs page ...")
access_page(DRIVER, "/html/body/aside[1]/div[1]/div[3]/ul/li[11]/a", "logs")
log_info("Selecting correct instance ...")
assert_button_click(DRIVER, "//button[@data-logs-setting-select='instances']")
instances = safe_get_element(DRIVER, By.XPATH, "//div[@data-logs-setting-select-dropdown='instances']/button", multiple=True)
assert isinstance(instances, list), "Instances is not a list"
first_instance = instances[0].text
if len(instances) == 0:
log_error("No instances found, exiting ...")
exit(1)
assert_button_click(DRIVER, instances[0])
submit_button = safe_get_element(DRIVER, By.ID, "submit-data")
assert isinstance(submit_button, WebElement), "Submit button is not a WebElement"
assert_button_click(DRIVER, submit_button)
sleep(3)
logs_list = safe_get_element(DRIVER, By.XPATH, "//ul[@data-logs-list='']/li", multiple=True)
assert isinstance(logs_list, list), "Logs list is not a list"
if len(logs_list) == 0:
log_error("No logs found, exiting ...")
exit(1)
log_info("Logs found, trying auto refresh ...")
live_update_button = safe_get_element(DRIVER, By.ID, "live-update")
assert isinstance(live_update_button, WebElement), "Live update button is not a WebElement"
assert_button_click(DRIVER, live_update_button)
submit_live_button = safe_get_element(DRIVER, By.ID, "submit-live")
assert isinstance(submit_live_button, WebElement), "Submit live button is not a WebElement"
assert_button_click(DRIVER, submit_live_button)
sleep(3)
new_logs_list = safe_get_element(DRIVER, By.XPATH, "//ul[@data-logs-list='']/li[not(contains(@class, 'hidden'))]", multiple=True)
assert isinstance(new_logs_list, list), "New logs list is not a list"
if len(logs_list) == len(new_logs_list):
log_error("Auto refresh is not working, exiting ...")
exit(1)
log_info("Auto refresh is working, deactivating it ...")
live_update_button = safe_get_element(DRIVER, By.ID, "live-update")
assert isinstance(live_update_button, WebElement), "Live update button is not a WebElement"
assert_button_click(DRIVER, live_update_button)
submit_button = safe_get_element(DRIVER, By.ID, "submit-data")
assert isinstance(submit_button, WebElement), "Submit button is not a WebElement"
assert_button_click(DRIVER, submit_button)
sleep(3)
logs_list = safe_get_element(DRIVER, By.XPATH, "//ul[@data-logs-list='']/li", multiple=True)
assert isinstance(logs_list, list), "Logs list is not a list"
log_info("Trying filters ...")
filter_input = safe_get_element(DRIVER, By.ID, "keyword")
assert isinstance(filter_input, WebElement), "Filter input is not a WebElement"
filter_input.send_keys("gen")
sleep(3)
new_logs_list = safe_get_element(DRIVER, By.XPATH, "//ul[@data-logs-list='']/li[not(contains(@class, 'hidden'))]", multiple=True)
assert isinstance(new_logs_list, list), "New logs list is not a list"
if len(logs_list) == len(new_logs_list):
log_error("The keyword filter is not working, exiting ...")
exit(1)
filter_input.clear()
log_info("Keyword filter is working, trying type filter ...")
assert_button_click(DRIVER, "//button[@data-logs-setting-select='types']")
assert_button_click(DRIVER, "//div[@data-logs-setting-select-dropdown='types']/button[@value='warn']")
new_logs_list = safe_get_element(DRIVER, By.XPATH, "//ul[@data-logs-list='']/li[not(contains(@class, 'hidden'))]", multiple=True)
assert isinstance(new_logs_list, list), "New logs list is not a list"
if len(logs_list) == len(new_logs_list):
log_error("The keyword filter is not working, exiting ...")
exit(1)
assert_button_click(DRIVER, "//button[@data-logs-setting-select='types']")
assert_button_click(DRIVER, "//div[@data-logs-setting-select-dropdown='types']/button[@value='all']")
log_info("Type filter is working, trying to filter by date ...")
current_date = datetime.now()
resp = get(
f"http://www.example.com{UI_URL}/logs/{first_instance}?from_date={int((current_date - timedelta(weeks=1)).timestamp())}&to_date={int((current_date - timedelta(days=1)).timestamp())}",
headers={"Host": "www.example.com", "User-Agent": DRIVER.execute_script("return navigator.userAgent;")},
cookies={"session": DRIVER.get_cookies()[0]["value"]},
)
if len(resp.json()["logs"]) != 0:
log_error("The date filter is not working, exiting ...")
exit(1)
log_info("Date filter is working, trying jobs page ...")
log_info("✅ Lobs page tests finished successfully")
except SystemExit as e:
exit_code = e.code
except KeyboardInterrupt:
exit_code = 1
except:
log_exception("Something went wrong, exiting ...")
DRIVER.save_screenshot("error.png")
exit_code = 1
finally:
DRIVER.quit()
exit(exit_code)

File diff suppressed because it is too large Load diff