diff --git a/src/Form_Test.py b/src/Form_Test.py index 1215d0f..a810c02 100644 --- a/src/Form_Test.py +++ b/src/Form_Test.py @@ -1,88 +1,65 @@ 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.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 + 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 +70,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..5f36101 --- /dev/null +++ b/src/Login_Sample_Test.py @@ -0,0 +1,40 @@ +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): + # 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() + + def tearDown(self): + self.driver.quit() + + +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_successful_login(self): + self.test_page.fill_form( + "student", + "Password123") + + # Submit the form + self.test_page.submit_form() + time.sleep(5) + self.test_page.validate_successful_login() + + +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..a590914 --- /dev/null +++ b/src/PageObjects/Login_Sample_Test_Objects.py @@ -0,0 +1,50 @@ +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") + 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): + submit_button = WebDriverWait(self.driver, 5).until( + EC.element_to_be_clickable(self.submit_button) + ) + submit_button.click() + + 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 as expected" + + # Check if the logout button is displayed + assert self.get_logout_button().is_displayed(), "Logout button not displayed as expected" diff --git a/src/PageObjects/__init__.py b/src/PageObjects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/PageObjects/page_objects.py b/src/PageObjects/page_objects.py new file mode 100644 index 0000000..70eef3f --- /dev/null +++ b/src/PageObjects/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: 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()