Transcripted Summary

In this chapter, we are going to work in more than 1 window in the same session. We will also work in more than 1 tab in the same session.

After opening a new window or tab, we will work in both at the same time without creating a new driver.

Selenium 4 allows us to perform these steps with only one line of code.

In addition, we will minimize the window.



Our AUT is Automation Practice.

# SetUp Method to Open and Maximize First Window

In the IDE, the BeforeMethod will SetUp Chrome to maximize the window, load the AUT and get the page title.


  @BeforeMethod
  public void setUp() {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://automationpractice.com/index.php");
    System.out.println("Title: " + driver.getTitle());
  }

# Opening a New Tab or Window

The Test method will start with @Test public void testNewWindowTab().

The first statement is driver.switchTo().newWindow(). The newWindow method brings in WindowType.



This one line of code performs everything —

  • driver controls the browser.
  • switchTo() method switches focus between the windows and send a command to the window.
  • newWindow() is the built-in method that creates a new window or tab.
  • WindowType is an enum that lists each constant that defines a new data type.

Our data types will be WINDOW and TAB. Let's select TAB.

If I hover over newWindow, then we see the return type is WebDriver.



Therefore, we can assign this statement to WebDriver with any name like “newWindow”.


  @Test
  public void testNewWindowTab(){
    WebDriver newWindow = driver.switchTo().newWindow(WindowType.TAB);
  }

Now, let's run.

We have 2 tabs, and a blank page is open with automatic focus in the new tab.

The console should have “My Store” as the title because we got that title in our SetUp method. Let's check the console, and it shows “My Store”.



Now, let's update the test so it does not show a blank page.

Go back to the AUT, scroll to the bottom and we see “Specials” in the footer.



So, the test script will load this page.

I'm going to change TAB to WINDOW and load a new page by writing a newWindow.get(), then paste the URL.

We will print the page title by writing driver.getTitle().


  @Test
  public void testNewWindowTab(){
    WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
    newWindow.get("http://automationpractice.com/index.php?controller=prices-drop");
    System.out.println("Title: " + driver.getTitle());
  }

Now, let's run and see what shows up in our console for us, the titles. It should show both titles.

It opened 2 windows and we see “My Store” and “Prices drop - My Store”.



The console shows both titles.

# Working in Two Windows or Tabs

Now, let's transition to working in both windows or tabs.

That process requires us to switch back to the main window because the new window has default focus.

Let's write @Test public void testWorkingInBothWindowTabs().

Here's the steps:

  • Our @BeforeMethod configuration annotation will load the homepage.
  • Then we are going to automatically open and switch to the new window or tab.
  • We will work in the new window or tab.
  • Then we get the window ID handles.
  • Finally, we switch and work in the main window or tab.



At this point, let's go back to the AUT and go through the steps.

Our test will load this homepage, then open the _Sign in _page.

On the _Sign in _page, we are going to work in this tab by entering an email address, then clicking the CREATE AN ACCOUNT button.



Next is to switch back to the homepage and search for an item.

For the first step, let's write driver.switchTo().newWindow(). Then pass in “WindowType.TAB”.

On the next line I'm going to load the page and that page is the Sign in page. Let me go back to the sign in page and get that URL. Copy then paste.

Let's print the page by writing ("Title: " + driver.getTitle()).


    // Automatically Open & Switch To The New Window Or Tab
    driver.manage().window().maximize();
    driver.switchTo().newWindow(WindowType.TAB)
            .get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
    System.out.println("Title: " + driver.getTitle());

Now let's work in the new tab.

By inspecting the email address,** **we see it has "email_create" as the id value. Inspect the button, and it has an id value of "SubmitCreate".

Go back to our test script and write driver.findElement(By.id("")) and the id value is "email_create".

Then we do .sendKeys. The keys we are going to send is "Selenium4@TAU.com". I like that.

We will click the button by writing driver.findElement(By.id("")) and the value is "SubmitCreate".


    // Work In The New Window Or Tab
    driver.findElement(By.id("email_create")).sendKeys("Selenium4@TAU.com");
    driver.findElement(By.id("SubmitCreate")).click();

