From cd7ece13bca5541db88ebdc21b6073c04d260705 Mon Sep 17 00:00:00 2001 From: yaaqob <3237084594@qq.com> Date: Wed, 27 Dec 2023 00:08:41 +0800 Subject: [PATCH] MPIANA updated test script test_assign_ta.py --- test/SeleniumMpiana/assign_ta_test.py | 65 -------------- test/SeleniumMpiana/test_assign_ta.py | 125 ++++++++++++++++++++++++++ test/SeleniumMpiana/test_results.txt | 4 +- 3 files changed, 127 insertions(+), 67 deletions(-) delete mode 100644 test/SeleniumMpiana/assign_ta_test.py create mode 100644 test/SeleniumMpiana/test_assign_ta.py diff --git a/test/SeleniumMpiana/assign_ta_test.py b/test/SeleniumMpiana/assign_ta_test.py deleted file mode 100644 index 11b6285..0000000 --- a/test/SeleniumMpiana/assign_ta_test.py +++ /dev/null @@ -1,65 +0,0 @@ -import pytest -from selenium import webdriver -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 UnexpectedAlertPresentException - -# New instance of the Chrome driver -driver = webdriver.Chrome() - -# Open the admin url -driver.get("http://localhost/itech/lrr/Admin.php") - -def assign_ta(driver, course_name, ta_name): - course_dropdown = driver.find_element('id', 'courseDropdown') - course_dropdown.click() - selected_course_option = WebDriverWait(driver, 1200).until( - EC.element_to_be_clickable((By.XPATH, f"//option[text()='{course_name}']")) - ) - selected_course_option.click() - - # Select the TA - ta_dropdown = driver.find_element('id', 'taDropdown') - ta_dropdown.click() - - # Check if ta_name is not null before selecting - if ta_name: - selected_ta_option = WebDriverWait(driver, 1200).until( - EC.element_to_be_clickable((By.XPATH, f"//option[text()='{ta_name}']")) - ) - selected_ta_option.click() - - # Click the Assign button - assign_button = driver.find_element('id', 'assignButton') - assign_button.click() - - # Use WebDriverWait for more reliable alert handling - try: - alert = WebDriverWait(driver, 1200).until(EC.alert_is_present()) - alert_text = alert.text - alert.accept() - return alert_text - except UnexpectedAlertPresentException: - return None - -# Generate all combinations of courses and TAs -courses = ["Python", "computer", "testing"] -tas = ["MPIANA", "KABWANGA", "mark"] - -@pytest.mark.parametrize("course_name, ta_name", [(course, ta) for course in courses for ta in tas]) -def assign_ta_test(course_name, ta_name): - alert_text = assign_ta(driver, course_name, ta_name) - - try: - assert "Success" in alert_text or "Error" in alert_text - result = "Passed" - except AssertionError: - result = "Failed" - - # Write the result to the text file - with open('test_results.txt', 'a') as result_file: - result_file.write(f"Course={course_name}, TA={ta_name}, Result={result}, Alert={alert_text}\n") - -# Close the browser window -driver.quit() diff --git a/test/SeleniumMpiana/test_assign_ta.py b/test/SeleniumMpiana/test_assign_ta.py new file mode 100644 index 0000000..50d9e49 --- /dev/null +++ b/test/SeleniumMpiana/test_assign_ta.py @@ -0,0 +1,125 @@ +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 +from selenium.webdriver.common.keys import Keys + +# New instance of the Chrome driver +driver = webdriver.Chrome() + +# Open the login page +driver.get("http://localhost/lrr/admin.php") + +# Credentials for login +username = "lanhui@qq.com" +password = "admin123" + +def login(driver, username, password): + try: + # 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")) + ) + + return True + + except (NoSuchElementException, UnexpectedAlertPresentException) as e: + return f"Error: {str(e)}" + +# Call the login function +login_result = login(driver, username, password) + +# Click on admin_tab after successful login +if login_result: + admin_tab = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "admin_tab")) + ) + admin_tab.click() + + # Optionally, wait for the Admin.php page to load + admin_url = "http://localhost/lrr/Admin.php" + WebDriverWait(driver, 15).until( + EC.url_to_be(admin_url) + ) + +print(login_result) + +def assign_ta(driver, course_id, ta_name): + try: + # Locate the form and select the TA + ta_form = WebDriverWait(driver, 15).until( + EC.presence_of_element_located((By.XPATH, f"//form[@id='drop_menu_form_{course_id}']")) + ) + + ta_dropdown = Select(ta_form.find_element(By.XPATH, ".//select[@name='ta']")) + ta_dropdown.select_by_visible_text(ta_name) + + # Submit the form using JavaScript + driver.execute_script("arguments[0].submit();", ta_form) + + # Wait for an expected alert and accept it + WebDriverWait(driver, 10).until(EC.alert_is_present()) + alert = driver.switch_to.alert + alert_text = alert.text + alert.accept() + + return alert_text + + except UnexpectedAlertPresentException as e: + # Unexpected alert, handle it as an error + return f"Error: Unexpected alert - {str(e)}" + + except (NoSuchElementException, Exception) as e: + return f"Error: {str(e)}" + + + +# The courses and test cases to test +courses_to_test = [ + {"id": 1, "name": "Teecloudy - Ashly Course Testing", "ta_assignments": {"JAMES": "Ta assigned successfully."}}, + {"id": 2, "name": "P.M2019 - Project Management", "ta_assignments": {"JAMES": "The selected TA is already assigned to this course."}}, +] + +# Execute the tests +@pytest.mark.parametrize("course", courses_to_test) +def test_assign_ta(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 ----- ---- --- + test_case_number = courses_to_test.index(course) + 1 + print(f"Test Case {test_case_number} - {course['name']} -- {ta_name}: Expected Result={expected_result}, Actual Alert Text={alert_text}") + + # Determine the result based on the comparison + if expected_result.lower() in alert_text.lower(): + result = "Passed" + 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}") + + assert result == "Passed", f"Test Case {test_case_number} failed: Result={result}, Expected Result={expected_result}, Actual Alert Text={alert_text}" diff --git a/test/SeleniumMpiana/test_results.txt b/test/SeleniumMpiana/test_results.txt index 139597f..4f55332 100644 --- a/test/SeleniumMpiana/test_results.txt +++ b/test/SeleniumMpiana/test_results.txt @@ -1,2 +1,2 @@ - - +Test Case 1 - Teecloudy - Ashly Course Testing -- JAMES: Result=Passed, Expected Result=Ta assigned successfully., Actual Alert Text=TA assigned successfully. +Test Case 2 - P.M2019 - Project Management -- DIEGO: Result=Passed, Expected Result=The selected TA is already assigned to this course., Actual Alert Text=The selected TA is already assigned to this course.