如何在selenium webdriver中使用java在2个浏览器之间切换

时间:2016-12-29 05:42:17

标签: java selenium-webdriver browser

我正在使用java的selenium webdriver。我想打开浏览器在其中执行一些操作。然后打开另一个浏览器并在其中执行相同的操作,然后返回到第一个浏览器并执行一些操作。

如何在2个浏览器之间切换(不是在2个标签之间切换)?

这就是我所做的:

@BeforeTest
    public void beforeTest() throws BiffException, IOException,InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\MyProjects\\SeleniumTrials\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get(properties.getProperty("VAR_BASEURL"));
        driver.manage().window().maximize();
      WebDriver  tempDriver = new ChromeDriver();
        tempDriver.get(properties.getProperty("VAR_BASEURL"));
        tempDriver.manage().window().maximize();
}
@Test
    public void playTournament() throws InterruptedException, BiffException,IOException {
    int rowNumber = 1;
    int newRowNumber=2;
    WebElement login =driver.findElement(By.xpath(properties.getProperty("VAR_LOGIN"))); 
    login.click();
    Thread.sleep(1000);
    WebElement username = driver.findElement(By.xpath(properties.getProperty("VAR_USERNAME")));
    username.clear();
    username.sendKeys(getCellContent(0, rowNumber));
    Thread.sleep(1000);
    WebElement password = driver.findElement(By.xpath(properties.getProperty("VAR_PASSWORD")));
    password.clear();
    password.sendKeys(getCellContent(1, rowNumber));
    Thread.sleep(1000);
    WebElement continueButton = driver.findElement(By.xpath(properties.getProperty("VAR_CONTINUE")));
    continueButton.click();
    Thread.sleep(1000);

   WebElement login =tempDriver .findElement(By.xpath(properties.getProperty("VAR_LOGIN"))); 
   login.click();
   Thread.sleep(1000);
   WebElement username = tempDriver .findElement(By.xpath(properties.getProperty("VAR_USERNAME")));
   username.clear();
   username.sendKeys(getCellContent(0, rowNumber));
   Thread.sleep(1000);
   WebElement password = tempDriver .findElement(By.xpath(properties.getProperty("VAR_PASSWORD")));
   password.clear();
   password.sendKeys(getCellContent(1, rowNumber));
   Thread.sleep(1000);
   WebElement continueButton = tempDriver .findElement(By.xpath(properties.getProperty("VAR_CONTINUE")));
   continueButton.click();

3 个答案:

答案 0 :(得分:1)

我想这就是你要找的东西,

  1. 保留两个浏览器对象
  2. 定义一个在浏览器上执行一组操作的方法
  3. 首先使用第一个浏览器并再次使用第二个浏览器调用此方法
  4. 然后在第一个浏览器上执行更多操作

答案 1 :(得分:0)

当你这样做时

WebDriver driver = new ChromeDriver();
driver = new ChromeDriver();

您重新初始化driver实例,这意味着您丢失了第一个浏览器。您可以致电getWindowHandles()

查看
driver.getWindowHandles(); // will be 1, the last open browser

如果您想使用不同的浏览器使用临时驱动程序

WebDriver driver = new ChromeDriver();
WebDriver tempDriver = new ChromeDriver();

// do some stuff on tempDriver

tempDriver.close();

// continue working with the first driver

答案 2 :(得分:0)

我已经解决了在Typescript中浏览器驱动程序之间切换的问题,但您应该很容易理解该方法并在Java中实现它。

@ TDriver.ts

    import {browser, ProtractorBrowser} from 'protractor';

    export class TDriver {
      private static defaultBrowser: ProtractorBrowser = browser;
      private static activeBrowser;

      public async newDriver() {
        const secondDriver = TDriver.activeBrowser.forkNewDriverInstance();
        await this.setActiveBrowser(secondDriver);
        }

      public setActiveBrowser(driver: ProtractorBrowser) {
        TDriver.activeBrowser = driver;
      }

      public getActiveBrowser() {
        return TDriver.activeBrowser;
      }

      public setDefaultBrowserActive() {
        TDriver.activeBrowser = TDriver.defaultBrowser;
      }

    }

@ Test.ts

import {by} from 'protractor';
import {TDriver} from '../driver/TDriver';

export class Test {
  private driver = new TDriver().getActiveBrowser();

   this.driver.findElement(by.xpath("xPathSelector")).click(); //this click in first native driver instance

   await new TDriver().newDriver(); //create new driver and switch to it!

   this.driver.findElement(by.xpath("xPathSelector")).click(); //this click in the second driver instance

   await new TDriver().setDefaultBrowserActive(); //now we switch and use again native driver

   this.driver.findElement(by.xpath("xPathSelector")).click(); //this click in first native driver instance
}

这些方法使您可以在本机浏览器中执行测试,然后创建新实例并在第二个浏览器驱动程序中继续进行测试。在我的项目中有效。