Saturday, July 5, 2014

How to verify the text present in my web page.

There could be two ways to verify text present in web page or not-
1) Find that element where that text is populating and use getText() method then verify.
2)1st Get the page source using getPageSource() method then verify.

Ex-

import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class VerifyTextPresentInWebPage {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.goibibo.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       
        //verify 'International Flights' text is there in the web page or not
        String expText = "International Flights";
       
        //first way to verify by using locator and getText() method
        String actText = driver.findElement(By.xpath("//a[@class='hm_inactive']")).getText();
        if(actText.contains(expText)){
            System.out.println("1) Expected text '"+expText+"' present in the web page.");
        }else{
            System.out.println("1) Expected text '"+expText+"' is not present in the web page.");
        }
       
        //second way to verify by using getPageSource method
        String pageSource = driver.getPageSource();
        if(pageSource.contains(expText)){
            System.out.println("2) Expected text '"+expText+"' present in the web page.");
        }else{
            System.out.println("2) Expected text '"+expText+"' is not present in the web page.");
        }
        driver.close();
    }
}


No comments:

Post a Comment