Introduction
Interaction methods simulate user behavior to test how a website or app works when a real person uses it. This helps find problems before real users do.
Interaction methods simulate user behavior to test how a website or app works when a real person uses it. This helps find problems before real users do.
driver.findElement(By.id("elementId")).click(); driver.findElement(By.name("inputName")).sendKeys("text");
click() and sendKeys() to mimic real user actions.id or name for stable tests.driver.findElement(By.id("submitBtn")).click();driver.findElement(By.name("username")).sendKeys("testuser");
driver.findElement(By.cssSelector(".menu-item")).click();This test opens a login page, types username and password like a user, clicks login, and checks if a welcome message appears.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class UserInteractionTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/login"); driver.findElement(By.id("username")).sendKeys("testuser"); driver.findElement(By.id("password")).sendKeys("password123"); driver.findElement(By.id("loginBtn")).click(); String welcomeText = driver.findElement(By.id("welcomeMessage")).getText(); if (welcomeText.contains("Welcome")) { System.out.println("Test Passed: Login successful."); } else { System.out.println("Test Failed: Login unsuccessful."); } } finally { driver.quit(); } } }
Interaction methods help catch issues that only appear when a real user clicks or types.
Always wait for elements to be ready before interacting to avoid errors.
Interaction methods mimic real user actions like clicking and typing.
This helps find problems that only show up during actual use.
Using these methods makes tests more reliable and realistic.
click() and sendKeys() simulate real user behavior?sendKeys().element.sendKeys("Hello World") sends keystrokes to the element correctly.click() method?WebElement button = driver.findElement(By.id("submitBtn"));
button.click();
String message = driver.findElement(By.id("msg")).getText();
System.out.println(message);click() doesclick() method simulates a user clicking the button, triggering any attached actions.WebElement btn = driver.findElement(By.id("loginBtn"));
driver.click(btn);click() method is not defined on the driver object to accept a WebElement parameter.click() directly on the WebElement, like btn.click();.click() and sendKeys() instead of directly modifying the DOM with JavaScript for testing user actions?click() and sendKeys() simulate real user actions, triggering browser events and UI changes.Pass the test and this pattern moves unknown