JUnit 5 extension for Selenium

In order to simplify the use of Selenium WebDriver in JUnit 5, the open source JUnit 5 extension called selenium-jupiter can be used. This extension has been built using the dependency injection capability provided by the extension model of JUnit 5. Thanks to this feature, different types objects can be injected in JUnit 5 in @Test methods as parameters. Concretely, selenium-jupiter allows to inject subtypes of the WebDriver interface (for example, ChromeDriver, FirefoxDriver, and so on).

Using selenium-jupiter is very easy. First, we need to import the dependency in our project (typically as test dependency). In Maven, it is done as follows:

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>selenium-jupiter</artifactId>
<version>${selenium-jupiter.version}</version>
<scope>test</scope>
</dependency>

selenium-jupiter depends on several libraries, which are added in our project as transitive dependencies, namely:

  • Selenium-java (org.seleniumhq.selenium:selenium-java): Java library for Selenium WebDriver.
  • WebDriverManager (io.github.bonigarcia:webdrivermanager): Java library for automatic Selenium WebDriver binaries management in runtime for Java (https://github.com/bonigarcia/webdrivermanager).
  • Appium (io.appium:java-client): Java client for Appium, testing framework that extends Selenium to automate testing of native, hybrid, and mobile web apps (http://appium.io/).

Once selenium-jupiter is included in our project, we need to declare selenium-jupiter extension in our JUnit 5 test, simply annotating it with @ExtendWith(SeleniumExtension.class). Then, we need to include one or more parameters in our @Test methods whose types implement the WebDriver interface, and selenium-jupiter control the lifecycle of the WebDriver object internally. He WebDriver subtypes supported by selenium-jupiter are the following:

  • ChromeDriver: This is used to control Google Chrome browser.
  • FirefoxDriver: This is used to control Firefox browser.
  • EdgeDriver: This is used to control Microsoft Edge browser.
  • OperaDriver: This is used to control Opera browser.
  • SafariDriver: This is used to control Apple Safari browser (only possible in OSX El Capitan or greater).
  • HtmlUnitDriver: This is used to control HtmlUnit (headless browser, that is, a browser without GUI).
  • PhantomJSDriver: This is used to control PhantomJS (another headless browser).
  • InternetExplorerDriver: This is used to control Microsoft Internet Explorer. Although this browser is supported, Internet Explorer is deprecated (in favor of Edge) and its use is highly discouraged.
  • RemoteWebDriver: This is used to control remote browsers (Selenium Grid).
  • AppiumDriver: This is used to control mobile devices (Android and iOS).

Consider the following class, which uses selenium-jupiter , that is, declaring the Selenium extension using @ExtendWith(SeleniumExtension.class). This example defines three tests, which are going be executed using local browsers. First one (named testWithChrome) uses Chrome as browsers. To that aim, and thanks to the dependency injection feature of selenium-jupiter, the method simply needs to declare a method argument using the type ChromeDriver. Then, in the body of the test, the WebDriver API is invoked in that object. Note that this test simple opens a web page and asserts that the title is as expected. Next, test (testWithFirefoxAndOpera) is similar, but this time using two different browsers at the same time: Firefox (using an instance of FirefoxDriver) and Opera (using an instance of OperaDriver). The third and last test (testWithHeadlessBrowsers) declares and uses two headless browsers (HtmlUnit and PhantomJS):

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import
org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

@ExtendWith(SeleniumExtension.class)
public class LocalWebDriverTest {

@Test
public void testWithChrome(ChromeDriver chrome) {
chrome.get("https://bonigarcia.github.io/selenium-jupiter/");
assertTrue(chrome.getTitle().startsWith("selenium-jupiter"));
}

@Test
public void testWithFirefoxAndOpera(FirefoxDriver firefox,
OperaDriver opera) {
firefox.get("http://www.seleniumhq.org/");
opera.get("http://junit.org/junit5/");
assertTrue(firefox.getTitle().startsWith("Selenium"));
assertTrue(opera.getTitle().equals("JUnit 5"));
}

@Test
public void testWithHeadlessBrowsers(HtmlUnitDriver htmlUnit,
PhantomJSDriver phantomjs) {
htmlUnit.get("https://bonigarcia.github.io/selenium-jupiter/");
phantomjs.get("https://bonigarcia.github.io/selenium-jupiter/");
assertTrue(htmlUnit.getTitle().contains("JUnit 5 extension"));
assertNotNull(phantomjs.getPageSource());
}

}
In order to execute properly this test class, the required browsers (Chrome, Firefox, and Opera) should be installed beforehand running it. On the other hand, the headless browsers (HtmlUnit and PhantomJS) are consumed as Java dependencies, and so there is no need to install them manually.

Let’s see another example, this time using remote browsers (that is, Selenium Grid). Again, this class uses the selenium-jupiter extension. The test (testWithRemoteChrome) declares a single parameter called remoteChrome, of type RemoteWedbrider. This argument is annotated with @DriverUrl and @DriverCapabilities, specifying the Selenium Server (or Hub) URL and the required capabilities respectively. Regarding the capabilities, we are configuring to use a Chrome browser version 59:

To run this test properly, a Selenium Server should up and running in the localhost, and a node (Chrome 59) needs to be registered in the Hub.
package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertTrue;

import
org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.remote.RemoteWebDriver;

@ExtendWith(SeleniumExtension.class)
public class RemoteWebDriverTest {

@Test
void testWithRemoteChrome(
@DriverUrl("http://localhost:4444/wd/hub")
@DriverCapabilities(capability = {
@Capability(name = "browserName", value ="chrome"),
@Capability(name = "version", value = "59") })
RemoteWebDriver remoteChrome)
throws InterruptedException {
remoteChrome.get("https://bonigarcia.github.io/selenium-
jupiter/");
assertTrue(remoteChrome.getTitle().contains("JUnit 5
extension"));
}

}

In the last example of this section, we use AppiumDriver. Concretely, we set up as capabilities the use of a Chrome browser in an Android emulated device (@DriverCapabilities). Again, this emulator needs to be up and running in the machine running the test beforehand:

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertTrue;

import
org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;

@ExtendWith(SeleniumExtension.class)
public class AppiumTest {

@DriverCapabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
{
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("deviceName", "Android");
}

@Test
void testWithAndroid(AppiumDriver<WebElement> android) {
String context = android.getContext();
android.context("NATIVE_APP");
android.findElement(By.id("com.android.chrome:id/terms_accept"))
.click();
android.findElement(By.id("com.android.chrome:id/negative_button"))
.click();
android.context(context);
android.get("https://bonigarcia.github.io/selenium-jupiter/");
assertTrue(android.getTitle().contains("JUnit 5 extension"));
}

}
For further examples of selenium-jupiter, visit https://bonigarcia.github.io/selenium-jupiter/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.139.224