Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 124 additions & 58 deletions src/Form_Test.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
40 changes: 40 additions & 0 deletions src/Login_Sample_Test.py
Original file line number Diff line number Diff line change
@@ -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()
50 changes: 50 additions & 0 deletions src/PageObjects/Login_Sample_Test_Objects.py
Original file line number Diff line number Diff line change
@@ -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"
Empty file added src/PageObjects/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions src/PageObjects/page_objects.py
Original file line number Diff line number Diff line change
@@ -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:
File renamed without changes.
Loading