简体   繁体   English

如何在 Bitbucket 管道中使用 Webdriver Testcontainer?

[英]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:尝试在 Bitbucket 管道中使用 Webdriver Testcontainer 时,我收到以下错误消息:

[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我的测试容器版本是 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:这是我的 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}]通过在我的测试方法中设置断点并使用docker inspect -f '{{.Mounts }}'我能够发现 selenium/standalone-chrome:4.1.1 图像的容器具有[{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.我认为在我的 chrome 选项中使用 --disable-dev-shm-usage 参数可以防止这种情况发生,但事实并非如此。 I don't know whether that's what's causing my issue in Bitbucket Pipelines though.我不知道这是否是导致我在 Bitbucket 管道中出现问题的原因。

I found that it worked after setting shm size to zero.我发现它在将 shm 大小设置为零后有效。 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. 📖 🤓  🔍 👍  ✨");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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