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

问题描述 投票:0回答:3

我正在使用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();
java selenium-webdriver browser
3个回答
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

1
投票

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

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

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
}

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

© www.soinside.com 2019 - 2024. All rights reserved.