Why interaction methods simulate user behavior in Selenium Java - Automation Benefits in Action
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; public class UserBehaviorSimulationTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); try { // Open the test page driver.get("https://example.com/testpage"); // Wait until the button is clickable WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn"))); // Click the button to simulate user behavior submitButton.click(); // Wait for confirmation message to appear WebElement confirmationMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmationMsg"))); // Assert the confirmation message is displayed if (confirmationMsg.isDisplayed()) { System.out.println("Test Passed: Confirmation message is displayed after clicking the button."); } else { System.out.println("Test Failed: Confirmation message is not displayed."); } } finally { driver.quit(); } } }
This test script uses Selenium WebDriver with Java to automate the manual test case.
First, it opens the web page where the button is located.
It uses an explicit wait to ensure the button is clickable before clicking it. This simulates a real user waiting for the button to be ready.
Then, it clicks the button using Selenium's click() method, which simulates the actual user interaction.
After clicking, it waits for the confirmation message to appear, verifying that the page responded as expected.
The assertion checks if the confirmation message is visible, confirming that the interaction method simulates user behavior correctly.
Finally, the browser is closed to clean up.
Now add data-driven testing with 3 different buttons having ids 'submitBtn1', 'submitBtn2', 'submitBtn3' and verify each confirmation message.