Tuesday, October 25, 2016

Opening url in new Window?


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 ?
  1. 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.
  2. Launch a new url in new tab of existing WebDriver session
    • Why to start a new WebDriver instance when you do use already existing one?
    • This will also share existing session with server
    • Approach: - 
      • Execute a JavaScript code it will open a new url in new tab/window but in existing WebDriver instance
      • 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
      WebDriver driver = new FirefoxDriver();
      WebDriver driver = new FirefoxDriver();
      driver.get("http://google.com");
      // copy current window handle for future reference
      String autWindowHandle = driver.getWindowHandle();
      JavascriptExecutor executor = (JavascriptExecutor) driver;
      executor.executeScript("window.open('http://www.gmail.com','_blank')");
      
      // Switch driver to gmail and do your work
      for (String handle : driver.getWindowHandles()) {
       driver.switchTo().window(handle);
      }
      // do operations and read data.
      // String otp = driver.findElement(By.id("some id")).getText();
      
      //Now close gmail and switch back to AUT site
      driver.close();
      driver.switchTo().window(autWindowHandle);
      //Continue automation testing
      
I hope this will be useful to solve your problem.

2 comments: