Showing posts with label Selenium WebDriver. Show all posts
Showing posts with label Selenium WebDriver. Show all posts

Thursday, 25 June 2015

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-

i
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();
    }
}

Thursday, 26 June 2014

Create a Label in gmail using TestNG framework

In this example we will create a Label in Gmail. Using TestNG framework in Selenium.
package Webdriverprgs;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class createlabel {
    public WebDriver driver;
    public WebElement element;
    public Robot robot;
    public Actions actions;
  @Test
  public void f() throws InterruptedException, Exception {
      driver.findElement(By.id("Email")).sendKeys("jan30selenium");
      driver.findElement(By.id("Passwd")).sendKeys("selenium1");
      driver.findElement(By.id("signIn")).click();
      Actions act=new Actions(driver);

      //Thread.sleep(20000);
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      element=driver.findElement(By.xpath("html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]"));
      actions=new Actions(driver);
      actions.moveToElement(element).build().perform();
      driver.findElement(By.xpath(".//*[@id=':j2']/span[2]/div")).click();

      WebElement element = driver.findElement(By.linkText("Create new label"));
      ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
      Thread.sleep(500);
      driver.findElement(By.linkText("Create new label")).click();
      driver.findElement(By.cssSelector("input.xx")).sendKeys("Aditya");
      driver.findElement(By.name("ok")).click();
      
   
  }
  @BeforeTest
  public void beforeTest() {
      driver=new FirefoxDriver();
      driver.get("http://gmail.com");
      driver.manage().window().maximize();
  }

  @AfterTest
  public void afterTest() {
  }

} 
Copy this code your eclipse IDE and import the required libraries and go for test. Enjoy! Keep posting your comments.