Thursday, 7 January 2016

How to Open link in new tab and close tab in selenium

package abcpack;

import static org.testng.Assert.assertEquals;
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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class NewTab {
    WebDriver d;
    @Test
    public void testNewTab() throws Exception
    {
        //Load the web page
        d.get("https://www.google.co.in/?gws_rd=ssl");
        assertEquals("Google",d.getTitle());
        Actions a=new Actions(d);
        WebElement link=d.findElement(By.linkText("Gmail"));
        a.moveToElement(link).keyDown(Keys.CONTROL).click(link).build().perform();
        Thread.sleep(5000);
    }
    @BeforeMethod
    public void setUp()
    {
        //Launch browser
        d=new FirefoxDriver();
        //System.setProperty("webdriver.ie.driver","F:\\Selenium_Scripts_Aug15\\Lib\\IEDriverServer.exe");
        //d=new InternetExplorerDriver();
        //System.setProperty("webdriver.chrome.driver","F:\\Selenium_Scripts_Aug15\\Lib\\chromedriver.exe");
        //d=new ChromeDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @AfterMethod
    public void tearDown()
    {
        //Close browser
        d.quit();
    }
}
++++++++++++++
package All_Types_Run;

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 Close_Or_Open_New_Tab {
    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
    }
}

No comments:

Post a Comment