Why advanced skills handle complex scenarios 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 org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class MultiStepFormTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com/multi-step-form"); } @Test public void testCompleteMultiStepForm() { // Step 1: Fill first step wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("firstName"))).sendKeys("John"); driver.findElement(By.id("lastName")).sendKeys("Doe"); driver.findElement(By.id("nextBtn")).click(); // Step 2: Wait for dynamic content wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicField"))); driver.findElement(By.id("dynamicField")).sendKeys("Dynamic Data"); driver.findElement(By.id("nextBtn")).click(); // Step 3: Fill third step wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))).sendKeys("john.doe@example.com"); driver.findElement(By.id("submitBtn")).click(); // Verify confirmation message WebElement confirmation = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmationMessage"))); Assert.assertEquals(confirmation.getText(), "Form submitted successfully"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with Java and TestNG for assertions.
We start by opening the multi-step form page in the @BeforeClass setup.
Each step waits explicitly for the required elements to be visible before interacting. This avoids timing issues with dynamic content.
We fill fields by locating them with By.id, which is clear and stable.
We click the 'Next' or 'Submit' buttons to move through the form steps.
After submission, we wait for the confirmation message and assert its text matches the expected success message.
The @AfterClass method closes the browser to clean up.
This approach shows advanced skills by handling dynamic waits, using good locators, and structuring the test clearly.
Now add data-driven testing with 3 different sets of form input data
Practice
Solution
Step 1: Understand dynamic web elements
Complex apps often have elements that load or change after some delay.Step 2: Importance of handling delays and exceptions
Advanced skills like waits and exception handling ensure tests wait for elements and handle errors gracefully.Final Answer:
They help handle dynamic elements and unexpected delays reliably. -> Option AQuick Check:
Advanced skills = handle dynamic elements [OK]
- Assuming tests should skip validations
- Ignoring errors instead of handling them
- Believing assertions are unnecessary
Solution
Step 1: Recall correct WebDriverWait syntax
WebDriverWait requires driver and timeout, then calls until with ExpectedConditions.Step 2: Check each option's syntax
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit"))); matches correct syntax; others have wrong method calls or missing driver.Final Answer:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit"))); -> Option AQuick Check:
Correct WebDriverWait syntax = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit"))); [OK]
- Using driver.wait() which does not exist
- Missing driver parameter in WebDriverWait constructor
- Incorrect method names like waitUntilClickable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login")));
System.out.println(button.isDisplayed());Solution
Step 1: Understand WebDriverWait with ExpectedConditions
wait.until waits up to 5 seconds for element visibility or throws TimeoutException if not found.Step 2: Analyze System.out.println output
If element is visible, isDisplayed() returns true; if not found in time, exception occurs before print.Final Answer:
true if element with id 'login' is visible within 5 seconds, else TimeoutException -> Option CQuick Check:
Wait returns element or throws exception [OK]
- Thinking wait returns false instead of exception
- Assuming isDisplayed() runs if element not found
- Confusing compilation errors with runtime exceptions
driver.findElement(By.id("submitBtn")).click();
Thread.sleep(5000);
Solution
Step 1: Analyze use of Thread.sleep
Thread.sleep pauses test for fixed time, which is inefficient and unreliable for dynamic waits.Step 2: Suggest better approach
Explicit waits like WebDriverWait wait for conditions dynamically, improving test stability.Final Answer:
Using Thread.sleep causes unnecessary fixed wait; better to use explicit wait. -> Option BQuick Check:
Use explicit waits, not Thread.sleep [OK]
- Thinking Thread.sleep is best for waiting
- Confusing findElement and findElements usage
- Believing click() is invalid method
Solution
Step 1: Handle dynamic elements with explicit waits
Explicit waits ensure each element is visible before interacting, avoiding errors.Step 2: Use try-catch to handle exceptions without stopping test
Try-catch blocks catch errors per element, allowing test to continue and log issues.Step 3: Apply soft assertions to log failures without halting
Soft assertions record failures but let the test finish, improving test reporting.Final Answer:
Use explicit waits for visibility, try-catch blocks for exceptions, and soft assertions for logging failures. -> Option DQuick Check:
Combine waits, exception handling, soft assertions [OK]
- Using fixed waits causing flaky tests
- Not handling exceptions causing test stops
- Using hard assertions stopping tests early
Ready to prove it?
Pass the test and this pattern moves unknown
