How can I handle late responses for confirmation messages in Selenium script?

129    Asked by Diyatomar in QA Testing , Asked on Jun 14, 2024

 I am currently engaged In a particular task that is related to testing a web-based application by using the Selenium WebDriver. During my automated testing scenario, I need to click a button labeled “submit” and then wait for a confirmation message to appear. However, the confirmation message sometimes takes a few seconds to load and my script fails immediately because it tries to verify the message very quickly. How can I modify my selenium script for handling this particular scenario? 

Answered by elonjigar

In the context of selenium, here is how you can handle this particular scenario by using the WebDriver in Java programming language:-


Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.support.ui.ExpectedConditions;
Import org.openqa.selenium.support.ui.WebDriverWait;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class SubmitButtonTest {
    Public static void main(String[] args) {
        // Assuming you have set up your WebDriver properly
        WebDriver driver = new ChromeDriver();
        // Open the web application URL
        Driver.get(http://example.com);
        // Find and click the “Submit” button
        WebElement submitButton = driver.findElement(By.id(“submit_button”));
        submitButton.click();
        // Use WebDriverWait to wait for the confirmation message to appear
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement confirmationMessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“confirmation_message”)));
        // Assert or perform actions based on the confirmation message
        If (confirmationMessage.isDisplayed()) {
            System.out.println(“Confirmation message appeared: “ + confirmationMessage.getText());
            // Add your assertions or further actions here
        } else {
            System.out.println(“Confirmation message did not appear within the expected time.”);
            // Handle this scenario accordingly
        }
        // Close the browser
        Driver.quit();
    }
}

In this coding :-

  • We first navigate the web application URL by using the driver.get().
  • Then we find and then click the submit button.
  • Next, we try to create a WebDriverwait Instance with a timeout of 10 seconds and wait for the confirmation message.
  • Once the confirmation message is shown, we can perform assertions or any further action based on it.
  • If the confirmation message doesn’t appear within the specified time-out then we can handle that scenario accordingly.

This approach would ensure that your selenium’s script waits for the confirmation message to appear before proceeding, thus avoiding intermittent failure because of the timing loss.



Your Answer

Answer (1)

Hey elonjigar,

Do you think it’s better to use ExpectedConditions.elementToBeClickable instead of visibilityOfElementLocated for clickable buttons?

snake game

3 Weeks

Interviews

Parent Categories