Tuesday 7 May 2019

Selenium + JUnit 5 maven dependency and sample test case

pom.xml

<!-- Allows the legacy unit tests code under JUnit 4 still to run -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency> 
    <groupId>org.junit.platform</groupId> 
    <artifactId>junit-platform-launcher</artifactId> 
    <version>1.5.0-M1</version> 
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

Sample test case:


import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {

    private WebDriver browser;
    
    @BeforeAll
    public static void init(){
        System.setProperty("webdriver.chrome.driver", "chromedriver");
    }
    
    @BeforeEach
    protected void setUp() throws Exception {
        browser = new ChromeDriver();
    }

    @Test
    public void browseGoogle() {
        browser.get("https://www.google.com.au");
    }
    
    @AfterEach
    protected void tearDown() throws Exception {
        browser.close();
    }

}

Selenium issue: java.lang.NoClassDefFoundError: okhttp3/WebSocketListener

A simple selenium test case yields the following error.

java.lang.NoClassDefFoundError: okhttp3/WebSocketListener
 at org.openqa.selenium.remote.internal.OkHttpClient.openSocket(OkHttpClient.java:75)
 at org.openqa.selenium.devtools.Connection.<init>(Connection.java:58)
 at org.openqa.selenium.chrome.ChromeDevToolsLocator.getChromeConnector(ChromeDevToolsLocator.java:81)
 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:196)
 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:176)
 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:131)
 at au.gov.nla.ums.keycloak.selenium.TroveTest.test(TroveTest.java:21)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 ....

It's caused by the version of selenium-java:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0-alpha-1</version>
</dependency>

Downgrading it to 3.141.59 solves the error.


<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

No tests found with test runner 'Junit 5'

When I execute a junit 5 test case, I get this error.









This is part of my pom.xml that is relevant.

<!-- Allows the legacy unit tests code under JUnit 4 still to run -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency> 
    <groupId>org.junit.platform</groupId> 
    <artifactId>junit-platform-launcher</artifactId> 
    <version>1.5.0-M1</version> 
    <scope>test</scope>
</dependency>

I try various suggestions on https://stackoverflow.com/questions/46717693/eclipse-no-tests-found-using-junit-5-caused-by-noclassdeffounderror, and the solution that works for me is adding another dependency of junit-jupiter-engine.

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>

It is this article that gives me the decisive hint.