From 1fa63f79e6197dce01d0e00bb1853fc379771e85 Mon Sep 17 00:00:00 2001 From: newtech7j Date: Fri, 17 May 2024 06:40:31 +0100 Subject: [PATCH] new update on the test script this is the demo video https://cloud.zjnu.edu.cn/share/32b27be407c0c5fbe74e051030 --- test/SeleniumNeil/LRR | 1 + test/SeleniumNeil/helper.py | 33 +++++ test/SeleniumNeil/test_cases.py | 106 ---------------- test/SeleniumNeil/test_script_352.py | 177 +++++++++++++++++++++++++++ 4 files changed, 211 insertions(+), 106 deletions(-) create mode 160000 test/SeleniumNeil/LRR create mode 100644 test/SeleniumNeil/helper.py delete mode 100644 test/SeleniumNeil/test_cases.py create mode 100644 test/SeleniumNeil/test_script_352.py diff --git a/test/SeleniumNeil/LRR b/test/SeleniumNeil/LRR new file mode 160000 index 0000000..1a01b4f --- /dev/null +++ b/test/SeleniumNeil/LRR @@ -0,0 +1 @@ +Subproject commit 1a01b4facd4c9d1cce59ef84da114f55a57f616d diff --git a/test/SeleniumNeil/helper.py b/test/SeleniumNeil/helper.py new file mode 100644 index 0000000..bea59f9 --- /dev/null +++ b/test/SeleniumNeil/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/SeleniumNeil/test_cases.py b/test/SeleniumNeil/test_cases.py deleted file mode 100644 index 69570eb..0000000 --- a/test/SeleniumNeil/test_cases.py +++ /dev/null @@ -1,106 +0,0 @@ -# Each time you run the test script reset the database. -# For this test script you won't need it since it changes -# the Ta's email and name automatically -import re -import time -import pytest - -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.wait import WebDriverWait -from selenium.webdriver.support import expected_conditions as EC - -from conftest import url, driver, restore_database - - -def test_function_1(restore_database): - assert restore_database is None -@pytest.mark.skip(reason="function to be used in the test_scripts") -def createTA(driver, TA_name, emails, password): - full_name = driver.find_element('name', 'fullname') - full_name.send_keys(TA_name) - email = driver.find_element('name', 'email') - email.send_keys(emails) - pas = driver.find_element('name', 'password') - pas.send_keys(password) - usr_type = driver.find_element('name', 'type') - usr_type.click() - click_create = driver.find_element('name', 'create_btn') - click_create.click() - -def login_lecturer(drivers): - # Open the website - drivers.get(url()) - drivers.maximize_window() - - username_input = drivers.find_element('name', "user") - - password_input = drivers.find_element('name', "password") - - login_button = drivers.find_element('id', "login_btn") - - # login as a Lecturer - username_input.send_keys("admin@qq.com") - password_input.send_keys("123") - # Click the login button - time.sleep(10) - login_button.click() - admin_tab = drivers.find_element('id', 'admin_tab') - admin_tab.click() - - cte_instructor = drivers.find_element('id', 'tab_ins_accounts') - cte_instructor.click() - time.sleep(15) - - -def test_createTA(url, driver, restore_database): - driver_open = driver(); - driver_open.maximize_window() - login_lecturer(driver_open) - try: - fullname = "lanhuitest1" - email = "lanhuitest1@qq.com" - password = "new1452345678" - createTA(driver_open, fullname, email,password) # CREATE A TA WITH FULLNAME lanhuitest1 email lanhuitest1@qq.com password new1452345678 - - get_output = WebDriverWait(driver_open, 10).until( - EC.element_to_be_clickable((By.ID, "tab_ins_accounts")) - ) - get_output.click() - get_output_msg = driver_open.find_element(By.CLASS_NAME, "alert-warning") - txt_alert = get_output_msg.text - time.sleep(10) - - if txt_alert.find("TA user created successfully") == 0: - logout_button = WebDriverWait(driver_open, 20).until( - EC.element_to_be_clickable( - (By.XPATH, "//a[contains(@class, 'nav-link') and contains(@href, 'logout.php')]")) - ) - time.sleep(13) - logout_button.click() - time.sleep(5) - username_input = driver_open.find_element('name', "user") - password_input = driver_open.find_element('name', "password") - login_button = driver_open.find_element('id', "login_btn") - # login as the new TA - username_input.send_keys(email) # login with credentials of the created TA - password_input.send_keys(password) - # Click the login button - time.sleep(10) - - login_button.click() - - time.sleep(5) - elif txt_alert.find("Email address ") == 0: - - time.sleep(10) - driver_open.quit() - - else: - driver_open.quit() - - time.sleep(5) - - finally: - driver_open.quit() - diff --git a/test/SeleniumNeil/test_script_352.py b/test/SeleniumNeil/test_script_352.py new file mode 100644 index 0000000..d35c51e --- /dev/null +++ b/test/SeleniumNeil/test_script_352.py @@ -0,0 +1,177 @@ +# Each time you run the test script reset the database. +# For this test script you won't need it since it changes +# the Ta's email and name automatically +import re +import time +import pytest + +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + + +def test_function_1(restore_database): + + assert restore_database is None +@pytest.mark.skip(reason="function to be used in the test_scripts") +def createTA(driver, TA_name, emails, password): + full_name = driver.find_element('name', 'fullname') + full_name.send_keys(TA_name) + email = driver.find_element('name', 'email') + email.send_keys(emails) + pas = driver.find_element('name', 'password') + pas.send_keys(password) + usr_type = driver.find_element('name', 'type') + usr_type.click() + click_create = driver.find_element('name', 'create_btn') + click_create.click() + + +def test_createTA(url, driver, restore_database): + driver_open = driver + driver_open.maximize_window() + login(driver, url, "admin@qq.com", "123") + try: + fullname = "lanhuitest1" + email = "lanhuitest1@qq.com" + password = "new1452345678" + createTA(driver_open, fullname, email,password) # CREATE A TA WITH FULLNAME lanhuitest1 email lanhuitest1@qq.com password new1452345678 + + get_output = WebDriverWait(driver_open, 10).until( + EC.element_to_be_clickable((By.ID, "tab_ins_accounts")) + ) + get_output.click() + get_output_msg = driver_open.find_element(By.CLASS_NAME, "alert-warning") + txt_alert = get_output_msg.text + time.sleep(10) + + if txt_alert.find("TA user created successfully") == 0: + logout_button = WebDriverWait(driver_open, 20).until( + EC.element_to_be_clickable( + (By.XPATH, "//a[contains(@class, 'nav-link') and contains(@href, 'logout.php')]")) + ) + time.sleep(13) + logout_button.click() + time.sleep(5) + username_input = driver_open.find_element('name', "user") + password_input = driver_open.find_element('name', "password") + login_button = driver_open.find_element('id', "login_btn") + # login as the new TA + username_input.send_keys(email) # login with credentials of the created TA + password_input.send_keys(password) + # Click the login button + time.sleep(10) + + login_button.click() + assert result == "Passed", f"Test Case 1 create TA passed" + time.sleep(5) + elif txt_alert.find("Email address ") == 0: + + time.sleep(10) + driver_open.quit() + assert result == "Passed", f"Test Case 1 create TA failed" + else: + driver_open.quit() + + time.sleep(5) + + finally: + driver_open.quit() +def test_generate_password(url, driver, restore_database): + driver_open = driver + login(driver, url, "admin@qq.com", "123") + try: + fullname = "lanhuitest2" + email = "lanhuitest2@qq.com" + password = "" + createTA(driver_open, fullname, email, + password) # CREATE A TA WITH FULLNAME lanhuitest2 email lanhuitest2@qq.com password "" + + get_output = WebDriverWait(driver_open, 15).until( + EC.element_to_be_clickable((By.ID, "tab_ins_accounts")) + ) + get_output.click() + get_output_msg = driver_open.find_element(By.CLASS_NAME, "alert-warning") + txt_alert = get_output_msg.text + time.sleep(10) + + if txt_alert.find("TA user created successfully") == 0: + time.sleep(15) + email_pattern = r"Use email (\S+) as account name" + password_pattern = r" (\S+)\ as password." + email_match = re.search(email_pattern, txt_alert) + password_match = re.search(password_pattern, txt_alert) + if email_match and password_match: + # Extract email and password from the matches + email = email_match.group(1) + password = password_match.group(1) + logout_button = WebDriverWait(driver_open, 15).until( + EC.element_to_be_clickable( + (By.XPATH, "//a[contains(@class, 'nav-link') and contains(@href, 'logout.php')]")) + ) + logout_button.click() + time.sleep(15) + username_input = driver_open.find_element('name', "user") + password_input = driver_open.find_element('name', "password") + login_button = driver_open.find_element('id', "login_btn") + # login as the new TA + username_input.send_keys(email) # login with credentials of the created TA + password_input.send_keys(password) + # Click the login button + time.sleep(15) + + login_button.click() + assert result == "Passed", f"Test Case 2 generate passsword passed" + time.sleep(5) + + elif txt_alert.find("Email address ") == 0: + time.sleep(15) + driver_open.quit() + assert result == "Passed", f"Test Case 2 generate passsword failed" + else: + driver_open.quit() + + time.sleep(5) + + finally: + driver_open.quit() + +def test_existingTA(url, driver, restore_database): + driver_open = driver + login(driver, url, "admin@qq.com", "123") + try: +# Use email nreyes@example.com as account name and new1452345678 as password. + fullname = "lanhuitest1" + email = "lanhuitest1@qq.com" + password = "new1452345678" + createTA(driver_open, fullname, email, + password) # CREATE A TA WITH FULLNAME lanhuitest1 email lanhuitest1@qq.com password new1452345678 + + get_output = WebDriverWait(driver_open, 10).until( + EC.element_to_be_clickable((By.ID, "tab_ins_accounts")) + ) + get_output.click() + get_output_msg = driver_open.find_element(By.CLASS_NAME, "alert-warning") + txt_alert = get_output_msg.text + time.sleep(15) + + if txt_alert.find("TA user created successfully") == 0: + assert result == "Passed", f"Test Case 2 existing account passed" + time.sleep(5) + + + elif txt_alert.find("Email address ") == 0: + time.sleep(15) + + assert result == "Passed", f"Test Case 2 existing account failed" + driver_open.quit() + + else: + driver_open.quit() + + time.sleep(5) + + finally: + driver_open.quit() +