How to open a new window with a new URL while browser automation session is ongoing?
Have you encountered situation where you are automating a website and same time you need to open other browser window to do something like read OTP or read E-Mail and come back to the AUT window and proceed automation...
I am sure If you are working in web automation testing you might have encountered this.
What solution we have ?
- Launch a new WebDriver instance while one is having AUT loaded.
- For example below code :
// Initial WebDriver instance to perform Automation Testing on AUT // (if Selenium 3.0 then set webdriver.gecko.driver path) WebDriver driver = new FirefoxDriver(); driver.get("http://myAUT.com"); // TODO: perform some automation stuff // .... // Now I want to open gmail in new window without disturbing // myAUT.com site // So launch a new WebDriver instance WebDriver gmailBrowser = new FirefoxDriver(); gmailBrowser.get("http://www.gmail.com"); // Perform operations like read mail data here // String otp = gmailDriver.findElement(By.xpath("some locator")) // .getText(); // Now close browser and go back to myAUT.com gmailBrowser.close(); // now type otp in myAUT.com driver.findElement(By.id("some id")).sendKeys(otp);
- But is this really good approach? NO!!! absolutely not!
- Because it's more confusing to work on two webdriver instance at a time.
- Both WebDriver instance do not share common session with server.
- Why to start a new WebDriver instance when you do use already existing one?
- This will also share existing session with server
- Approach: -
- Switch driver to point newly opened driver and do some task
- Once you are done close newly opened tab/window and switch back to AUT browser window
- Below is the sample script