_java
TL;DR - Show Me The Code
package com.elemental.selenium;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NewWindowNewTabTest {
WebDriver driver;
@BeforeEach
public void beforeEach() {
driver = new FirefoxDriver();
}
@AfterEach
public void afterEach() {
if (driver != null) {
driver.quit();
}
}
@Test
public void newWindow() throws InterruptedException {
driver.get("https://the-internet.herokuapp.com/windows");
driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("https://the-internet.herokuapp.com/typos");
Assertions.assertEquals(2, driver.getWindowHandles().toArray().length);
}
@Test
public void newTab() throws InterruptedException {
driver.get("https://the-internet.herokuapp.com/windows");
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://the-internet.herokuapp.com/typos");
Assertions.assertEquals(2, driver.getWindowHandles().toArray().length);
}
}
Code Walkthrough
Importing Libraries
First let's import our requisite classes (for annotations (e.g., org.junit.After
, etc.), driving the
browser with Selenium (e.g., org.openqa.selenium.WebDriver
, etc.), and matchers for our assertions
(e.g., org.junit.jupiter.api.Assertions
, etc.)), and start our class with some setup and teardown
methods.
Setup and Teardown
After specifying the namespace and the class (e.g., public class NewWindowNewTabTest
), we are setting up and
tearing down the browser instance. The beforeEach
method, will execute before
each test. In it, we are launching a new instance of Firefox with Selenium and storing it in a class variable that
we'll use throughout all tests. After our test executes the second method, afterEach
, will execute. This calls
driver.quit()
which ends the session by closing the browser instance.
Open New Window
newWindow
shows how to open a new window, switch to it, and assert the number of window handles open. They key
to this is the driver.switchTo().newWindow(WindowType.WINDOW)
line. Which opens a new window and switches to it.
Open New Tab
newTab
shows how to open a new tab, switch to it, and assert the number of window handles open. They key to this
is the driver.switchTo().newWindow(WindowType.TAB)
line. Which opens a new tab and switches to it.
Executing the Test
Before executing the test, we need to make sure the required dependencies are declared on the pom.xml
file.
Toggle to see the pom.xml
file.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Finally, we can run the test by executing mvn test
from the command-line.