from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException
import time
class WebInputTest:
def __init__(self, driver_path):
service = Service(driver_path)
self.driver = webdriver.Chrome(service=service)
self.driver.maximize_window()
def navigate_to_url(self, url):
"""Navigate to the specified URL."""
self.driver.get(url)
def fill_input_fields(self, INPUTDATE):
"""Fill in the input fields and validate outputs."""
try:
number_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-number\']')
number_input.send_keys('10')
text_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-text\']')
text_input.send_keys('nishanth')
password_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-password\']')
password_input.send_keys('password123')
date_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-date\']')
date_input.send_keys(INPUTDATE)
time.sleep(1)
display_button = self.driver.find_element(
By.ID, 'btn-display-inputs')
display_button.click()
except NoSuchElementException as e:
print("Element not found:", e)
def verify_field_values(self, OUTPUTDATE):
try:
number_output = self.driver.find_element(
By.XPATH, '//strong[@id=\'output-number\']').text
assert number_output == '10', 'Number Input : Test Case Failed'
print('Number Input : Test Case Passed')
text_output = self.driver.find_element(
By.XPATH, '//strong[@id=\'output-text\']').text
assert text_output == 'nishanth', 'Text Input : Test Case Failed'
print('Text Input : Test Case Passed')
password_output = self.driver.find_element(
By.XPATH, '//strong[@id=\'output-password\']'
).text
assert password_output == 'password123', 'Password Input : Test Case Failed'
print('Password Input : Test Case Passed')
date_output = self.driver.find_element(
By.XPATH, '//strong[@id=\'output-date\']'
).text
assert date_output == OUTPUTDATE, 'Date Input : Test Case Failed'
print('Date Input : Test Case Passed')
except NoSuchElementException as e:
print("Element not found:", e)
def close_browser(self):
"""Close the browser."""
self.driver.quit()
if __name__ == "__main__":
DRIVER_PATH = 'D:/testtools/chromedriver.exe'
TEST_URL = 'https://practice.expandtesting.com/inputs'
INPUTDATE = '28022023'
OUTPUTDATE = '2023-02-28'
test = WebInputTest(DRIVER_PATH)
test.navigate_to_url(TEST_URL)
test.fill_input_fields(INPUTDATE)
test.verify_field_values(OUTPUTDATE)
test.close_browser()