nagard
nagard

Reputation: 101

Unable to run Selenium test from jar file

UPDATE:

Thanks to @udalmik I verified that the java file from src/test/java is not included in jar file. I tried to add maven compiler plugin but still no luck:

...
<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <includes>
                            <include>src/test/java/</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

END OF UPDATE

I have created a maven project in IntelliJ IDEA for creating Selenium test in Ubuntu 20.10.

The problem is when I try to run this via java I got the error:

~/PROJECTS/selenium-test2$ sudo java -cp target/selenium-test2-1.0-SNAPSHOT.jar org.example.JUnitSeleniumTest
Error: Could not find or load main class org.example.JUnitSeleniumTest
Caused by: java.lang.ClassNotFoundException: org.example.JUnitSeleniumTest

Can you please tell me what is wrong? Or what I am doing wrong?

First I packaged it by:

$ mvn package
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< org.example:selenium-test2 >---------------------
[INFO] Building selenium-test2 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ selenium-test2 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ selenium-test2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ selenium-test2 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/nagardv/PROJECTS/selenium-test2/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ selenium-test2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ selenium-test2 ---
[INFO] Surefire report directory: /home/nagardv/PROJECTS/selenium-test2/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running JUnitSeleniumTest
Feb 21, 2021 10:46:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Feb 21, 2021 10:47:00 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Formy
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.233 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ selenium-test2 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  18.654 s
[INFO] Finished at: 2021-02-21T22:47:04+01:00
[INFO] ------------------------------------------------------------------------

And then tried to run from java command and there is error shown a the beginning. Same issue when I run this from the docker:

FROM openjdk:15-jdk-alpine
RUN apk update
RUN apk add maven
COPY pom.xml /usr/local/service/pom.xml
COPY src /usr/local/service/src

WORKDIR /usr/local/service
RUN mvn package
CMD ["java", "-cp", "target/selenium-test2-1.0-SNAPSHOT.jar", "org.example.JUnitSeleniumTest"]

Structure:

enter image description here

My test code:

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.By;
import java.net.URL;
import java.net.MalformedURLException;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;

public class JUnitSeleniumTest {
public static RemoteWebDriver driverF;
public static RemoteWebDriver driverCh;

@Before
public void initialize() throws MalformedURLException {
    FirefoxOptions firefoxOptions = new FirefoxOptions();
    ChromeOptions chromeOptions = new ChromeOptions();
    this.driverF = new RemoteWebDriver(new URL("http://192.168.2.31:4444/wd/hub"), firefoxOptions);
    driverF.get("https://formy-project.herokuapp.com/keypress");

    this.driverCh = new RemoteWebDriver(new URL("http://192.168.2.31:4444/wd/hub"), chromeOptions);
    driverCh.get("https://formy-project.herokuapp.com/keypress");
}

@Test
public void testing1() throws Exception{
    // Test1 Firefox
    String webelementF = this.driverF.getTitle();
    System.out.println(webelementF);
    assertEquals("Formy", webelementF);

    WebElement name = this.driverF.findElement(By.id("name"));
    name.click();
    name.sendKeys("Dorian");
    WebElement button = this.driverF.findElement(By.id("button"));
    button.click();
}

@AfterClass
public static void clenup() throws Exception{
    driverF.quit();
    driverCh.quit();
}
}

pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 

4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>selenium-test2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Upvotes: 0

Views: 290

Answers (1)

udalmik
udalmik

Reputation: 7988

Your class has empty package, not the org.example you use. You can either remove package name from cmd launch or move your class to org.example package.

Upvotes: 1

Related Questions