Skip to main content

Web Inputs

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):
# """Initialize the WebDriver."""
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:
# Fill in the number input field
number_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-number\']') # Update the locator accordingly
number_input.send_keys('10')

# Fill in the text input field
text_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-text\']') # Update the locator accordingly
text_input.send_keys('nishanth')

# Fill in the password input field
password_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-password\']') # Update the locator accordingly
password_input.send_keys('password123')

# Fill in the date input field
date_input = self.driver.find_element(
By.XPATH, '//input[@id=\'input-date\']') # Update the locator accordingly
# MMDDYYYY format, adjust if the field expects another format
date_input.send_keys(INPUTDATE)

time.sleep(1)

# Click the 'Display Inputs' button
display_button = self.driver.find_element(
By.ID, 'btn-display-inputs') # Update the locator accordingly
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__":
# Replace with the actual path to your WebDriver
DRIVER_PATH = 'D:/testtools/chromedriver.exe'
TEST_URL = 'https://practice.expandtesting.com/inputs'
INPUTDATE = '28022023'
OUTPUTDATE = '2023-02-28'

# Initialize the test
test = WebInputTest(DRIVER_PATH)

# Navigate to the test URL
test.navigate_to_url(TEST_URL)

# Fill input fields and perform actions
test.fill_input_fields(INPUTDATE)

# Verify the output values
test.verify_field_values(OUTPUTDATE)

# Close the browser after the test
test.close_browser()