I think that's it.

At this point, let's get the window handles of each window.

The window handle is an alphanumeric ID assigned to each window.

We are going to start by writing Set<String> and the object reference will be allWindowTabss= driver.getWindowHandles()

The purpose of getWindowHandles is to return a set of all window handles that can be used to iterate over all open windows.

Our next method, after importing the Set class, will return an Iterator for the collection of both window IDs —Iterator<String> iterate = allWindowTabss.iterator(). We need to import Iterator.

Now we need a way to iterate over the collection of elements. And . next is a method for returning an element. String mainFirstWindow is the main window and the first element in the iteration.


    // Get The Window ID Handles
    Set<String> allWindowTabss= driver.getWindowHandles();
    Iterator<String> iterate = allWindowTabss.iterator();
    String mainFirstWindow = iterate.next();

The last step is to switch and work in the main window.

So let's write driver.switchTo().window(), then pass in “mainFirstWindow”.

Once we pass in “mainFirstWindow”, we can go back and inspect the search box. And we see it has an id value of "search_query_top.

Let's also inspect the search icon and it has no id value, but a value of "submit_search" for name.

Let's enter some data by writing driver.findElement(By.id("")) and the value is “search_query_top”,

And the keys we send will be “Shirt”.

We will click the search icon by writing driver.findElement(By.name("")) and the value was “submit_search”.

We will also print the page title by writing driver.getTitle().


    // Switch & Work In The Main Window Or Tab
    driver.switchTo().window(mainFirstWindow);
    driver.manage().window().maximize();
    driver.findElement(By.id("search_query_top")).sendKeys("Shirt");
    driver.findElement(By.name("submit_search")).click();
    System.out.println("Title: " + driver.getTitle());
  } 

Now let's run.

I like to print the title so we can check if our automation script switched to the new tab.

We see the main tab search for a shirt and the new tab is ready to create an account with a pre-populated email address that we entered.

So now the console should show all 3 titles.



Yes, it shows all 3 titles — My store”, “Login – My Store”, and “Search - My Store”.

# Minimizing Windows and Tabs

There’s one more concept for managing windows and tabs. We can minimize the window by changing maximize to minimize.



I'm going to copy this statement — driver.manage().window().minimize(); — and place it before every get statement.

Also, after we switch to the “mainFirstWindow”.


public class Chapter4_WindowManagement {
  WebDriver driver;

  @BeforeMethod
  public void setUp() {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().minimize();
    driver.get("http://automationpractice.com/index.php");
    System.out.println("Title: " + driver.getTitle());
  }

  @Test
  public void testNewWindowTab(){
    WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
    driver.manage().window().minimize();
    newWindow.get("http://automationpractice.com/index.php?controller=prices-drop");
    System.out.println("Title: " + driver.getTitle());
  }

  @Test
  public void testWorkingInBothWindowTabs(){
    // Automatically Open & Switch To The New Window Or Tab
    driver.manage().window().minimize();
    driver.switchTo().newWindow(WindowType.TAB)
            .get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
    System.out.println("Title: " + driver.getTitle());

    // Work In The New Window Or Tab
    driver.findElement(By.id("email_create")).sendKeys("Selenium4@TAU.com");
    driver.findElement(By.id("SubmitCreate")).click();

    // Get The Window ID Handles
    Set<String> allWindowTabss= driver.getWindowHandles();
    Iterator<String> iterate = allWindowTabss.iterator();
    String mainFirstWindow = iterate.next();

    // Switch & Work In The Main Window Or Tab
    driver.switchTo().window(mainFirstWindow);
    driver.manage().window().minimize();
    driver.findElement(By.id("search_query_top")).sendKeys("Shirt");
    driver.findElement(By.name("submit_search")).click();
    System.out.println("Title: " + driver.getTitle());
  }
}

Now let's run again.

This time we will not see our test scripts executing in the browser, but we still should see the page titles in the console.

Yes, we see all 3 titles in the console. It works. That's it for managing windows and managing tabs.

Next, we will get the element position with the getRect method.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR