webdrivermanager
webdrivermanager copied to clipboard
java.lang.RuntimeException: java.io.IOException: com.sun.jna.LastErrorException
Description of the problem: Cannot create WebDriver in Docker using coretto JDK
Browser and version: Latest (Current 103.0.5060.53)
Operating system: Amazon Linux 2 (Linux 5.13.0-51-generic)
WebDriverManager version: 5.2.0
WebDriverManager call: WebDriverManager.chromedriver().avoidBrowserDetection().browserInDocker().capabilities(new ChromeOptions().addArguments("--headless")).enableVnc().enableRecording().create();
WebDriverManager traces:
2022-06-27 15:02:37.493 ERROR 1 --- [lTaskExecutor-5] i.g.bonigarcia.wdm.WebDriverManager : There was an error creating WebDriver object for Chrome
2022-06-27T15:02:37.493479007Z
2022-06-27T15:02:37.493488690Z java.lang.RuntimeException: java.io.IOException: com.sun.jna.LastErrorException: [2] No such file or directory
2022-06-27T15:02:37.493496609Z at com.github.dockerjava.httpclient5.ApacheDockerHttpClientImpl.execute(ApacheDockerHttpClientImpl.java:195) ~[docker-java-transport-httpclient5-3.2.13.jar!/:na]
2022-06-27T15:02:37.493503385Z at com.github.dockerjava.httpclient5.ApacheDockerHttpClient.execute(ApacheDockerHttpClient.java:9) ~[docker-java-transport-httpclient5-3.2.13.jar!/:na]
2022-06-27T15:02:37.493510349Z at com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:228) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493517572Z at com.github.dockerjava.core.DefaultInvocationBuilder.post(DefaultInvocationBuilder.java:124) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493525424Z at com.github.dockerjava.core.exec.CreateContainerCmdExec.execute(CreateContainerCmdExec.java:37) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493532948Z at com.github.dockerjava.core.exec.CreateContainerCmdExec.execute(CreateContainerCmdExec.java:13) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493540250Z at com.github.dockerjava.core.exec.AbstrSyncDockerCmdExec.exec(AbstrSyncDockerCmdExec.java:21) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493547308Z at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:35) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493560409Z at com.github.dockerjava.core.command.CreateContainerCmdImpl.exec(CreateContainerCmdImpl.java:608) ~[docker-java-core-3.2.13.jar!/:na]
2022-06-27T15:02:37.493564565Z at io.github.bonigarcia.wdm.docker.DockerService.startContainer(DockerService.java:243) ~[webdrivermanager-5.2.0.jar!/:na]
2022-06-27T15:02:37.493568914Z at io.github.bonigarcia.wdm.docker.DockerService.startBrowserContainer(DockerService.java:644) ~[webdrivermanager-5.2.0.jar!/:na]
2022-06-27T15:02:37.493573103Z at io.github.bonigarcia.wdm.WebDriverManager.createDockerWebDriver(WebDriverManager.java:1820) [webdrivermanager-5.2.0.jar!/:na]
2022-06-27T15:02:37.493578758Z at io.github.bonigarcia.wdm.WebDriverManager.instantiateDriver(WebDriverManager.java:1713) [webdrivermanager-5.2.0.jar!/:na]
2022-06-27T15:02:37.493583248Z at io.github.bonigarcia.wdm.WebDriverManager.create(WebDriverManager.java:412) [webdrivermanager-5.2.0.jar!/:na]
2022-06-27T15:02:37.493587204Z at com.tartigrado.anpapi.automation.AnpPage.
I've just run a test using your command, and it works nicely. This is the test:
class DockerChromeTest {
WebDriver driver;
WebDriverManager wdm = WebDriverManager.chromedriver()
.avoidBrowserDetection().browserInDocker()
.capabilities(new ChromeOptions().addArguments("--headless"))
.enableVnc().enableRecording();
@BeforeEach
void setupTest() {
driver = wdm.create();
}
@AfterEach
void teardown() {
wdm.quit();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
assertThat(driver.getTitle()).contains("Selenium WebDriver");
}
}
And this is the output:
2022-07-27 16:27:44 [main] DEBUG i.g.b.wdm.docker.DockerHubService.listTags(60) -- Getting browser image list from Docker Hub
2022-07-27 16:27:46 [main] DEBUG i.g.b.wdm.docker.DockerService.getImageVersionFromDockerHub(440) -- The latest version of Chrome in Docker Hub is 103.0
2022-07-27 16:27:46 [main] INFO i.g.b.wdm.docker.DockerService.pullImageIfNecessary(299) -- Pulling Docker image selenoid/vnc:chrome_103.0 (this might take some time, but only the first time)
2022-07-27 16:28:09 [main] DEBUG i.g.b.wdm.cache.ResolutionCache.putValueInResolutionCacheIfEmpty(119) -- Storing resolution chrome-container-latest=103.0 in cache (valid until 17:28:09 27/07/2022 CEST)
2022-07-27 16:28:09 [main] INFO i.g.b.wdm.docker.DockerService.startContainer(173) -- Starting Docker container selenoid/vnc:chrome_103.0
2022-07-27 16:28:09 [main] DEBUG i.g.b.wdm.docker.DockerService.startBrowserContainer(667) -- VNC server URL: vnc://localhost:49153/
2022-07-27 16:28:09 [main] DEBUG i.g.b.wdm.webdriver.WebDriverCreator.createRemoteWebDriver(77) -- Creating WebDriver object for chrome at http://localhost:49154/ with Capabilities {browserName: chrome, goog:chromeOptions: {args: [--disable-gpu, --headless], extensions: []}}
Jul 27, 2022 4:28:10 PM org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer createTracer
INFO: Using OpenTelemetry for tracing
Jul 27, 2022 4:28:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected upstream dialect: W3C
2022-07-27 16:28:11 [main] DEBUG i.g.b.wdm.webdriver.WebDriverCreator.getSessionId(115) -- The sessionId is 783878ad97373221e9ec8bde8a78e7ee
2022-07-27 16:28:11 [main] DEBUG i.g.b.wdm.cache.ResolutionCache.checkValidity(156) -- Removing resolution novnc-container=1.1.0 from cache (expired on Fri Jun 03 06:31:11 CEST 2022)
2022-07-27 16:28:11 [main] INFO i.g.b.wdm.docker.DockerService.pullImageIfNecessary(299) -- Pulling Docker image bonigarcia/novnc:1.1.0 (this might take some time, but only the first time)
2022-07-27 16:28:13 [main] DEBUG i.g.b.wdm.cache.ResolutionCache.putValueInResolutionCacheIfEmpty(119) -- Storing resolution novnc-container=1.1.0 in cache (valid until 17:28:13 27/07/2022 CEST)
2022-07-27 16:28:13 [main] INFO i.g.b.wdm.docker.DockerService.startContainer(173) -- Starting Docker container bonigarcia/novnc:1.1.0
2022-07-27 16:28:13 [main] INFO i.g.bonigarcia.wdm.WebDriverManager.createDockerWebDriver(1896) -- Docker session noVNC URL: http://localhost:49155/
2022-07-27 16:28:13 [main] INFO i.g.b.wdm.docker.DockerService.pullImageIfNecessary(299) -- Pulling Docker image selenoid/video-recorder:7.1 (this might take some time, but only the first time)
2022-07-27 16:28:20 [main] DEBUG i.g.b.wdm.cache.ResolutionCache.putValueInResolutionCacheIfEmpty(119) -- Storing resolution recorder-container=7.1 in cache (valid until 17:28:20 27/07/2022 CEST)
2022-07-27 16:28:20 [main] INFO i.g.b.wdm.docker.DockerService.startContainer(173) -- Starting Docker container selenoid/video-recorder:7.1
2022-07-27 16:28:20 [main] INFO i.g.bonigarcia.wdm.WebDriverManager.createDockerWebDriver(1910) -- Starting recording ./chrome_2022.07.27_16.28.20.313_783878ad97373221e9ec8bde8a78e7ee.mp4
2022-07-27 16:28:21 [main] DEBUG i.g.bonigarcia.wdm.WebDriverManager.quit(956) -- Quitting RemoteWebDriver: chrome on LINUX (783878ad97373221e9ec8bde8a78e7ee)
2022-07-27 16:28:21 [main] INFO i.g.b.wdm.docker.DockerService.stopAndRemoveContainer(323) -- Stopping Docker container selenoid/video-recorder:7.1
2022-07-27 16:28:23 [main] INFO i.g.b.wdm.docker.DockerService.stopAndRemoveContainer(323) -- Stopping Docker container selenoid/vnc:chrome_103.0
2022-07-27 16:28:23 [main] INFO i.g.b.wdm.docker.DockerService.stopAndRemoveContainer(323) -- Stopping Docker container bonigarcia/novnc:1.1.0
I run it in Linux and Windows, and both operating systems work.
I guess your problem is you do not have access to Docker. If you are running this into a container, then I think you need Docker in Docker.
@bonigarcia I have tried your code, and I see an issue with method wdm.create()
.
It returns null
if it failed to create webdriver, which causes NPE in the following tests.
I believe it should throw an exception instead.
@BeforeEach
void setupTest() {
driver = wdm.create();
// logs WebDriverManager... ("Timeout of 30 seconds creating WebDriver object")
// BUT returns null
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/"); // causes NullPointerException because `driver` is null
}
I have just released WDM 5.2.2. This version includes the latest PRs about NPE handling. In any case, the Docker support should work (of course, Docker should be installed in the machine running the test).
@bonigarcia I am able to create wdm instance and assign it to driver when i run it on my local , but same thing fails when i run the tests inside docker container
Here is the dockerfile i am using
FROM maven:3-eclipse-temurin-11 WORKDIR /home/app
RUN apt-get update &&
apt-get install -y wget &&
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb &&
apt-get install ./google-chrome-stable_current_amd64.deb -y &&
rm ./google-chrome-stable_current_amd64.deb
COPY src src COPY *.xml ./
Here is the wdm instance created
WebDriverManager wdm = WebDriverManager.chromedriver()
.avoidBrowserDetection().browserInDocker()
.capabilities(new ChromeOptions().addArguments("--headless"))
.enableVnc().enableRecording();
//driver.manage().window().maximize();
driver=wdm.create();
logger.info("driver initialised is" +driver);
Error trace
/root/.cache/selenium/chromedriver/linux64/105.0.5195.52/chromedriver: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
Running TestSuite 2022-09-28 05:44:03 INFO [main] BasePage:55 - Intializing Driver 2022-09-28 05:44:03 INFO [main] BasePage:81 - assigning driver based on option 2022-09-28 05:44:03 INFO [main] BasePage:86 - options for capabilitiesCapabilities {browserName: chrome, goog:chromeOptions: {args: [--disable-dev-shm-usage, headless, --headless], extensions: []}} 2022-09-28 05:44:05 INFO [main] BasePage:90 - driver initialised isnull 2022-09-28 05:44:05 INFO [main] BasePage:91 - driver optionsCapabilities {browserName: chrome, goog:chromeOptions: {args: [--disable-dev-shm-usage, headless, --headless], extensions: []}} 2022-09-28 05:44:05 INFO [main] BasePage:60 - java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.toString()" because the return value of "java.lang.ThreadLocal.get()" is null