Running Selenium with chromedriver inside a Windows container
Running Selenium for Web UI testing in a Windows container is a little bit tricky. I spent a week on this and finally could make it to work thanks to the idea in [1]. Woohuu!
Let's take a look at this sample Java application that runs Selenium with chromedriver.
1. Make sure your Desktop Docker running with Windows Container mode
2. In your Dockerfile, make sure:
- Install the needed fonts (googlechrome) => this is the most important thing!
ADD files/fonts.tar /Fonts/
WORKDIR /Fonts/
RUN @powershell -NoProfile -ExecutionPolicy Bypass -Command ".\Add-Font.ps1 Fonts"
- Enable Web-WebSockets feature using Powershell
- Install chocolatey to install openjdk8, and googlechrome
- Copy the chromedriver.exe driver into your working directory
ADD files/drivers/chromedriver.exe "C:/ProgramData/drivers"
Full Dockerfile:
FROM mcr.microsoft.com/windows/servercore:ltsc2019 | |
# install needed fonts for googlechrome to run properly | |
ADD files/fonts.tar /Fonts/ | |
WORKDIR /Fonts/ | |
RUN @powershell -NoProfile -ExecutionPolicy Bypass -Command ".\Add-Font.ps1 Fonts" | |
WORKDIR "C:/ProgramData" | |
# enable Web-WebSockets | |
RUN @powershell -NoProfile -ExecutionPolicy Bypass -Command "Add-WindowsFeature Web-WebSockets" | |
# install chocolately | |
RUN @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" -Y | |
# install openjdk8 | |
RUN choco install openjdk8 -Y | |
# update environment variables | |
RUN refreshenv | |
# install googlechrome | |
RUN choco install googlechrome -Y | |
# copy your application to the WORKDIR | |
ADD files/myapplication.war "C:/ProgramData" | |
# copy chromedriver.exe to the same level of your application | |
# assumming your application | |
ADD files/drivers/chromedriver.exe "C:/ProgramData/drivers" | |
EXPOSE 8080 | |
# run your application | |
CMD ["java", "-jar", "C:/ProgramData/myapplication.war"] |
3. Build your Docker image using Powershell with administrator privileges
PS C:\Users\Trinh Nguyen\projects\mycontainer> docker build . -t yourimage_uri:yourtag
4. Run your containerized application from Powershell
PS C:\Users\Trinh Nguyen\projects\mycontainer> docker run -p 8080:8080 yourimage_uri:yourtag
5. You can find the Dockerfile and supplementary files in [2].
References:
[1] https://github.com/gantrior/docker-chrome-windows
[2] https://github.com/dangtrinhnt/windows-container-utils
Comments
Post a Comment