Forms are everywhere on websites and apps. Why do testers often focus on testing forms?
Think about what happens when users enter data in forms and submit it.
Forms collect user data that can affect the system. Testing ensures data is correct and safe, preventing errors and security risks.
Given the code below, what will be printed after running the test?
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys browser = webdriver.Chrome() browser.get('https://example.com/login') username = browser.find_element(By.ID, 'username') password = browser.find_element(By.ID, 'password') username.send_keys('user1') password.send_keys('wrongpass') password.send_keys(Keys.RETURN) error = browser.find_element(By.ID, 'error-msg').text print(error) browser.quit()
Think about what happens when wrong credentials are submitted.
The code submits wrong credentials, so the page shows an error message 'Invalid username or password.'
You want to check if the success message 'Form submitted!' appears after submitting a form. Which assertion is correct?
success_message = driver.find_element(By.ID, 'success-msg').textCheck if the expected text is part of the message, not necessarily the whole text.
Using 'in' checks if the expected text is contained anywhere in the message, making the test more flexible.
You want to locate the email input field in a form. The label has for='email' and the input has id='email'. Which locator is best?
Use the locator that matches the input's unique id linked to the label.
Using ID is the most reliable and fastest way to locate the input associated with the label.
Review the code below. The test tries to fill and submit a form but fails. What is the cause?
driver.get('https://example.com/contact') name_input = driver.find_element(By.NAME, 'name') email_input = driver.find_element(By.NAME, 'email') submit_button = driver.find_element(By.ID, 'submit') name_input.send_keys('Alice') email_input.send_keys('alice@example.com') submit_button.click() success = driver.find_element(By.ID, 'success-msg').text print(success)
Check if the form or button is inside a frame or popup.
If the submit button is inside an iframe, Selenium must switch to that frame before interacting with elements inside it.