Delivered by FeedBurner

Selenium WebDriver wait for title with example

WebDriver has many Canned Expected Conditions by which we can force webdriver to wait explicitly. However Implicit wait is more practical than explicit wait in WebDriver software testing tool. But in some special cases where implicit wait is not able to handle your scenario then in that case you need to use explicit waits in your software automation test cases. You can view different posts on explicit waits where I have described WebDriver's different Canned Expected conditions with examples.

Create selenium webdriver software automation data driven framework from scratch @This Page.

If you have a scenario where you need to wait for title then you can use titleContains(java.lang.String title) with webdriver wait. You need to provide some part of your expected software web application page title with this condition as bellow.

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains(": MyTest"));

In above syntax, ": MyTest" is my web page's expected title and 15 seconds is max waiting time to appear title on web page. If title will not appears within 15 seconds due to the any reason then your test case will fails with timeout error.

First run bellow given test case in your eclipse and then try same test case for your own software application.

Copy bellow given @Test method part of wait for title 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).

 public void test () 
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
  driver.findElement(By.xpath("//a[contains(text(),'Click Here')]")).click();

  //Wait for page title
  WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.titleContains(": MyTest"));

  //Get and store page title in to variable
  String title = driver.getTitle();

In above example, when webdriver will click on Click Here link, another page will open. During page navigation, webdriver will wait for expected page title of software web application. 

1 comment:

  1. Getting "The method textToBePresentInElementLocated(By, String) is undefined for the type ExpectedConditions" error while trying to implement Explicit Wait (Selenium WebDriver Tutorials - Basic Action Commands And Operations With Examples => 7. Applying Explicit wait in webdriver with WebDriver canned conditions.)

    textToBePresentInElementLocated is not available and fixes available are
    1. textToBePresentInElement
    2. textToBePresentInElementValue

    Is there any libraries missing or mismatch with versions ?