33. WebDriver Explicit Wait : Text to be present in selenium webdriver



Sometimes you need to wait for textwait for element, wait 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.

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.

package Others;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class webdriver_explicit {
                       
            @BeforeClass
            public void beforeClass() {
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
                       
            }
           
            public WebDriver driver;
            @Test
            public void testIsDiplayed_Enabled() throws Exception {
                       
                        driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("alpesh");
                        WebDriverWait wait = new WebDriverWait(driver, 15);
                        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
                        driver.findElement(By.cssSelector("#submitButton")).click();
                        wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));
           
            }

            @AfterClass
            public void afterClass() throws Exception {
                        driver.quit();
            }

}


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.

No comments:

Post a Comment