我正在尝试在Docker容器中运行Selenium Java项目:
我的docker-compose.yml:
version: '3'
services:
hub:
image: selenium/hub:3.141.59-vanadium
container_name: hub
ports:
- "4444:4444"
firefox:
image: selenium/node-firefox:3.141.59-vanadium
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
- HUB_HOST=hub
- HUB_PORT=4444
mongo:
image: 'mongo:4.0.4'
ports:
- '27017:27017'
volumes:
- 'mongo:/data/db'
app:
image: app:latest
ports:
- "5050:5050"
depends_on:
- mongo
- firefox
volumes:
mongo:
我的适用于Java应用程序的Dockerfile:
#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
# build jar with dependencies
RUN mvn -f /home/app/pom.xml clean compile assembly:single
#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/app-1.0-SNAPSHOT-jar-with-dependencies.jar /usr/local/lib/app.jar
EXPOSE 5001
ENTRYPOINT ["java","-jar","/usr/local/lib/app.jar", "docker"]
来自pom.xml的插件:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>learn.selenium.Main</mainClass>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>learn.selenium.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>learn.selenium.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
通过代码连接到硒:
Capabilities firefoxCapabilities = new FirefoxOptions();
WebDriver driver = new RemoteWebDriver(new URL("http://hub:4444/wd/hub"), firefoxCapabilities);
当我尝试仅用硒材料运行docker compose时,看起来工作正常。但是,当我向容器中添加Java应用程序时,出现以下错误:
hub | org.openqa.grid.common.exception.GridException: Empty pool of VM for setup Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:firefoxOptions: {args : {}}}
hub | at org.openqa.grid.internal.ProxySet.verifyAbilityToHandleDesiredCapabilities(ProxySet.java:146)
...
app_1 | org.openqa.selenium.WebDriverException: Error forwarding the new session Empty pool of VM for setup Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:firefoxOptions: {args: [], prefs: {}}}
app_1 | Command duration or timeout: 453 milliseconds
app_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
您能告诉我我在做什么错吗?
答案 0 :(得分:0)
好吧,似乎在尝试连接集线器时,集线器尚未准备好。因此,为了进行测试,我添加了Thread.sleep(1000)并成功了。我想这里的正确方法是对集线器执行ping操作,并在状态更改为就绪后继续进行Web驱动程序初始化。 对我来说,这很奇怪,因为我添加了
depends_on:
- mongo
- firefox
所以我希望我的Java项目在与硒相关的容器启动并准备就绪后运行。