问题
I'm working on selenium webdriver with java. I want to open a browser perform some actions in it. Then open another browser and do the same actions in it, then go back to first browser and perform some actions.
How can i switch between 2 browsers (not the switching between 2 tabs)?
This is what i have done:
@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();
回答1:
When you do
WebDriver driver = new ChromeDriver();
driver = new ChromeDriver();
You reinitialize the driver instance, witch means you loose the first browser. You can see it by calling getWindowHandles()
driver.getWindowHandles(); // will be 1, the last open browser
If you want to different browsers use temporary driver
WebDriver driver = new ChromeDriver();
WebDriver tempDriver = new ChromeDriver();
// do some stuff on tempDriver
tempDriver.close();
// continue working with the first driver
回答2:
I guess this is what you are looking for,
- keep two browser objects
- define a method which does a set of operations on a browser
- call this method first using the first browser and again using the second browser
- then perform some more action on the first browser
回答3:
I have solved switching between browser drivers in a Typescript but it should be easy for you to understand the methodology and implement it in the 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
}
Those methods enables you to perform test in native browser then create new instance and continue test in second browser driver. Works in my project.
来源:https://stackoverflow.com/questions/41373746/how-to-switch-between-2-browsers-in-selenium-webdriver-with-java