SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

WebDriver - wait for text to be present with example using explicit wait

Some times you need to wait for textwait for elementwait for alert before performing actions in your regular software web application test cases of selenium webdriver. Generally we need to use this types of conditions in our test case when software web application page is navigating from one page to other page. In my previous post, we have seen how to write and use wait for element syntax in our test case. Now let we see how can we force webdriver to wait if expected text is not displayed on targeted element of software web application page.

As you know, we can use "waitForText" or "waitForTextPresent" commands in selenium IDE software testing tool to wait till targeted text appears on page of software web application or element. In webdriver, we can use syntax as shown bellow.

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));

Look in to above syntax. Here i have used textToBePresentInElementLocated(By, String) to wait till expected text appears on targeted element. So here what WebDriver will do is it will check for expected text on targeted element on every 500 milliseconds and will execute next step as soon as expected text appears on targeted element.

Run bellow given practical example.in eclipse and see how it works.
Copy bellow given @Test method part of wait for text example and replace it with the @Test method part of example given on this page(Note : @Test method is marked with pink color in that linked page).

@Test
 public void test () 
  {  
   driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("alpesh");
   WebDriverWait wait = new WebDriverWait(driver, 15);
   wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton1")));
   driver.findElement(By.cssSelector("#submitButton")).click();
   wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));
  }

In above example, When WebDriver clicks on #submitButton page of software web application will be refreshed and then WebDriver will wait for text = "Time left: 7 seconds" on targeted element.

4 comments:

  1. This code timeout at:

    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton1")));

    ReplyDelete
  2. I am not very sure of teh last line used in code.
    After clicking on submit , the Page refreshes, and "Time Left:" keeps on reducing to zero. moreover i get a no text found exception for same.

    Plz reply..

    ReplyDelete
  3. Does the 15seconds are total for the 2 expectedConditions waiting time?
    -> wait.until(ExpectedConditions.....
    Or each take 15seconds?

    ReplyDelete
    Replies
    1. each explicit wait will take it's own time. But it is max 15 seconds here. Means if condition match within 7 seconds then execution control will move to execute next statement.

      Delete