简体   繁体   中英

How can I use a Webdriver Testcontainer in Bitbucket Pipelines?

When trying to use a Webdriver Testcontainer in Bitbucket Pipelines, I get the following error messages:

[main] WARN 🐳 [selenium/standalone-chrome:4.1.1] - Unable to mount a file from test host into a running container. This may be a misconfiguration or limitation of your Docker environment. Some features might not work.
[main] ERROR 🐳 [selenium/standalone-chrome:4.1.1] - Could not start container
com.github.dockerjava.api.exception.DockerException: Status 403: {"message":"authorization denied by plugin pipelines: -v only supports $BITBUCKET_CLONE_DIR and its subdirectories"}

My testcontainers version is 1.17.6

Here is the code I'm using while trying to troubleshoot:

package com.byzpass.demo;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;

public class SeleniumTest {
    public ChromeOptions chromeOptions = new ChromeOptions().addArguments("--no-sandbox").addArguments("--headless").addArguments("--disable-dev-shm-usage");
    @Rule
    public BrowserWebDriverContainer<?> driverContainer = new BrowserWebDriverContainer<>().withCapabilities(chromeOptions);

    @Test
    public void openWikipedia() {
        WebDriver driver = new RemoteWebDriver(driverContainer.getSeleniumAddress(), chromeOptions);
        driver.navigate().to("https://www.wikipedia.org/");
        String subtitleText = driver.findElement(By.cssSelector("#www-wikipedia-org h1 strong")).getText();
        assert subtitleText.equals("The Free Encyclopedia");
        driver.quit();
        System.out.println("Finished opening wikipedia. 📖 🤓  🔍 👍  ✨");
    }
}

Here is my bitbucket-pipelines.yml:

pipelines:
  default:
    - step:
        image: amazoncorretto:11
        services:
          - docker
        script:
          - export TESTCONTAINERS_RYUK_DISABLED=true
          - cd selenium-test ; bash ./mvnw --no-transfer-progress test

definitions:
  services:
    docker:
      memory: 2048

By setting a breakpoint in my test method and using docker inspect -f '{{.Mounts }}' I was able to discover that the container for the selenium/standalone-chrome:4.1.1 image has [{bind /dev/shm /dev/shm rw true rprivate}]

I thought that using the --disable-dev-shm-usage argument in my chrome options would prevent that, but it didn't. I don't know whether that's what's causing my issue in Bitbucket Pipelines though.

I found that it worked after setting shm size to zero. Here's the code that worked:

package com.byzpass.demo;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.util.ArrayList;

public class SeleniumTest {
    @Test
    public void openWikipedia() {
        ChromeOptions chromeOptions = new ChromeOptions().addArguments("--no-sandbox").addArguments("--headless").addArguments("--disable-dev-shm-usage");
        BrowserWebDriverContainer driverContainer = new BrowserWebDriverContainer<>().withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.SKIP, null);

        driverContainer.setShmSize(0L);

        driverContainer.start();

        WebDriver driver = new RemoteWebDriver(driverContainer.getSeleniumAddress(), chromeOptions);
        driver.navigate().to("https://www.wikipedia.org/");
        String subtitleText = driver.findElement(By.cssSelector("#www-wikipedia-org h1 strong")).getText();
        assert subtitleText.equals("The Free Encyclopedia");
        driver.quit();

        driverContainer.stop();

        System.out.println("Finished opening wikipedia. 📖 🤓  🔍 👍  ✨");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM