Verify typing text into a search input field
Preconditions (2)
✅ Expected Result: The search input field should display the typed text 'Selenium testing'
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 import unittest class TestTypingText(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/search') self.wait = WebDriverWait(self.driver, 10) def test_typing_text(self): driver = self.driver wait = self.wait # Wait until the search input is visible search_input = wait.until(EC.visibility_of_element_located((By.ID, 'search-input'))) # Clear any existing text search_input.clear() # Type the text search_input.send_keys('Selenium testing') # Assert the input's value is as expected self.assertEqual(search_input.get_attribute('value'), 'Selenium testing') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium WebDriver with Python's unittest framework.
In setUp, it opens the Chrome browser and navigates to the test page.
It waits explicitly for the search input field to be visible using WebDriverWait and expected_conditions.
Before typing, it clears the input field to avoid appending text.
Then it types 'Selenium testing' using send_keys.
Finally, it asserts that the input field's value matches the typed text.
The tearDown method closes the browser after the test.
Now add data-driven testing with 3 different input texts: 'Selenium testing', 'QA automation', 'Python scripting'
send_keys method do in Selenium Python?send_keyssend_keys method simulates typing text into input fields or editable elements in Selenium.click(), text property, or WebDriverWait.input_box?send_keys().send_keys on the element with the text as a string argument.input_box = driver.find_element(By.ID, "username")
input_box.send_keys("user123")
print(input_box.get_attribute("value"))send_keys("user123") types "user123" into the input box, setting its value attribute.get_attribute("value") returns the current text inside the input box, which is "user123".input_box = driver.find_element(By.ID, "user") input_box.send_keys(admin)
admin is not in quotes, so Python treats it as a variable, causing a NameError.send_keys("admin").clear() method to remove any existing text in the input box.send_keys("mypassword") to type the password.Pass the test and this pattern moves unknown