You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven+TestNG+Selenium Grid2启动Hub遇No main manifest attribute错误

关于Selenium Grid Hub在Maven+TestNG项目中的启动问题

核心问题拆解

你碰到的两个关键疑问:一是Maven引入的selenium-server依赖无法直接用java -jar启动,二是TestNG项目没有main方法时,POM中maven-jar-plugin的mainClass该如何配置。


为什么Maven依赖的selenium-server jar不能直接启动?

你通过Maven导入的selenium-server-3.12.0.jar和单独下载的selenium-server-standalone-3.8.1.jar是完全不同的包:

  • 单独下载的standalone包是一个自包含的可执行Jar,它打包了Selenium运行所需的所有依赖(包括Jetty服务器、WebDriver实现等),并且在Manifest文件中预先指定了主类(3.x版本对应org.openqa.grid.selenium.GridLauncherV3),所以可以直接用java -jar命令启动。
  • 而Maven仓库里的selenium-server只是Selenium Grid的核心类库,不包含完整依赖,也没有在Manifest中配置主类,它的定位是供项目代码调用,而非直接启动运行,所以执行java -jar会提示"No main manifest attribute"。

结论:即使通过Maven导入了selenium-server依赖,你仍然需要单独下载selenium-server-standalone包来启动Grid Hub/Node,或者用Maven插件帮你整合启动逻辑。


TestNG项目无main方法时,POM的mainClass该怎么配置?

首先要明确:你的TestNG测试类本身不需要main方法,但如果是想通过Maven打包成可执行Jar来运行测试(而非启动Grid),才需要配置mainClass。如果你的需求是启动Grid Hub,这个配置其实帮不上忙。

如果想用Maven来启动Selenium Grid Hub,更推荐用maven-exec-plugin直接调用Grid的启动类,无需单独下载standalone包,具体POM配置如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>start-hub</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>org.openqa.grid.selenium.GridLauncherV3</mainClass>
                <arguments>
                    <argument>-role</argument>
                    <argument>hub</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.12.0</version>
        </dependency>
    </dependencies>
</plugin>

执行命令:mvn exec:java@start-hub,就能直接通过Maven调用Grid的启动类启动Hub。

如果一定要用maven-jar-plugin打包成可执行Jar(不推荐,因为需要手动处理所有依赖的类路径),可以指定Grid的启动类作为mainClass:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>org.openqa.grid.selenium.GridLauncherV3</mainClass>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
    </configuration>
</plugin>

总结

  1. Maven的selenium-server是类库而非可执行包,无法直接java -jar启动;单独的standalone包是可执行包,因此能直接启动Hub。
  2. TestNG测试不需要main方法,启动Grid Hub推荐用exec-maven-plugin调用org.openqa.grid.selenium.GridLauncherV3,或者直接使用standalone包启动。

内容的提问来源于stack exchange,提问作者Ishita Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:40:16