From b117f95b139dfd6dff6f43e684aed75edeeb0c23 Mon Sep 17 00:00:00 2001 From: yaaqob <3237084594@qq.com> Date: Sat, 13 Jan 2024 02:16:17 +0800 Subject: [PATCH] Mpiana added README and restore database state function --- test/SeleniumMpiana/README.md | 58 +++++++++++++++++++++++++++ test/SeleniumMpiana/test_assign_ta.py | 31 ++++++++++---- 2 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 test/SeleniumMpiana/README.md diff --git a/test/SeleniumMpiana/README.md b/test/SeleniumMpiana/README.md new file mode 100644 index 0000000..0d093b3 --- /dev/null +++ b/test/SeleniumMpiana/README.md @@ -0,0 +1,58 @@ +# Automated Testing with Selenium + +## Prerequisites +- Python 3 installed +- Chrome browser installed +- ChromeDriver downloaded and added to the system PATH ([Download ChromeDriver](https://sites.google.com/chromium.org/driver/)) +- MySQL Database configured with appropriate credentials + +## Running the Tests : + + + +# Configure Database Restoration: + +- Open test_assign_ta.py. +- Update the PASSWORD variable in the restore_database fixture with your MySQL root password. +- Running the Tests +- Open the test_assign_ta.py file and update the following variables with your login credentials: + +``` Login + username = "your_username@example.com" + password = "your_password" +Customize the list of courses and test cases in the courses_to_test variable: +``` + +``` The_courses_and_test_cases_to_test + courses_to_test = [ + {"id": 1, "name": "Your Course Name 1", "ta_assignments": {" Your TA Name 1": "Ta assigned successfully."}}, + {"id": 2, "name": "Your Course Name 2", "ta_assignments": {"Your TA Name 2": "The selected TA is already assigned to this course."}}, + # Add more courses as needed , also make sure the informations your entering is correct and same in your database ! + + # for exemple if you have a Ta named : [MPIANA] in your database and in the script you write [mpiana]. it will result on error or failed. + ] +``` +# Run the tests with this command : + +- pytest test_assign_ta.py + + +## How It Works : + +- The login function logs into the web application using the provided credentials. + +- After successful login, the script waits for redirection to the Admin page and verifies the URL. + +- The assign_ta function assigns a Teaching Assistant (TA) to a course and handles any alerts that may occur. + +- The tests in test_assign_ta.py use the pytest framework to run multiple test cases for TA assignments. + +- Test results, including success and failure information, are printed to the console and logged in test_results.txt. + +- Note: Ensure that the URLs and element locators in the code match the structure of the web application you are testing. Adjustments may be necessary based on the actual HTML structure. + +- Feel free to customize the list of courses and test cases based on the TAs and courses available in your instance of the web application. + +- For database restoration, make sure to provide the correct MySQL root password in the restore_database fixture. If any issues occur during testing, check the console output and logs for troubleshooting. + +Contact: @Mpiana_diego. \ No newline at end of file diff --git a/test/SeleniumMpiana/test_assign_ta.py b/test/SeleniumMpiana/test_assign_ta.py index 50d9e49..85fba33 100644 --- a/test/SeleniumMpiana/test_assign_ta.py +++ b/test/SeleniumMpiana/test_assign_ta.py @@ -6,12 +6,33 @@ from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException, UnexpectedAlertPresentException from selenium.webdriver.common.keys import Keys +import os + +# Fixture to restore the database +@pytest.fixture +def restore_database(): + '''Restore the database.''' + 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} < lrr_database.sql' + ] + + for command in cmds: + os.system(command) + + return None # New instance of the Chrome driver driver = webdriver.Chrome() # Open the login page -driver.get("http://localhost/lrr/admin.php") +driver.get("http://localhost/itech/lrr/admin.php") # Credentials for login username = "lanhui@qq.com" @@ -57,7 +78,7 @@ if login_result: admin_tab.click() # Optionally, wait for the Admin.php page to load - admin_url = "http://localhost/lrr/Admin.php" + admin_url = "http://localhost/itech/lrr/admin.php" WebDriverWait(driver, 15).until( EC.url_to_be(admin_url) ) @@ -102,7 +123,7 @@ courses_to_test = [ # Execute the tests @pytest.mark.parametrize("course", courses_to_test) -def test_assign_ta(course): +def test_assign_ta(restore_database,course): for ta_name, expected_result in course["ta_assignments"].items(): alert_text = assign_ta(driver, course["id"], ta_name) # ----- ---- Print the raw strings for debugging ----- ---- --- @@ -115,10 +136,6 @@ def test_assign_ta(course): else: result = "Failed" - # Write the result to a test file with test case number --- - with open("test_results.txt", "a") as file: - file.write(f"Test Case {test_case_number} - {course['name']} -- {ta_name}: Result={result}, Expected Result={expected_result}, Actual Alert Text={alert_text}\n") - # Print the result to the console --- print(f"Test Case {test_case_number} - {course['name']} -- {ta_name}: Result={result}, Expected Result={expected_result}, Actual Alert Text={alert_text}")