From 4024a36021068bf30acaa1eb4cc0607475d99ff9 Mon Sep 17 00:00:00 2001 From: Lan Hui Date: Fri, 10 May 2024 07:17:56 +0800 Subject: [PATCH] (1) Move conftest.py to the top level test directory so that it applies to all test subfolders; (2) Move login() to helper.py so that it could be reused by other test scripts. --- test/SeleniumMpiana/helper.py | 33 +++++++++++++++++++++++ test/SeleniumMpiana/test_bug418_yaaqob.py | 32 +--------------------- test/{SeleniumMpiana => }/conftest.py | 4 +-- 3 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 test/SeleniumMpiana/helper.py rename test/{SeleniumMpiana => }/conftest.py (90%) diff --git a/test/SeleniumMpiana/helper.py b/test/SeleniumMpiana/helper.py new file mode 100644 index 0000000..bea59f9 --- /dev/null +++ b/test/SeleniumMpiana/helper.py @@ -0,0 +1,33 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.common.exceptions import NoSuchElementException, UnexpectedAlertPresentException + + +def login(driver, url, username, password): + try: + driver.get(url) + + # Fill in the login form + user_input = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "user_name")) + ) + user_input.send_keys(username) + + password_input = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "user_password")) + ) + password_input.send_keys(password) + + # Click the login button + login_button = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "login_btn")) + ) + login_button.click() + + # Wait for the admin_tab to become clickable + admin_tab = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "admin_tab")) + ) + except (NoSuchElementException, UnexpectedAlertPresentException) as e: + return f"Error: {str(e)}" diff --git a/test/SeleniumMpiana/test_bug418_yaaqob.py b/test/SeleniumMpiana/test_bug418_yaaqob.py index 2c2835d..bb1a95d 100644 --- a/test/SeleniumMpiana/test_bug418_yaaqob.py +++ b/test/SeleniumMpiana/test_bug418_yaaqob.py @@ -1,40 +1,10 @@ import pytest -from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException, UnexpectedAlertPresentException - - -def login(driver, url, username, password): - try: - driver.get(url) - - # Fill in the login form - user_input = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "user_name")) - ) - user_input.send_keys(username) - - password_input = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "user_password")) - ) - password_input.send_keys(password) - - # Click the login button - login_button = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "login_btn")) - ) - login_button.click() - - # Wait for the admin_tab to become clickable - admin_tab = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "admin_tab")) - ) - except (NoSuchElementException, UnexpectedAlertPresentException) as e: - return f"Error: {str(e)}" - +from helper import login @pytest.mark.parametrize("course_id, course_name, ta_name", [(1, "Teecloudy - Ashly Course Testing", "Mark")]) def test_assign_a_new_ta_to_a_course(course_id, course_name, ta_name, driver, url, admin_username, admin_password, restore_database): diff --git a/test/SeleniumMpiana/conftest.py b/test/conftest.py similarity index 90% rename from test/SeleniumMpiana/conftest.py rename to test/conftest.py index 704cdb7..3741f44 100644 --- a/test/SeleniumMpiana/conftest.py +++ b/test/conftest.py @@ -11,7 +11,7 @@ def restore_database(): Benefit: we can reproduce the same test result. ''' - PASSWORD = '' # root password + PASSWORD = 'p-@va9' # root password DB_NAME = 'lrr' # database name used for LRR # commands used to import data to DB_NAME @@ -19,7 +19,7 @@ def restore_database(): f'mysql -u root -p{PASSWORD} -e "DROP DATABASE IF EXISTS {DB_NAME};"', f'mysql -u root -p{PASSWORD} -e "CREATE DATABASE {DB_NAME};"', f'mysql -u root -p{PASSWORD} -e "GRANT ALL PRIVILEGES ON {DB_NAME}.* TO lrr@localhost WITH GRANT OPTION;"', - f'mysql -u root -p{PASSWORD} {DB_NAME} < ../../lrr_database.sql'] + f'mysql -u root -p{PASSWORD} {DB_NAME} < ../lrr_database.sql'] for command in cmds: os.system(command)