This page looks best with JavaScript enabled

Jenkins Integrated Robot Framework Automated Testing

1. Installing a Headless Browser

1.1 Installing PhantomJS on CentOS

  • Download and extract

Visit Phantomjs, find the download link for Download phantomjs-2.1.1-linux-x86_64.tar.bz2, and copy it.

Run the following commands on CentOS:

1
2
3
4
5
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
# 如果没有安装 bzip2 可能会报错
yum install bzip2.x86_64
tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
mv phantomjs-2.1.1-linux-x86_64 /usr/local/
  • Add it to the environment variables
1
vim /etc/profile

Append the following at the end of the file:

1
export PATH=$PATH:/usr/local/phantomjs-2.1.1-linux-x86_64/bin

Make the environment variables take effect immediately:

1
source /etc/profile

Check the PhantomJS version:

1
phantomjs --version

After installing it, I found this notice on the Phantomjs official site: Important: PhantomJS development is suspended until further notice. It means PhantomJS development has been suspended — heartbreaking. When using PhantomJS in Robot Framework, the Console prints a similar notice as well.

That does not matter, though: PhantomJS is only one kind of headless browser, and starting with version 59 Chrome added a new mode — headless mode. In other words, Chrome headless can replace PhantomJS.

1.2 Installing Chrome on CentOS

  • Configure the installation source
1
2
cd /ect/yum.repos.d/
vi google-chrome.repo

Add the following content:

1
2
3
4
5
6
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
  • Start the installation:
1
yum install google-chrome-stable
  • Check the Chrome version
1
2
google-chrome -version
Google Chrome 68.0.3440.84
  • Install chromedriver

The chromedriver version must match the Chrome version. On the chromedriver page, find the download link for the matching driver.

1
2
3
wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/

tips: If you do not place the chromedriver file in the /usr/bin/ directory, running the test case fails with WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home.

2. Jenkins Configuration

2.1 Installing Jenkins

  • First you need a JDK environment installed, then you install Jenkins.
1
2
3
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins
  • Create the runner group and runner user
1
2
groupadd -g 1234 runner
useradd runner -u 1234 -g 1234
  • Modify the configuration file

/etc/sysconfig/jenkins

1
2
3
JENKINS_HOME="/data/runner"
JENKINS_USER="runner"
JENKINS_PORT="8080"
  • Start Jenkins

Visit the http://127.0.0.1:8080/ page:

1
2
3
4
5
6
Unlock Jenkins
To ensure Jenkins is securely set up by the administrator, a password has been written to the log (not sure where to find it?) and this file on the server:

/home/runner/secrets/initialAdminPassword

Please copy the password from either location and paste

Following the page prompt, run cat /home/runner/secrets/initialAdminPassword to find the password. Then it is recommended to install the plugins Jenkins recommends — click Install suggested plugins.

2.1 Installing the robotframework Plugin

On the Manage Jenkins > Plugin Manager page, select the Avaliable tab. Search for robot and install the Robot Framework plugin plugin.

Finally, do not forget to restart Jenkins so the plugin takes effect.

3. Pipeline Configuration

  • In Jenkins, create a Freestyle project named robot-framework-demo

  • Under Source Code Management, select the Git type and enter: https://github.com/shaowenchen/docker-robotframework.git. The repository contains a simple Robot Framework test case that takes a screenshot of a website’s homepage.

  • Under Build, choose Execute Shell in Add build step and enter: /usr/bin/pybot ..

  • Under Post-build Actions, choose Publish Robot Framework test results in Add post buld step. Click Advanced to expand more options, and enter *.png in Others files to copy. This is so that the screenshots can be seen in report.html or log.html. Otherwise the test case screenshots are not archived to the specified location.

  • After installing the robot framework plugin, you can see the test results directly in the details of each build.

  • View the test report

  • View the test log

4. Problems and Solutions

  • Opening Robot Framework report/log failed

Enter the following in Manage Jenkins > Script Console:

1
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

Click Run.

You need to run the build once more, and then you can view report.html and log.html.

  • WebDriverException: Message: unknown error: DevToolsActivePort file doesn’t exist

Add --no-sandbox --headless --disable-gpu to the Chrome arguments. If you are not using Robot Framework, in code:

1
2
3
4
5
6
7
8
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-gpu`')
driver = webdriver.Chrome(executable_path="/usr/bin/chromedriver",
chrome_options=options)
  • Selenium2Library . Capture Page Screenshot produces garbled screenshots

Install Chinese fonts on CentOS

1
yum install cjkuni-ukai-fonts cjkuni-uming-fonts wqy-zenhei-fonts -y

5. References


微信公众号
WRITTEN BY
微信公众号