Site icon Vinsguru

Selenium WebDriver – Embedding Zalenium Live Preview In Jenkins

Overview:

I have been using Docker a lot for my automated Selenium Webdriver test execution. I use Zalenium for the selenium grid.

If you are not sure what Zalenium is, I would suggest you to take a look at this article first.

To dockerize your automated tests, check these.

As you are already aware, Zalenium provides a live preview of your test execution as shown here which is cool.

This is what happens as part of a Jenkins build.

I have multiple nodes. Most of the times, multiple jobs would be running on different nodes. So It was big challenging for me to access the Zalenium live preview for a specific build. Also, I had to remember node IP details /bookmark the zalenium url for every node.

I would like to show you how I was able to embed the live preview in the Jenkins job itself directly whenever the test executes!!

Jenkins Plugins:

First, we need to install few Jenkins plugins.

Once plugins are installed, follow these steps.

Global Security:

When JavaScript is added inside the description, it effectively has access to the user’s session of the Jenkins, which can be then used to do operations on behalf of the user. There’s an inherent security risk in this. Use with caution, only when you can trust all the users of your installation.

Job Related Changes:

We need to do few changes in the Job Configuration.

import jenkins.model.Jenkins

def jobname = build.project.name
def hostname = build.getExecutor().getOwner().hostName
Jenkins.instance.getItem(jobname).description = "<iframe src='http://${hostname}:4444/grid/admin/live' width='1400' height='500'></iframe>"
import jenkins.model.Jenkins
def  jobname = manager.build.getProject().getName()
Jenkins.instance.getItem(jobname).description = ""

That’s it!

Demo:

 

Jenkinsfile – Declarative Pipeline:

The process is still almost same if you are using Jenkinsfile – declarative pipeline.

pipeline {
    agent any
    stages {
        stage("Setup IFrame") {
            steps {
                script {
                    currentBuild.rawBuild.project.setDescription("<iframe src='http://${hostname}:4444/grid/admin/live' width='1400' height='500'></iframe>")
                }
            }
        }
        stage("Run Test") {
            steps {
                echo 'Running Test'
            }
        }   
        stage("Remove IFrame") {
            steps {
                script {
                    currentBuild.rawBuild.project.setDescription("")
                }
            }
        }        
    }
}

Now I do not have to remember the node IP / zalenium URL details. Whenever a job executes, it automatically adds the preview in Jenkins itself and removes once the execution is done. Groovy post build script is always executed even when the build is aborted. So, it removes the IFrame irrespective of the status of the build once it is done.

Happy Testing & Subscribe 🙂

 

 

Share This:

Exit mobile version