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

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


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/


I used the Selenium image, installed dotnet runtime there and got it working.
Here is my Dockerfile:

FROM selenium/standalone-chrome:latest AS baseWORKDIR /app# build and copy dotnet projectFROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS buildWORKDIR /srcCOPY ["TestBot/TestBot.csproj", "TestBot/"]RUN dotnet restore "TestBot/TestBot.csproj"COPY . .WORKDIR "/src/TestBot"RUN dotnet build "TestBot.csproj" -c Release -o /app/buildFROM build AS publishRUN dotnet publish "TestBot.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .# install dotnetRUN sudo wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.debRUN sudo dpkg -i packages-microsoft-prod.debRUN sudo apt-get updateRUN sudo apt-get install -y dotnet-runtime-5.0ENTRYPOINT ["dotnet", "TestBot.dll"]

My C# code looks similar to yours:

using OpenQA.Selenium.Remote;using OpenQA.Selenium;using System;using OpenQA.Selenium.Chrome;using System.Drawing;using OpenQA.Selenium.Firefox;using System.Threading;namespace TestBot{    internal class Program    {        private static void Main(string[] args)        {            ChromeOptions Options = new ChromeOptions();            if (OperatingSystem.IsWindows())            {                Options.AddAdditionalCapability("platform", "WIN10", true); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)            }            else            {                Options.AddAdditionalCapability("platform", "LINUX", true); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)            }            Options.AddArgument("--headless");            ChromeDriver driver = new ChromeDriver(Options);            try            {                // driver.Manage().Window.Size = new Size(1920, 1080);                driver.Navigate().GoToUrl("https://google.com/");                var test = driver.FindElementByTagName("html").Text;                Console.WriteLine(test);            }            finally            {                driver.Quit();            }            Console.ReadLine();        }    }}

I used the following Libraries:enter image description here


There's a pretty neat framework built around Docker containers being used as Remote Driver locations.

http://aerokube.com/selenoid/latest/

I haven't fully implemented it yet, but I managed to effortlessly create docker containers with appropriate chrome and firefox drivers inside.