我正在使用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();
答案 0 :(得分:1)
我想这就是你要找的东西,
答案 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)
@ 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
}
这些方法使您可以在本机浏览器中执行测试,然后创建新实例并在第二个浏览器驱动程序中继续进行测试。在我的项目中有效。