无法从 jar 文件运行 Selenium 测试

时间:2021-02-21 21:56:41

标签: java selenium maven junit

更新:

感谢@udalmik,我验证了来自 src/test/java 的 java 文件未包含在 jar 文件中。 我尝试添加 maven 编译器插件,但仍然没有运气:

...
<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>

更新结束

我在 IntelliJ IDEA 中创建了一个 maven 项目,用于在 Ubuntu 20.10 中创建 Selenium 测试。

问题是当我尝试通过 java 运行它时出现错误:

~/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

你能告诉我有什么问题吗?还是我做错了什么?

首先我打包了:

$ 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] ------------------------------------------------------------------------

然后尝试从 java 命令运行,开始时显示错误。 当我从 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"]

结构:

enter image description here

我的测试代码:

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 文件:

<?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>

1 个答案:

答案 0 :(得分:1)

您的类有空包,而不是您使用的 org.example。您可以从 cmd 启动中删除包名称或将您的类移动到 org.example 包。