Robot Framework SeleniumLibrary
Chrome Docker
We will use Robot Framework
to run E2E test in a Docker container.
Here are the frameworks and toolkit that we will use.
·
Robot Framework: An open source
automation framework
·
SeleniumLibrary:
Web testing library for Robot Framework.
·
ChromeDriver: WebDriver
for Chrome
·
XVFB:
It performs all graphical operations in virtual memory without showing any
screen output.
·
Katalon
Recorder: Web extension for automating actions and automated testing on the
browser
▋Robot Framework 4.1.2
▋ChromeDriver 95.0.4638.17
▋Google Chrome 95.0.4638.54
▋SeleniumLibrary 5.1.3
▋Dockerfile
Here is a sample dockerfile that installs Chrome, ChromeDriver
and XVFB on the fly.
FROM python:3.7
COPY src/env/drivers/Linux/chromedriver /usr/local/bin
RUN chmod +x /usr/local/bin/
# Install python packages
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN pip install -r requirements.txt
# Install dependencies of Chrome driver and chrome
# Notice that xvfb is an in-memory display server for Linux
RUN apt-get update && \
apt-get install -y libnss3 libdbus-1-dev && \
apt-get install -y xdg-utils libgbm1 libasound2 fonts-liberation xvfb
# Install google-chrome
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& dpkg -i google-chrome*.deb \
&& rm google-chrome*.deb
# (Optional) Start dbus in WSL, see https://github.com/microsoft/WSL/issues/376#issuecomment-295933508
RUN /etc/init.d/dbus start
ENV PATH=/usr/local/bin:$PATH
▋Requirement: Python packages
Here are the python packages we need to run recorded E2E test by
Chrome with Robot Framework.
certifi==2020.4.5.1
chardet==3.0.4
idna==2.9
requests==2.23.0
robotframework==4.1.2
robotframework-requests==0.7.0
urllib3==1.25.9
robotframework-seleniumlibrary==5.1.3
robotframework-xvfb==1.2.2
▋Export Test Case to Robot Framework from Katalon Recoder
One of the fast ways to have an E2E test case is recording the
end-user’s steps by Selenium IDE that can be exported or playback. Take Katalon
Recorder for example, we can export a test case as Robot Framework format
(.robot file) like following.
However, if we want to run the test in a docker environment, we
will need to modify some code of the .robot file.
▋Update Test Case
The original test case exported from Katalon Recorder is as following,
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${BROWSER} chrome
${SELSPEED} 0.0s
*** Test Cases ***
My Test
[Setup] Run Keywords Open Browser https://172.19.160.1:5001/OpenId/Login ${BROWSER}
... AND Set Selenium Speed ${SELSPEED}
# ... skip
[Teardown] Close Browser
*** Keywords ***
# ... skip
To run Chrome in a docker container (Like Debian in this sample),
we need to run it with headless and no-sandbox arguments.
·
--headless: Run Chrome without GUI.
·
--no-sandbox: Sandbox removes unnecessary
privileges from the processes that don't need them in Chrome. Disable
Sandboxing will run google chrome as a root user.
Let’s create a module that can create and return a new WebDriver
instance with the above arguments.
▋ChromeConfiguration.py
from selenium.webdriver.chrome.options import Options
def config():
options = Options()
options.add_argument('--headless')
options.add_argument("--no-sandbox")
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--window-size=1920,1080")
options.add_argument('--log-level=ALL')
return options
def serviceargs():
return ["--verbose", "--log-path=/var/log/chromedriver.log"]
Then we can use it in our test case.
▋MyTest.robot
*** Settings ***
Library SeleniumLibrary
Library ChromeConfiguration.py
Library XvfbRobot
*** Variables ***
${BROWSER} chrome
${SELSPEED} 0.0s
*** Test Cases ***
My Test
${chrome_options} ChromeConfiguration.Config
${args} ChromeConfiguration.Serviceargs
Start Virtual Display 1920 1080
Create WebDriver Chrome chrome_options=${chrome_options} service_args=${args}
Go To https://172.19.160.1:5001/OpenId/Login
# ... skip
[Teardown] Close Browser
*** Keywords ***
# ... skip
Notice that after the WebDriver created, we must use Go To keyword to
open the URL with the WebDriver instance.
Create WebDriver Chrome chrome_options=${chrome_options} service_args=${args}
Go To https://172.19.160.1:5001/OpenId/Login
Or we can replace the above 2 line with Open
Browser keyword.
Open Browser https://172.19.160.1:5001/OpenId/Login
${BROWSER} options=${chrome_options}
If you encounter any
problem, you can open /var/log/chromedriver.log to see what happened.
And please make sure
the Chrome and ChromeDriver are the same version.
For example, I am
having Chrome 95 for my testing.
▋Robot Framework
▋Robot
Framework: SeleniumLibrary
▋Selenium:
WebDriverException:Chrome failed to start: crashed as google-chrome is no
longer running so ChromeDriver is assuming that Chrome has crashed
▋WSL: dbus
doesn't seem to work