Example on Operating Multiple Tabs or Browsers.

Note:You need to create a maven project and you need add Selenium 4.0.0-alpha-3 dependency.
Scenario

in another tab/window
Display the title of the newtours.demoaut site
Display the title of the facebook site

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class multiplebrowserexamples {

      public static void main(String[] args) {
     
        WebDriver driver;
        WebDriverManager.chromedriver().setup();
        driver=new ChromeDriver();         
        driver.get("https://www.facebook.com/");
        System.out.println("Face Book Web Page Title is :: "+driver.getTitle());
        //here you have to choose new open window is "Window or TAB"
        driver.switchTo().newWindow(WindowType.TAB);
        driver.get("http://newtours.demoaut.com/");
        System.out.println("Tour Demo Site Title is :: "+driver.getTitle());
     }
}

With the help of newWindow() for opening a new blank tab or window.
If you want to open new blank tab window you have to pass parameter as WindowType.TAB to newWindow().
If you want to open new blank window you have to pass parameter as WindowType.WINDOW to newWindow().

How to handle already opened window when you close newly opened window/Tab or how to  perform operations in both the windows/Tabs(opened windows and new windows/tabs).

Answer is simple , you should use getWindowHandle() method to perform operation on both the windows.

Below script is to print the title of the Facebook then open another site tourdemo in separate window and print title of the page then close the newly opened window and again print the title of the Facebook.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver; 
import io.github.bonigarcia.wdm.WebDriverManager;

public class multiplebrowserexamples {

      public static void main(String[] args) {     
            WebDriver driver;
            WebDriverManager.chromedriver().setup();
            driver=new ChromeDriver();         
            driver.get("https://www.facebook.com/");
            System.out.println("Face Book Web Page Title is :: "+driver.getTitle());
            String firstwindow=driver.getWindowHandle();
            // here you have to choose new open window is "Window or TAB"
            driver.switchTo().newWindow(WindowType. WINDOW);
            driver.get("http://newtours.demoaut.com/");
            System.out.println("Tour Demo Site Title is :: "+driver.getTitle());
            driver.close();
            driver.switchTo().window(firstwindow);
            System.out.println("Face Book Web Page Title is :: "+driver.getTitle());      
      }
}

Note : if you are trying to access first window without switching over, it will thrown an exception  “NoSuchWinowException”.

Comments