_selenide
TL;DR - Show Me The Code
package com.elemental.selenium;
import com.codeborne.selenide.junit5.BrowserPerTestStrategyExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.switchTo;
import static com.codeborne.selenide.Selenide.webdriver;
import static com.codeborne.selenide.WebDriverConditions.numberOfWindows;
import static com.codeborne.selenide.WebDriverConditions.title;
import static com.codeborne.selenide.WebDriverConditions.urlContaining;
import static org.openqa.selenium.WindowType.TAB;
import static org.openqa.selenium.WindowType.WINDOW;
@ExtendWith(BrowserPerTestStrategyExtension.class)
public class NewWindowNewTabTest {
@Test
public void newWindow() {
open("https://the-internet.herokuapp.com/windows");
switchTo().newWindow(WINDOW);
open("https://the-internet.herokuapp.com/typos");
webdriver().shouldHave(numberOfWindows(2));
}
@Test
public void newTab() {
open("https://the-internet.herokuapp.com/windows");
webdriver().shouldHave(urlContaining("/windows"));
webdriver().shouldHave(title("The Internet"));
switchTo().newWindow(TAB);
open("https://the-internet.herokuapp.com/typos");
webdriver().shouldHave(numberOfWindows(2));
webdriver().shouldHave(urlContaining("/typos"));
webdriver().shouldHave(title("The Internet"));
}
}
Code Walkthrough
Importing Libraries
First, we pull in our requisite classes:
- test runner (e.g.,
org.junit.jupiter.api.BeforeEach
, etc.), - opening the browser with Selenide (e.g.,
com.codeborne.selenide.Selenide.open
), - matchers for assertions (e.g.,
org.assertj.core.api.Assertions.assertThat
)
Setup and Teardown
After specifying the namespace and the class (e.g., public class NewWindowNewTabTest
),
we add BrowserPerTestStrategyExtension
- this is Selenide extension for JUnit5 that closes the browser after every test.
It's not always needed, but in this specific class every test opens few windows or tabs, so it's easier to close the whole browser with all tabs and windows.
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 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 switchTo().newWindow(WindowType.TAB)
line. Which opens a new tab and switches to it.
Additional checks
Also, in newTab
test we showed how you can check some details of current window/tab.
Selenide has a number of built-in checks for URL, title etc.
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-selenide-open-new-window</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>7.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Finally, we can run the test by executing mvn test
from the command-line.