Showing posts with label New Tab. Show all posts
Showing posts with label New Tab. Show all posts

Saturday, June 7, 2014

How to switch to new tab and perform operation there then again switch back to previous tab and perform oepration in previous tab.

import java.awt.AWTException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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;

public class NewTab {
    public static void main(String[] args) throws AWTException, InterruptedException {
     
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=dGKEU8HNCqeOiAenwoDgAQ");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//a[text()='Gmail']"));
        act.moveToElement(link).contextClick().sendKeys("T").perform(); //open link in new tab
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();//switch to new tab by pressing control+tab
        Thread.sleep(3000); // simply wait for 3sec to see new tab open or not
        String main = driver.getWindowHandle();
        driver.switchTo().window(main);//switch control to new tab
        //act.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform(); //press control+f4 to close tab
        driver.findElement(By.xpath("//input[@name='Email']")).sendKeys("hello"); //confirm that control is in new tab or not, just send few words in Email box
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.SHIFT,Keys.TAB)).perform(); //open previous tab
        driver.switchTo().window(main); //switch control to previous tab
        driver.findElement(By.name("q")).sendKeys("i am in previous tab"); //confirm that control is in previous tab or not, just send few words in google search box
       
    }
}

Tuesday, May 27, 2014

How to move control to new tab or how to do operation in new tab ?


import java.awt.AWTException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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;

public class NewTab {
    public static void main(String[] args) throws AWTException, InterruptedException {
      
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=dGKEU8HNCqeOiAenwoDgAQ");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//a[text()='Gmail']"));
        act.moveToElement(link).contextClick().sendKeys("T").perform(); //open link in new tab
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();//switch to new tab by pressing control+tab
        Thread.sleep(3000); // simply wait for 3sec to see new tab open or not
        String main = driver.getWindowHandle();
        driver.switchTo().window(main);//switch control to new tab
        //act.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform(); //press control+f4 to close tab
        driver.findElement(By.xpath("//input[@name='Email']")).sendKeys("hello"); //click on a 'Show all posts' link of main tab to confirm we are in main tab
    }
}

Thursday, May 22, 2014

How to close new tab or How to open new tab and close it ?

Note- open link in new tab using sendKeys() method by passing arg as 'T'. Then press CONTROL+TAB key to go to new tab. Then press CONTROL+F4 to close new tab.

Please refer the below example-

import java.awt.AWTException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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;

public class NewTab {
    public static void main(String[] args) throws AWTException, InterruptedException {
       
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://selenium-makeiteasy.blogspot.in/search/label/Interview%20Ques-Ans%20for%20Automation%20Tester");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//a[text()='How to improve performance of the script in selenium webdriver?']"));
        act.moveToElement(link).contextClick().sendKeys("T").perform(); //open link in new tab
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();//switch to new tab by pressing control+tab
        Thread.sleep(3000); // simply wait for 3sec to see new tab open or not
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform(); //press control+f4 to close tab
        driver.findElement(By.xpath("//a[text()='Show all posts']")).click(); //click on a 'Show all posts' link of main tab to confirm we are in main tab
    }
}

Wednesday, May 21, 2014

How to open a link in new tab.



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class NewTab {
    public static void main(String[] args) {
        DesiredCapabilities capability = DesiredCapabilities.firefox();
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.id("gb_70"));
        act.moveToElement(link).contextClick().sendKeys("T").perform();
       
    }
}

Monday, May 12, 2014

How to open a link in new tab.



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class NewTab {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.id("gb_70"));
        act.moveToElement(link).contextClick().sendKeys("T").perform();
    }
}