From e808bec658efde5461d260e789191d03f328ac99 Mon Sep 17 00:00:00 2001 From: glowzinthedark69 <75811554+glowzinthedark69@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:17:00 -0600 Subject: [PATCH 1/4] More practice --- src/Form_Test.py | 180 +++++++++++++------ src/Login_Sample_Test.py | 87 +++++++++ src/PageObjects/Login_Sample_Test_Objects.py | 52 ++++++ src/PageObjects/__init__.py | 0 src/{test.py => Python_Site_Test.py} | 0 src/main.py | 64 ------- src/page_objects.py | 60 +++++++ 7 files changed, 324 insertions(+), 119 deletions(-) create mode 100644 src/Login_Sample_Test.py create mode 100644 src/PageObjects/Login_Sample_Test_Objects.py create mode 100644 src/PageObjects/__init__.py rename src/{test.py => Python_Site_Test.py} (100%) delete mode 100644 src/main.py create mode 100644 src/page_objects.py diff --git a/src/Form_Test.py b/src/Form_Test.py index 1215d0f..a137b5b 100644 --- a/src/Form_Test.py +++ b/src/Form_Test.py @@ -5,84 +5,65 @@ from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException +from page_objects import TextBoxPage -class TestFormSubmission(unittest.TestCase): +class BaseTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() - self.driver.get("https://demoqa.com/text-box") + + +class TestFormSubmission(BaseTest): + def setUp(self): + super().setUp() # This will call the setUp method of BaseTest + self.text_box_page = TextBoxPage(self.driver) + self.text_box_page.open() def test_form_submission(self): - self.driver.find_element(By.ID, "userName").send_keys("student") - self.driver.find_element(By.ID, "userEmail").send_keys("test@test.com") - self.driver.find_element(By.ID, "currentAddress").send_keys( - "13678 Krameria St.\nThornton, CO\n80602" - ) - self.driver.find_element(By.ID, "permanentAddress").send_keys( - "13678 Krameria St.\nThornton, CO\n80602" - ) - - # Since the submit button is at the end of the form, we'll keep a wait before it to ensure all previous - # operations are complete - submit_button = WebDriverWait(self.driver, 5).until( - EC.element_to_be_clickable((By.ID, "submit")) - ) - submit_button.click() - - # Validate that the submission was successful after clicking the Submit button + self.text_box_page.fill_form( + "student", + "test@test.com", + "13678 Krameria St.\nThornton, CO\n80602", + "13678 Krameria St.\nThornton, CO\n80602") + + # Submit the form + self.text_box_page.submit_form() + try: - output = WebDriverWait(self.driver, 5).until( - EC.presence_of_element_located((By.ID, "output")) - ) - name = output.find_element(By.ID, "name") - assert "student" in name.text + validation_data = self.text_box_page.form_success_validation() - email = output.find_element(By.ID, "email") - assert "test@test.com" in email.text + assert "student" in validation_data["name"] + assert "test@test.com" in validation_data["email"] - # ... other assertions for output + # Add other assertions if needed... except TimeoutException: self.driver.quit() raise Exception("The expected output was not found on the page") + # Continue the rest of the test... - def test_form_submission_error(self): - self.driver.find_element(By.ID, "userName").send_keys("student") - self.driver.find_element(By.ID, "userEmail").send_keys("test#test.com") - self.driver.find_element(By.ID, "currentAddress").send_keys( - "13678 Krameria St.\nThornton, CO\n80602" - ) - self.driver.find_element(By.ID, "permanentAddress").send_keys( - "13678 Krameria St.\nThornton, CO\n80602" - ) - - # Since the submit button is at the end of the form, we'll keep a wait before it to ensure all previous - # operations are complete - submit_button = WebDriverWait(self.driver, 5).until( - EC.element_to_be_clickable((By.ID, "submit")) - ) - submit_button.click() + def test_form_submission_fail(self): + self.text_box_page.fill_form( + "student", + "test#test.com", + "13678 Krameria St.\nThornton, CO\n80602", + "13678 Krameria St.\nThornton, CO\n80602") + # Submit the form + self.text_box_page.submit_form() time.sleep(3) - # Validate that the submission was unsuccessful after clicking the Submit button try: - email_error = WebDriverWait(self.driver, 5).until( - EC.presence_of_element_located( - (By.CSS_SELECTOR, "#userEmail.field-error") - ) - ) + error_validation_data = self.text_box_page.form_error_validation() # Assert that the error field exists - assert email_error is not None - - # Get the CSS border property of the input field - border_color = email_error.value_of_css_property("border-color") + assert error_validation_data["error_element"] is not None # Assert that the color is what you expect - assert "rgb(255, 0, 0)" in border_color + # print("Actual border color:", error_validation_data["border_color"]) + assert "rgb(255, 0, 0)" in error_validation_data["border_color"] - # ... other assertions for output + # Add other assertions if needed... except TimeoutException: self.driver.quit() raise Exception("The expected output was not found on the page") @@ -93,3 +74,92 @@ def tearDown(self): if __name__ == "__main__": unittest.main() + +# Previous version of test without using page object model + +# class TestFormSubmissions(unittest.TestCase): +# def setUp(self): +# self.driver = webdriver.Chrome() +# self.driver.maximize_window() +# self.driver.get("https://demoqa.com/text-box") +# +# def test_form_submission(self): +# self.driver.find_element(By.ID, "userName").send_keys("student") +# self.driver.find_element(By.ID, "userEmail").send_keys("test@test.com") +# self.driver.find_element(By.ID, "currentAddress").send_keys( +# "13678 Krameria St.\nThornton, CO\n80602" +# ) +# self.driver.find_element(By.ID, "permanentAddress").send_keys( +# "13678 Krameria St.\nThornton, CO\n80602" +# ) +# +# # Since the submit button is at the end of the form, we'll keep a wait before it to ensure all previous +# # operations are complete +# submit_button = WebDriverWait(self.driver, 5).until( +# EC.element_to_be_clickable((By.ID, "submit")) +# ) +# submit_button.click() +# +# # Validate that the submission was successful after clicking the Submit button +# try: +# output = WebDriverWait(self.driver, 5).until( +# EC.presence_of_element_located((By.ID, "output")) +# ) +# name = output.find_element(By.ID, "name") +# assert "student" in name.text +# +# email = output.find_element(By.ID, "email") +# assert "test@test.com" in email.text +# +# # ... other assertions for output +# except TimeoutException: +# self.driver.quit() +# raise Exception("The expected output was not found on the page") +# +# def test_form_submission_error(self): +# self.driver.find_element(By.ID, "userName").send_keys("student") +# self.driver.find_element(By.ID, "userEmail").send_keys("test#test.com") +# self.driver.find_element(By.ID, "currentAddress").send_keys( +# "13678 Krameria St.\nThornton, CO\n80602" +# ) +# self.driver.find_element(By.ID, "permanentAddress").send_keys( +# "13678 Krameria St.\nThornton, CO\n80602" +# ) +# +# # Since the submit button is at the end of the form, we'll keep a wait before it to ensure all previous +# # operations are complete +# submit_button = WebDriverWait(self.driver, 5).until( +# EC.element_to_be_clickable((By.ID, "submit")) +# ) +# submit_button.click() +# +# time.sleep(3) +# +# # Validate that the submission was unsuccessful after clicking the Submit button +# try: +# email_error = WebDriverWait(self.driver, 5).until( +# EC.presence_of_element_located( +# (By.CSS_SELECTOR, "#userEmail.field-error") +# ) +# ) +# +# # Assert that the error field exists +# assert email_error is not None +# +# # Get the CSS border property of the input field +# border_color = email_error.value_of_css_property("border-color") +# +# # Assert that the color is what you expect +# assert "rgb(255, 0, 0)" in border_color +# +# # ... other assertions for output +# except TimeoutException: +# self.driver.quit() +# raise Exception("The expected output was not found on the page") +# +# def tearDown(self): +# self.driver.quit() + + +# if __name__ == "__main__": +# unittest.main() diff --git a/src/Login_Sample_Test.py b/src/Login_Sample_Test.py new file mode 100644 index 0000000..8bd2d0f --- /dev/null +++ b/src/Login_Sample_Test.py @@ -0,0 +1,87 @@ +import time +import unittest +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 TimeoutException +from src.PageObjects.Login_Sample_Test_Objects import LoginPage + + +class BaseTest(unittest.TestCase): + def setUp(self): + self.driver = webdriver.Chrome() + self.driver.maximize_window() + + +class TestLoginFunction(BaseTest): + + def setUp(self): + super().setUp() # This will call the setUp method of BaseTest + self.test_page = LoginPage(self.driver) + self.test_page.open() + + def test_form_submission(self): + self.test_page.fill_form( + "student", + "Password123") + + # Submit the form + self.test_page.submit_form() + + def test_login(self): + driver = webdriver.Chrome() # corrected line + # maximize window + driver.maximize_window() + # open the website + driver.get("https://practicetestautomation.com/practice-test-login/") + + # locating username field and keying in the username + username = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "username")) + ) + username.send_keys("student") + + # locating password field and keying in the password + password = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "password")) + ) + password.send_keys("Password123") + + # locating login button and clicking it to login + login_button = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.ID, "submit")) + ) + login_button.click() + + # Pause the execution of the script for 5 seconds + time.sleep(5) + + # Check if the login was successful by validating the presence of a logout button + logout_link = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.LINK_TEXT, "Log out")) + ) + + # Assert that the logout button is displayed, meaning a successful login + assert logout_link.is_displayed() + + # Attempt to locate the text element + try: + element = WebDriverWait(driver, 10).until( + EC.presence_of_element_located( + ( + By.XPATH, + "//*[text()='Congratulations student. You successfully logged in!']", + ) + ) + ) + except TimeoutException: + driver.quit() + raise Exception("The expected text was not found on the page") + + # close the driver + driver.quit() + + +if __name__ == "__main__": + unittest.main() diff --git a/src/PageObjects/Login_Sample_Test_Objects.py b/src/PageObjects/Login_Sample_Test_Objects.py new file mode 100644 index 0000000..df43f20 --- /dev/null +++ b/src/PageObjects/Login_Sample_Test_Objects.py @@ -0,0 +1,52 @@ +import unittest +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 + + +class LoginPage: + def __init__(self, driver): + self.driver = driver + self.url = "https://practicetestautomation.com/practice-test-login/" + self.user_name_field = (By.ID, "userName") + self.user_password_field = (By.ID, "password") + self.submit_button = (By.ID, "submit") + + def open(self): + self.driver.get(self.url) + + def fill_form(self, user_name, user_password): + self.driver.find_element(*self.user_name_field).send_keys(user_name) + self.driver.find_element(*self.user_password_field).send_keys(user_password) + + def submit_form(self): + submit_button = WebDriverWait(self.driver, 5).until( + EC.element_to_be_clickable(self.submit_button) + ) + submit_button.click() + + def form_success_validation(self): + output = WebDriverWait(self.driver, 5).until( + EC.presence_of_element_located(self.output_validation) + ) + name_validation = output.find_element(*self.name_validation_locator) + email_validation = output.find_element(*self.email_validation_locator) + # Extract other elements if needed... + + return { + "name": name_validation.text, + "email": email_validation.text + # Add other extracted data if needed... + } + + def form_error_validation(self): + email_error = WebDriverWait(self.driver, 5).until( + EC.presence_of_element_located((By.CSS_SELECTOR, "#userEmail.field-error")) + ) + border_color = email_error.value_of_css_property("border-color") + return { + "error_element": email_error, + "border_color": border_color + } + # In your test class: diff --git a/src/PageObjects/__init__.py b/src/PageObjects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/test.py b/src/Python_Site_Test.py similarity index 100% rename from src/test.py rename to src/Python_Site_Test.py diff --git a/src/main.py b/src/main.py deleted file mode 100644 index 6b45d36..0000000 --- a/src/main.py +++ /dev/null @@ -1,64 +0,0 @@ -import time -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 TimeoutException - - -def test_login(): - driver = webdriver.Chrome() # corrected line - # maximize window - driver.maximize_window() - # open the website - driver.get("https://practicetestautomation.com/practice-test-login/") - - # locating username field and keying in the username - username = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "username")) - ) - username.send_keys("student") - - # locating password field and keying in the password - password = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "password")) - ) - password.send_keys("Password123") - - # locating login button and clicking it to login - login_button = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "submit")) - ) - login_button.click() - - # Pause the execution of the script for 5 seconds - time.sleep(5) - - # Check if the login was successful by validating the presence of a logout button - logout_link = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.LINK_TEXT, "Log out")) - ) - - # Assert that the logout button is displayed, meaning a successful login - assert logout_link.is_displayed() - - # Attempt to locate the text element - try: - element = WebDriverWait(driver, 10).until( - EC.presence_of_element_located( - ( - By.XPATH, - "//*[text()='Congratulations student. You successfully logged in!']", - ) - ) - ) - except TimeoutException: - driver.quit() - raise Exception("The expected text was not found on the page") - - # close the driver - driver.quit() - - -if __name__ == "__main__": - test_login() diff --git a/src/page_objects.py b/src/page_objects.py new file mode 100644 index 0000000..70eef3f --- /dev/null +++ b/src/page_objects.py @@ -0,0 +1,60 @@ +import unittest + +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 + + +class TextBoxPage: + def __init__(self, driver): + self.driver = driver + self.url = "https://demoqa.com/text-box" + self.user_name_field = (By.ID, "userName") + self.user_email_field = (By.ID, "userEmail") + self.current_address_field = (By.ID, "currentAddress") + self.permanent_address_field = (By.ID, "permanentAddress") + self.submit_button = (By.ID, "submit") + self.output_validation = (By.ID, "output") + self.name_validation_locator = (By.ID, "name") + self.email_validation_locator = (By.ID, "email") + + def open(self): + self.driver.get(self.url) + + def fill_form(self, user_name, user_email, current_address, permanent_address): + self.driver.find_element(*self.user_name_field).send_keys(user_name) + self.driver.find_element(*self.user_email_field).send_keys(user_email) + self.driver.find_element(*self.current_address_field).send_keys(current_address) + self.driver.find_element(*self.permanent_address_field).send_keys(permanent_address) + + def submit_form(self): + submit_button = WebDriverWait(self.driver, 5).until( + EC.element_to_be_clickable(self.submit_button) + ) + submit_button.click() + + def form_success_validation(self): + output = WebDriverWait(self.driver, 5).until( + EC.presence_of_element_located(self.output_validation) + ) + name_validation = output.find_element(*self.name_validation_locator) + email_validation = output.find_element(*self.email_validation_locator) + # Extract other elements if needed... + + return { + "name": name_validation.text, + "email": email_validation.text + # Add other extracted data if needed... + } + + def form_error_validation(self): + email_error = WebDriverWait(self.driver, 5).until( + EC.presence_of_element_located((By.CSS_SELECTOR, "#userEmail.field-error")) + ) + border_color = email_error.value_of_css_property("border-color") + return { + "error_element": email_error, + "border_color": border_color + } + # In your test class: From 4817e044749b7949f65a31ad1a4696a526f225fd Mon Sep 17 00:00:00 2001 From: glowzinthedark69 <75811554+glowzinthedark69@users.noreply.github.com> Date: Sun, 20 Aug 2023 09:38:35 -0600 Subject: [PATCH 2/4] More practice with page object model --- src/Login_Sample_Test.py | 103 ++++++++++--------- src/PageObjects/Login_Sample_Test_Objects.py | 59 ++++++----- 2 files changed, 90 insertions(+), 72 deletions(-) diff --git a/src/Login_Sample_Test.py b/src/Login_Sample_Test.py index 8bd2d0f..16e2413 100644 --- a/src/Login_Sample_Test.py +++ b/src/Login_Sample_Test.py @@ -13,6 +13,9 @@ def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() + def tearDown(self): + self.driver.quit() + class TestLoginFunction(BaseTest): @@ -28,56 +31,60 @@ def test_form_submission(self): # Submit the form self.test_page.submit_form() - - def test_login(self): - driver = webdriver.Chrome() # corrected line - # maximize window - driver.maximize_window() - # open the website - driver.get("https://practicetestautomation.com/practice-test-login/") - - # locating username field and keying in the username - username = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "username")) - ) - username.send_keys("student") - - # locating password field and keying in the password - password = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "password")) - ) - password.send_keys("Password123") - - # locating login button and clicking it to login - login_button = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.ID, "submit")) - ) - login_button.click() - - # Pause the execution of the script for 5 seconds time.sleep(5) - # Check if the login was successful by validating the presence of a logout button - logout_link = WebDriverWait(driver, 10).until( - EC.element_to_be_clickable((By.LINK_TEXT, "Log out")) - ) - - # Assert that the logout button is displayed, meaning a successful login - assert logout_link.is_displayed() - - # Attempt to locate the text element - try: - element = WebDriverWait(driver, 10).until( - EC.presence_of_element_located( - ( - By.XPATH, - "//*[text()='Congratulations student. You successfully logged in!']", - ) - ) - ) - except TimeoutException: - driver.quit() - raise Exception("The expected text was not found on the page") + def test_login_success(self): + self.test_page.validate_successful_login() + + # def test_login(self): + # driver = webdriver.Chrome() # corrected line + # # maximize window + # driver.maximize_window() + # # open the website + # driver.get("https://practicetestautomation.com/practice-test-login/") + # + # # locating username field and keying in the username + # username = WebDriverWait(driver, 10).until( + # EC.element_to_be_clickable((By.ID, "username")) + # ) + # username.send_keys("student") + # + # # locating password field and keying in the password + # password = WebDriverWait(driver, 10).until( + # EC.element_to_be_clickable((By.ID, "password")) + # ) + # password.send_keys("Password123") + # + # # locating login button and clicking it to login + # login_button = WebDriverWait(driver, 10).until( + # EC.element_to_be_clickable((By.ID, "submit")) + # ) + # login_button.click() + # + # # Pause the execution of the script for 5 seconds + # time.sleep(5) + # + # # Check if the login was successful by validating the presence of a logout button + # logout_link = WebDriverWait(driver, 10).until( + # EC.element_to_be_clickable((By.LINK_TEXT, "Log out")) + # ) + # + # # Assert that the logout button is displayed, meaning a successful login + # assert logout_link.is_displayed() + # + # # Attempt to locate the text element + # try: + # element = WebDriverWait(driver, 10).until( + # EC.presence_of_element_located( + # ( + # By.XPATH, + # "//*[text()='Congratulations student. You successfully logged in!']", + # ) + # ) + # ) + # except TimeoutException: + # driver.quit() + # raise Exception("The expected text was not found on the page") # close the driver driver.quit() diff --git a/src/PageObjects/Login_Sample_Test_Objects.py b/src/PageObjects/Login_Sample_Test_Objects.py index df43f20..5e420f8 100644 --- a/src/PageObjects/Login_Sample_Test_Objects.py +++ b/src/PageObjects/Login_Sample_Test_Objects.py @@ -9,15 +9,27 @@ class LoginPage: def __init__(self, driver): self.driver = driver self.url = "https://practicetestautomation.com/practice-test-login/" - self.user_name_field = (By.ID, "userName") + self.user_name_field = (By.ID, "username") self.user_password_field = (By.ID, "password") self.submit_button = (By.ID, "submit") + self.login_message = (By.XPATH, + "//*[text()='Congratulations student. You successfully logged in!']") + self.logout_button = (By.LINK_TEXT, "Log out") def open(self): self.driver.get(self.url) def fill_form(self, user_name, user_password): + # Wait for the 'userName' field to be present before interacting with it + WebDriverWait(self.driver, 5).until( + EC.presence_of_element_located(self.user_name_field) + ) self.driver.find_element(*self.user_name_field).send_keys(user_name) + + # Similarly, wait for the 'password' field + WebDriverWait(self.driver, 5).until( + EC.presence_of_element_located(self.user_password_field) + ) self.driver.find_element(*self.user_password_field).send_keys(user_password) def submit_form(self): @@ -26,27 +38,26 @@ def submit_form(self): ) submit_button.click() - def form_success_validation(self): - output = WebDriverWait(self.driver, 5).until( - EC.presence_of_element_located(self.output_validation) - ) - name_validation = output.find_element(*self.name_validation_locator) - email_validation = output.find_element(*self.email_validation_locator) - # Extract other elements if needed... - - return { - "name": name_validation.text, - "email": email_validation.text - # Add other extracted data if needed... - } - - def form_error_validation(self): - email_error = WebDriverWait(self.driver, 5).until( - EC.presence_of_element_located((By.CSS_SELECTOR, "#userEmail.field-error")) - ) - border_color = email_error.value_of_css_property("border-color") - return { - "error_element": email_error, - "border_color": border_color - } + def get_login_success_message(self): + return WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(self.login_message)) + + def get_logout_button(self): + return WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable(self.logout_button)) + + def validate_successful_login(self): + # Check if the login message is displayed + assert self.get_login_success_message().is_displayed(), "Login success message not displayed" + + # Check if the logout button is displayed + assert self.get_logout_button().is_displayed(), "Logout button not displayed" + + # def form_error_validation(self): + # email_error = WebDriverWait(self.driver, 5).until( + # EC.presence_of_element_located((By.CSS_SELECTOR, "#userEmail.field-error")) + # ) + # border_color = email_error.value_of_css_property("border-color") + # return { + # "error_element": email_error, + # "border_color": border_color + # } # In your test class: From 03f84fb4c7003bf2827dc3877b9caadf61a8c385 Mon Sep 17 00:00:00 2001 From: glowzinthedark69 <75811554+glowzinthedark69@users.noreply.github.com> Date: Sun, 20 Aug 2023 09:59:42 -0600 Subject: [PATCH 3/4] More practice with page object model. Cleaning stuff up. --- src/Login_Sample_Test.py | 60 +------------------- src/PageObjects/Login_Sample_Test_Objects.py | 17 +----- 2 files changed, 5 insertions(+), 72 deletions(-) diff --git a/src/Login_Sample_Test.py b/src/Login_Sample_Test.py index 16e2413..5f36101 100644 --- a/src/Login_Sample_Test.py +++ b/src/Login_Sample_Test.py @@ -9,6 +9,7 @@ class BaseTest(unittest.TestCase): + # Each test will automatically open a new browser window at the start and close it at the end when inherited def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() @@ -17,14 +18,14 @@ def tearDown(self): self.driver.quit() -class TestLoginFunction(BaseTest): +class TestSuccessfulLogin(BaseTest): def setUp(self): super().setUp() # This will call the setUp method of BaseTest self.test_page = LoginPage(self.driver) self.test_page.open() - def test_form_submission(self): + def test_successful_login(self): self.test_page.fill_form( "student", "Password123") @@ -32,63 +33,8 @@ def test_form_submission(self): # Submit the form self.test_page.submit_form() time.sleep(5) - - def test_login_success(self): self.test_page.validate_successful_login() - # def test_login(self): - # driver = webdriver.Chrome() # corrected line - # # maximize window - # driver.maximize_window() - # # open the website - # driver.get("https://practicetestautomation.com/practice-test-login/") - # - # # locating username field and keying in the username - # username = WebDriverWait(driver, 10).until( - # EC.element_to_be_clickable((By.ID, "username")) - # ) - # username.send_keys("student") - # - # # locating password field and keying in the password - # password = WebDriverWait(driver, 10).until( - # EC.element_to_be_clickable((By.ID, "password")) - # ) - # password.send_keys("Password123") - # - # # locating login button and clicking it to login - # login_button = WebDriverWait(driver, 10).until( - # EC.element_to_be_clickable((By.ID, "submit")) - # ) - # login_button.click() - # - # # Pause the execution of the script for 5 seconds - # time.sleep(5) - # - # # Check if the login was successful by validating the presence of a logout button - # logout_link = WebDriverWait(driver, 10).until( - # EC.element_to_be_clickable((By.LINK_TEXT, "Log out")) - # ) - # - # # Assert that the logout button is displayed, meaning a successful login - # assert logout_link.is_displayed() - # - # # Attempt to locate the text element - # try: - # element = WebDriverWait(driver, 10).until( - # EC.presence_of_element_located( - # ( - # By.XPATH, - # "//*[text()='Congratulations student. You successfully logged in!']", - # ) - # ) - # ) - # except TimeoutException: - # driver.quit() - # raise Exception("The expected text was not found on the page") - - # close the driver - driver.quit() - if __name__ == "__main__": unittest.main() diff --git a/src/PageObjects/Login_Sample_Test_Objects.py b/src/PageObjects/Login_Sample_Test_Objects.py index 5e420f8..a590914 100644 --- a/src/PageObjects/Login_Sample_Test_Objects.py +++ b/src/PageObjects/Login_Sample_Test_Objects.py @@ -1,5 +1,3 @@ -import unittest -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 @@ -46,18 +44,7 @@ def get_logout_button(self): def validate_successful_login(self): # Check if the login message is displayed - assert self.get_login_success_message().is_displayed(), "Login success message not displayed" + assert self.get_login_success_message().is_displayed(), "Login success message not displayed as expected" # Check if the logout button is displayed - assert self.get_logout_button().is_displayed(), "Logout button not displayed" - - # def form_error_validation(self): - # email_error = WebDriverWait(self.driver, 5).until( - # EC.presence_of_element_located((By.CSS_SELECTOR, "#userEmail.field-error")) - # ) - # border_color = email_error.value_of_css_property("border-color") - # return { - # "error_element": email_error, - # "border_color": border_color - # } - # In your test class: + assert self.get_logout_button().is_displayed(), "Logout button not displayed as expected" From 49331983ad9abc663b82083a78f160270f3f6ae2 Mon Sep 17 00:00:00 2001 From: glowzinthedark69 <75811554+glowzinthedark69@users.noreply.github.com> Date: Mon, 28 Aug 2023 11:03:40 -0600 Subject: [PATCH 4/4] More practice with page object model. Cleaning stuff up. --- src/Form_Test.py | 6 +----- src/{ => PageObjects}/page_objects.py | 0 2 files changed, 1 insertion(+), 5 deletions(-) rename src/{ => PageObjects}/page_objects.py (100%) diff --git a/src/Form_Test.py b/src/Form_Test.py index a137b5b..a810c02 100644 --- a/src/Form_Test.py +++ b/src/Form_Test.py @@ -1,11 +1,8 @@ import time import unittest 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 TimeoutException -from page_objects import TextBoxPage +from src.PageObjects.page_objects import TextBoxPage class BaseTest(unittest.TestCase): @@ -60,7 +57,6 @@ def test_form_submission_fail(self): assert error_validation_data["error_element"] is not None # Assert that the color is what you expect - # print("Actual border color:", error_validation_data["border_color"]) assert "rgb(255, 0, 0)" in error_validation_data["border_color"] # Add other assertions if needed... diff --git a/src/page_objects.py b/src/PageObjects/page_objects.py similarity index 100% rename from src/page_objects.py rename to src/PageObjects/page_objects.py