Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run selenium chrome driver in a docker container?

tl;dr

How can I install all the components to run Selenium in a docker container?


Question

I'm starting with this image:

FROM microsoft/aspnetcore-build:2 AS builder
WORKDIR /source

COPY . .
RUN dotnet restore
RUN dotnet build
ENTRYPOINT ["dotnet", "run"]

How can I make it so that I can start and use an headless Chrome Driver with this:

ChromeOptions options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
var driverPath = Path.GetFullPath(Path.Combine(environment.ContentRootPath, "bin/Debug/netcoreapp2.0"));
return new ChromeDriver(driverPath, options, TimeSpan.FromSeconds(60));

within a docker container?


What have I tried

Installing the Chrome driver

The chromedriver is distributed via the Selenium.WebDriver.ChromeDriver NuGet package.

Installing Chrome

On my Mac OS X with Google Chrome installed the current setup works just fine.

I've tried to add these lines:

RUN apt-get update && apt-get -y install libglib2.0-dev libxi6 libnss3-dev
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get -y install google-chrome-stable

The above installs this version of Chrome:

google-chrome-stable:
  Installed: 64.0.3282.119-1
  Candidate: 64.0.3282.119-1
  Version table:
 *** 64.0.3282.119-1 500
        500 http://dl.google.com/linux/chrome/deb stable/main amd64 Packages
        100 /var/lib/dpkg/status

which is compatible with the version of the Chrome Driver.

which come from trying to solve each error that came out by trying to run Selenium with the docker container.

If I run this setup I get:

Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 57889 Only local connections are allowed. An error occurred while sending the request. Couldn't connect to

when running the container.

Debugging in the container

If I enter the container manually and try to run the chrome driver manually I get:

Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 9515 Only local connections are allowed.

and running curl -i http://localhost:9515/status I get:

HTTP/1.1 200 OK
Content-Length:136
Content-Type:application/json; charset=utf-8
Connection:close

{"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Linux","version":"4.9.60-linuxkit-aufs"}}}

so it seems that the driver works just fine.

If I run chrome headless instead via google-chrome-stable --headless --disable-cpu --no-sandbox I get:

[0125/210641.877388:WARNING:discardable_shared_memory_manager.cc(178)] Less than 64MB of free space in temporary directory for shared memory files: 63
[0125/210641.902689:ERROR:instance.cc(49)] Unable to locate service manifest for metrics
[0125/210641.902756:ERROR:service_manager.cc(890)] Failed to resolve service name: metrics
[0125/210642.031088:ERROR:instance.cc(49)] Unable to locate service manifest for metrics
[0125/210642.031119:ERROR:service_manager.cc(890)] Failed to resolve service name: metrics
[0125/210642.032934:ERROR:gpu_process_transport_factory.cc(1009)] Lost UI shared context.

The first warning can be solved via setting a docker volume in /dev/shm:/dev/shm or by setting -shm-size to something large (higher than 64MB).

The rest of the errors, if google, lead to me many bug reports from Google Chrome repositories.

like image 857
Shoe Avatar asked Jan 26 '18 15:01

Shoe


People also ask

How do I run a test in a Docker container?

Running tests against a browser in a Docker container through an Open Agent that is running in another Docker container. The browser and the Open Agent communicate with each other through a Docker network. The test executor is running on your local Windows machine. Running tests entirely from Docker containers.

How Docker helps in automation testing?

Docker Hub can automatically test changes to your source code repositories using containers. You can enable Autotest on any Docker Hub repository to run tests on each pull request to the source code repository to create a continuous integration testing service.

How do I integrate a Chrome driver using selenium?

Go to the terminal and type the command: sudo nano /etc/paths. Enter the password. At the bottom of the file, add the path of your ChromeDriver. Type Y to save.


1 Answers

The most popular options are "docker selenium" or "selenoid". The implementation is different but both solutions take advantage of docker to create test environment similar to selenium grid.

I recommend "selenoid" and to configure it properly you could start with the following guide: https://www.swtestacademy.com/selenoid-tutorial/

If you choose "docker selenium" this could be your starting point: https://www.swtestacademy.com/docker-selenium-tutorial/

like image 55
eroteev Avatar answered Sep 23 '22 19:09

eroteev