Saturday, 11 February 2017

VerifyTextPresentInWebPage(Selenium WebDriver)

package seleniumconcepts;

import java.util.concurrent.TimeUnit;

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

public class VerifyTextPresentInWebPage {

/**
* Author by Rajat Tiwari
*/
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