updated code tha uses fixture to restore the database. make sure the link of the database sql file is set nomally. this is the demo video https://cloud.zjnu.edu.cn/share/5d5a595aa863203768489383cd
							parent
							
								
									9d1670577e
								
							
						
					
					
						commit
						dcd0e522c7
					
				|  | @ -72,7 +72,7 @@ if ($_SESSION['user_type'] != "Lecturer" && $_SESSION['user_type'] != "Admin") { | |||
| 			Email | ||||
| 			<input type="text" name="email" placeholder="Email / Student Number" class="form-control" > <br> | ||||
|                         Initial password (Enter a strong password or leave it empty to let LRR generate one) | ||||
|                         <input type="password" class="form-control" name="password" minlength="8" placeholder="Initial password" > <br> | ||||
|                         <input type="password" class="form-control" name="password"  minlength="8" placeholder="Initial password" > <br> | ||||
| 			User type: | ||||
|                 <?php | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,48 @@ | |||
| import os | ||||
| import pytest | ||||
| from selenium import webdriver | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def restore_database(): | ||||
|     ''' Restore the database. | ||||
|         It is useful for making sure that each end-to-end test | ||||
|         starts with the same database. | ||||
|         Benefit: we can reproduce the same test result. | ||||
|     ''' | ||||
| 
 | ||||
|     PASSWORD = ''  # root password | ||||
|     DB_NAME = 'lrr' # database name used for LRR | ||||
| 
 | ||||
|     # commands used to import data to DB_NAME | ||||
|     cmds = [ | ||||
|         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} < C:\\xampp\\htdocs\\LRR\\lrr_database.sql'] #make sure th link of the database is ok | ||||
| 
 | ||||
|     try: | ||||
|         for command in cmds: | ||||
|             os.system(command) | ||||
|     except Exception as e: | ||||
|         print(f"Error restoring database: {e}") | ||||
| 
 | ||||
|     return None | ||||
| @pytest.fixture | ||||
| def url(): | ||||
|     return 'http://localhost/LRR/' # URL of LRR | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def driver(): | ||||
|     return  webdriver.Chrome() | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def admin_username(): | ||||
|     return 'admin@qq.com' | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def admin_password(): | ||||
|     return '123' | ||||
| 
 | ||||
|  | @ -4,13 +4,18 @@ | |||
| import re | ||||
| import time | ||||
| import pytest | ||||
| from faker import Faker | ||||
| 
 | ||||
| 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 | ||||
| 
 | ||||
| faker = Faker() | ||||
| 
 | ||||
| from .conftest import 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') | ||||
|  | @ -26,7 +31,7 @@ def createTA(driver, TA_name, emails, password): | |||
| 
 | ||||
| def login_lecturer(drivers): | ||||
|     # Open the website | ||||
|     drivers.get("http://localhost/lrr/") | ||||
|     drivers.get("http://localhost/LRR/") | ||||
|     drivers.maximize_window() | ||||
| 
 | ||||
|     username_input = drivers.find_element('name', "user") | ||||
|  | @ -36,27 +41,27 @@ def login_lecturer(drivers): | |||
|     login_button = drivers.find_element('id', "login_btn") | ||||
| 
 | ||||
|     # login as a Lecturer | ||||
|     username_input.send_keys("lanhui@qq.com") | ||||
|     password_input.send_keys("nil1234H@") | ||||
|     username_input.send_keys("admin@qq.com") | ||||
|     password_input.send_keys("123") | ||||
|     # Click the login button | ||||
|     time.sleep(5) | ||||
|     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(25) | ||||
|     time.sleep(15) | ||||
| 
 | ||||
| def test_createTA(): | ||||
|     driver_open = webdriver.Chrome() | ||||
|     driver_open.maximize_window() | ||||
|     login_lecturer(driver_open) | ||||
|     try: | ||||
|         fullname = faker.name() | ||||
|         email = faker.email() | ||||
|         fullname = "lanhuitest1" | ||||
|         email = "lanhuitest1@qq.com" | ||||
|         password = "new1452345678" | ||||
|         createTA(driver_open, fullname, email,password)  # CREATE A TA  WITH FULLNAME lanhuitest email lanhuitest@test.com  password lanhui12345678 | ||||
|         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")) | ||||
|  | @ -64,16 +69,16 @@ def test_createTA(): | |||
|         get_output.click() | ||||
|         get_output_msg = driver_open.find_element(By.CLASS_NAME, "alert-warning") | ||||
|         txt_alert = get_output_msg.text | ||||
|         time.sleep(20) | ||||
|         time.sleep(10) | ||||
| 
 | ||||
|         if txt_alert.find("TA user created successfully") == 0: | ||||
|             logout_button = WebDriverWait(driver_open, 15).until( | ||||
|             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(5) | ||||
|             time.sleep(13) | ||||
|             logout_button.click() | ||||
|             time.sleep(10) | ||||
|             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") | ||||
|  | @ -81,14 +86,14 @@ def test_createTA(): | |||
|             username_input.send_keys(email)  # login with credentials of the created TA | ||||
|             password_input.send_keys(password) | ||||
|             # Click the login button | ||||
|             time.sleep(20) | ||||
|             time.sleep(10) | ||||
| 
 | ||||
|             login_button.click() | ||||
| 
 | ||||
|             time.sleep(20) | ||||
|             time.sleep(5) | ||||
|         elif txt_alert.find("Email address ") == 0: | ||||
| 
 | ||||
|             time.sleep(22) | ||||
|             time.sleep(10) | ||||
|             driver_open.quit() | ||||
| 
 | ||||
|         else: | ||||
|  | @ -104,22 +109,22 @@ def test_generate_password(): | |||
|     driver_open = webdriver.Chrome() | ||||
|     login_lecturer(driver_open) | ||||
|     try: | ||||
|         fullname = faker.name() | ||||
|         email = faker.email() | ||||
|         fullname = "lanhuitest2" | ||||
|         email = "lanhuitest2@qq.com" | ||||
|         password = "" | ||||
|         createTA(driver_open, fullname, email, | ||||
|                  password)  # CREATE A TA  WITH FULLNAME lanhuitest email lanhuitest@test.com  password lanhui12345678 | ||||
|                  password)  # CREATE A TA  WITH FULLNAME lanhuitest2 email lanhuitest2@qq.com  password "" | ||||
| 
 | ||||
|         get_output = WebDriverWait(driver_open, 5).until( | ||||
|         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(20) | ||||
|         time.sleep(10) | ||||
| 
 | ||||
|         if txt_alert.find("TA user created successfully") == 0: | ||||
|             time.sleep(20) | ||||
|             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) | ||||
|  | @ -128,7 +133,7 @@ def test_generate_password(): | |||
|                 # Extract email and password from the matches | ||||
|                 email = email_match.group(1) | ||||
|                 password = password_match.group(1) | ||||
|             logout_button = WebDriverWait(driver_open, 10).until( | ||||
|             logout_button = WebDriverWait(driver_open, 15).until( | ||||
|                 EC.element_to_be_clickable( | ||||
|                     (By.XPATH, "//a[contains(@class, 'nav-link') and contains(@href, 'logout.php')]")) | ||||
|             ) | ||||
|  | @ -141,14 +146,14 @@ def test_generate_password(): | |||
|             username_input.send_keys(email)  # login with credentials of the created TA | ||||
|             password_input.send_keys(password) | ||||
|             # Click the login button | ||||
|             time.sleep(20) | ||||
|             time.sleep(15) | ||||
| 
 | ||||
|             login_button.click() | ||||
| 
 | ||||
|             time.sleep(20) | ||||
|             time.sleep(5) | ||||
| 
 | ||||
|         elif txt_alert.find("Email address ") == 0: | ||||
|             time.sleep(22) | ||||
|             time.sleep(15) | ||||
|             driver_open.quit() | ||||
| 
 | ||||
|         else: | ||||
|  | @ -164,31 +169,32 @@ def test_existingTA(): | |||
|     login_lecturer(driver_open) | ||||
|     try: | ||||
| # Use email nreyes@example.com as account name and new1452345678 as password. | ||||
|         fullname = "Maria" | ||||
|         email = "nreyes@example.com" | ||||
|         fullname = "lanhuitest1" | ||||
|         email = "lanhuitest1@qq.com" | ||||
|         password = "new1452345678" | ||||
|         createTA(driver_open, fullname, email, | ||||
|                  password)  # CREATE A TA  WITH FULLNAME lanhuitest email lanhuitest@test.com  password lanhui12345678 | ||||
|                  password)  # CREATE A TA  WITH FULLNAME lanhuitest1 email lanhuitest1@qq.com  password new1452345678 | ||||
| 
 | ||||
|         get_output = WebDriverWait(driver_open, 5).until( | ||||
|         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(20) | ||||
|         time.sleep(15) | ||||
| 
 | ||||
|         if txt_alert.find("TA user created successfully") == 0: | ||||
|             time.sleep(20) | ||||
|             time.sleep(5) | ||||
| 
 | ||||
| 
 | ||||
|         elif txt_alert.find("Email address ") == 0: | ||||
|             time.sleep(22) | ||||
|             time.sleep(15) | ||||
|             driver_open.quit() | ||||
| 
 | ||||
|         else: | ||||
|             driver_open.quit() | ||||
| 
 | ||||
|         time.sleep(5) | ||||
| 
 | ||||
|     finally: | ||||
|         driver_open.quit() | ||||
|         driver_open.quit() | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue