Skip to main content

_selenide

TL;DR - Show Me The Code

4-work-with-multiple-windows/code/selenide/src/test/java/com/elemental/selenium/MultipleWindowsTest.java
package com.elemental.selenium;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
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.url;

public class MultipleWindowsTest {
@BeforeEach
final void openAUT() {
open("https://the-internet.herokuapp.com/windows");
}

@Test
public void multipleWindows() {
webdriver().shouldHave(numberOfWindows(1));
$(".example a").shouldHave(text("Click Here")).click();

webdriver().shouldHave(numberOfWindows(2));
webdriver().shouldHave(title("The Internet"));
webdriver().shouldHave(url("https://the-internet.herokuapp.com/windows"));

switchTo().window(1);
// Waiting for the new window to have a title, then we consider it is loaded
webdriver().shouldHave(title("New Window"));
webdriver().shouldHave(url("https://the-internet.herokuapp.com/windows/new"));
}
}

Code Walkthrough

Importing Libraries, Setup and Teardown

First let's import our requisite classes

  • for annotations (e.g., org.junit.jupiter.api.Test),
  • opening the browser with Selenide (e.g., com.codeborne.selenide.Selenide.open),
  • matchers for assertions (e.g., com.codeborne.selenide.Condition.text)

In the following lines we start our class off with some setup.

The test

The multipleWindows test exercises new window functionality from an application. In this case, we'll be using the new window example from the-internet.

After loading the page we click the link with text "Click Here" which spawns a new window.

We then switch between windows based on their order (assuming that the first one is the originating window, and the second one is the new window). We round this test out by performing a simple check against the title of the page to make sure Selenide is focused on the correct window.

Details

  • A pre-condition is checked: the browser has only one window before we start any actions
  • Click the link "Click Here"
  • Check that now the browser has 2 windows
  • Check that the active window is still the first one (we check its title and url)
  • Switch to the "new" window (index 0 for the original window, index 1 for the new window)
  • Check that the active window is the new one (we check its title and url)

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.
4-work-with-multiple-windows/code/selenide/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips-selenide-work-with-windows</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.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.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.