Web App Testing With Robot Framework Via Jenkins In Docker
Overview
This blog post will show you one of practical testing scenarios which clearly demonstrates how to automate web application testing scenario with Robot Framework.
The test is specified to run on Chrome browser so you need to have follow steps to install Chrome on Jenkins server.
ref: install Chrome using Dockerfile
Additionally, the test is using Python language so some steps to install Python on Jenkins environment are also necessary. You can use Dockerfile
to make it done.
ref: install Python3 using Dockerfile
The blog does not explain docker in depth but you can visit some reference links for details. Instead, it focuses on configuring Jenkins job, running the test and viewing the test report.
And it is designed to work on Windows 10 Pro only, not on a macOS or Linux machine.
Before continuing, ensure you have installed the following tools
- Docker
You can install the latest version of Docker Desktop which contains both Docker and Docker-Compose tools
ref: download link - GitLab
During the steps, you will be required to download the source code to GitLab. Ensure you have installed and configured Git and authentication to your GitLab
Jenkins Server Startup
Follow the procedure below to set up a Jenkins server with Chrome and Python3 installed.
If you are currently working from home, VPN connection may prevent installing Jenkins plugins. Please consider disconnecting VPN temporarily before continuing the next steps.
- Run Docker Desktop
- Start Jenkins container using docker-compose
- Checkout the source code from GitLab(Link is TBD)
- Change directory to project root folder and then run 2 following commands sequentially
docker-compose build
&docker-compose up -d
- Wait until both containers are up and running successfully
D:\projects\jenkins-robot-sample>docker-compose build
...
[+] Building 364.8s (16/16) FINISHED
...
[+] Running 2/2kins-robot-sample>docker-compose up -d
- Network jenkins-robot-sample_net Created 0.1s
- Container jenkins-robot Started 3.0s
D:\projects\jenkins-robot-sample>
-
Access localhost:8080 to perform initial configuration on Jenkins
- Wait until the Unlock Jenkins page appears
- Get password by accessing Jenkins container
docker logs jenkins-robot
- Paste the password into the Administrator password field and click Continue
- Install suggested plugins
- When doing Create the first administrator user then select Skip and continue as admin as screenshot
- And in the remaining screens select option as default (blue highlight)
- Then welcome screen is displayed
- Change password using Configure button
- Log in again with a new password
- Then Welcome page is displayed again
ref: Setup Wizard
- Wait until the Unlock Jenkins page appears
-
Install
Pyenv Pipeline
plugin from Jenkins Dashboard localhost:8080 >> Manage Jenkins >> Manage Plugins (Restart Jenkins after installed it)
ref: Pyenv Pipeline plugin
Create & Configure Pipeline Job
Follow the procedure below to create and configure pipeline job to do the following
- Download the latest source code from GitLab
- Run test with current source code inside virtual environment
In the upcoming configuration, we will use an existing GitLab repository(Link is TBD) which contains a simple Python test.
The test focuses on downloading a file in headless mode (Chrome) and then save it to a specific directory. After the Jenkins build finished, test report is generated as log.html
, you can view it directly on Last Artifacts with your browser or you can download .zip file then view it at local.
Now please follow the next step-by-step
- Create a new item for pipeline job
- Copy the script below to pipeline definition and save it
node {
stage('Clone code from Git') {
// The below will clone your repo and will be checked out to master branch by default.
git credentialsId: 'auto-credentials', url: 'https://gitlab.shiftasia.com/thuanpham/jenkins-robot-sample.git', branch: 'master'
sh 'rm -f testsuits/output_data/python_docs/*.zip'
}
stage('Run test') {
// Run test inside virtual environment
withPythonEnv('/usr/bin/python3') {
sh 'pip install -r requirements.txt'
sh 'cd testsuits && robot --outputdir output_data scenario_download.robot'
}
}
stage('Archive robot ouptut files') {
def file='testsuits/output_data/'+JOB_NAME+BUILD_NUMBER+'.zip'
sh 'zip -r '+file+' testsuits/output_data/screenshots/ testsuits/output_data/log.html testsuits/output_data/report.html'
archiveArtifacts artifacts: 'testsuits/output_data/log.html,testsuits/output_data/report.html,'+file
sh 'rm -rf testsuits/output_data/screenshots'
}
}
The path /usr/bin/python3
has been utilized for Python environment setup.
For pipeline syntax, click the link for details
You can generate the following command by using it
git credentialsId: 'auto-credentials', url: 'https://gitlab.shiftasia.com/thuanpham/jenkins-docker-sample.git'
- Run the job
If you are currently working from home, VPN connection is required to run the job. The job will download source code from GitLab.
- Click Console Output to view the log
- Navigate back to
Robot_Test
job to view Last Artifacts
- Open
log.html
directly with your browser, you cannot view the screenshots attached with log. At this build, the test result is PASS otherwise the error will be displayed here
- Download .zip file and save it at local
- Unzip the file at local
- Open
log.html
at local with your browser, you can view the screenshots attached with log
- Stop Jenkins server by the command
docker-compose down
- Restart the server by the command
docker-compose up -d
That's all for the example, thanks for reading.
Enjoy!