Tuesday, 4 August 2015

How to open and close tab in selenium webdriver using java?.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class OpenandClosenewtab {

    public static void main(String[] a) throws InterruptedException {
        // Initialize driver 
        WebDriver dr = new FirefoxDriver();
        //Maximize browser window  
        dr.manage().window().maximize();
        //Go to URL 
        dr.get("http://www.google.com");
        //Set  timeout 
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // Open new tab 
dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
        //Go to URL 
        dr.get("http://www.gmail.com");
        //Set new tab timeout 
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // Do some operation         
        dr.findElement(By.id("gmail-sign-in")).click();
        dr.findElement(By.id("Email")).sendKeys("WebDriver");
        dr.findElement(By.id("Passwd")).sendKeys("WebDriver");
        dr.findElement(By.id("signIn")).click();
        Thread.sleep(2000);

        // Close new tab 
dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");

        // Switch first tab 
        dr.switchTo().defaultContent();
        Thread.sleep(2000);
        // Write search String 
        dr.findElement(By.id("gbqfq")).sendKeys("WebDriver");
        // Click on Search button         
        dr.findElement(By.id("gbqfb")).click();
        Thread.sleep(2000);

        // Browser close   
        dr.close();
    }

}

No comments:

Post a Comment