Selenium Tests not working in Jenkins

Automated Testing is an important part of CI/CD process and Selenium is most popular tool for Automated Testing.

When you are using Jenkins to achieve  CI/CD, you might be using Selenium for Automated Testing.

Jenkins Selenium - DevOpsBuzz

And in some cases, you may find that your Selenium tests are working in windows command prompt but not with Jenkins

Issue# 1 : Your Jenkins server do not have host key added on the server where its hitting through selenium url

Issue# 2: Jenkins actually not able to run the cases on Desktop, even though you enable this through services -it will not work

 

Lets see through some steps how Selenium tests are behaving when we run them from Windows command prompt.

I have written 4 test cases and out for 4 tests – 2 should be passed and 2 should be failed.

Run the command to initiate the automate test execution

test run from windows 1 - DevOpsBuzz

It has started the execution and its in process to open Chrome

Test run from windows 2 - DevOpsBuzz

It has successfully opened Chrome browser
test run from windows 4 - DevOpsBuzz

Its opening the provided website and running the automated test cases.

test run from windows 5b - DevOpsBuzz

And confirming the Test execution status

test run from windows 6 - DevOpsBuzz

Yes, its working perfectly fine and we can see 2 Test cases executed successfully whereas 2 are failed as expected.

Now, lets try to execute the same through Jenkins build job and see the output.

tests run from jenkins failure - DevOpsBuzz

Ohh – Jenkins even can’t execute all the four test cases and the job was failed with the first test case failure.

 

Lets discuss Issue# 1 first –  Your Jenkins server do not have host key added on the server where its hitting through selenium url

Solution : Add the host key on both Jenkins servers and on Client server.

Open hosts file directory of Jenkins server – Go to C:\Windows\System32\Drivers\etc\hosts directory

issue fix 1 - DevOpsBuzz

Locate hosts file and open it

issue fix 3 - DevOpsBuzz

And add the host entry of the selenium server

issue fix 5a - DevOpsBuzz

Save the file and update the Selenium Client server’s host entry with Jenkins server details in same fashion.

Now run the Build job again

issue fix 6a - DevOpsBuzz

issue fix 7a - DevOpsBuzz

Now, at-least Jenkins is able to execute all 4 Test cases, but unfortunately all are failed because of another Issue.

Issue# 2: Jenkins actually not able to run the cases on Desktop, even though you enable this through services – It will not work

Solution : Either start your whole jenkins server with command or just create a normal slave on the same machine where selenium server is installed OR where the test cases worked with command prompt (in my case its on the same host as Jenkins server)

Add the new Slave in Jenkins

issue fix 9b - DevOpsBuzz

Configure it according to your requirement

issue fix 10a - DevOpsBuzz

Use the default “Launch agent via Java Web start” – To know how to add Windows Slave to Jenkins, please refer article

issue fix 11 - DevOpsBuzz

You need to add the new Windows agent till the Jenkins service creation part by referring article.

But, the task is not yet complete. We need to let the slave server know to allow the local system account to “Allow service to interact with Desktop”. This will allow Jenkins to open the browser on the Slave desktop and run the Selenium test execution.

Jenkins agent service - DevOpsBuzz Jenkins agent service 2 - DevOpsBuzz

Jenkins agent service 3 - DevOpsBuzz

 

You can check the status for new Jenkins Node

issue fix 13a - DevOpsBuzz

Now, you need to tied this newly created agent to the Selenium Jenkins job

issue fix 14a - DevOpsBuzz

Verify the TestNG Results file

issue fix 16a - DevOpsBuzz

Save it and Run the build again

issue fix 17a - DevOpsBuzz

The build has started

issue fix 18a - DevOpsBuzz

For testing purpose, I have kept the server’s session open and you will see how Jenkins is executing the tests on Chrome browser

issue fix 19a - DevOpsBuzz

It has opened the Chrome browser and trying to login to website to execute the automated tests

issue fix 20a - DevOpsBuzz

You can verify through Jenkins console as well- the Selenium tests are executing without any issue.

issue fix 21a - DevOpsBuzz

Finally, it has finished the build job and we can see the Selenium Test results as expected, i.e. 2 Tests are passed and 2 are failed

issue fix 22a - DevOpsBuzz

You can also see the Selenium Tests status on Jenkin’s Project Dashboard

issue fix 23a - DevOpsBuzz

And verify the TestNG results to explore more about passed/failed tests.

issue fix 24a - DevOpsBuzz

So now you should be able to execute your Selenium Test cases with Jenkins without any issue 🙂

 

5 thoughts on “Selenium Tests not working in Jenkins”

  1. Hi,
    I’ve followed all the steps you have mentioned except “adding the host entry of the selenium server”. I don’t have a server, I’ve installed selenium on my home machine. My question is how do I get the host ID of the selenium?
    Thank you for a detailed description of the problem and the solution.
    Thanks

    1. Hi Zara,
      Thanks for your feedback.
      Can you please provide more details :
      – Is your Jenkins server also running on your home machine ?
      – If not, then can you install selenium on Jenkins or any other server which is in same network ?
      – Do you have remote access enabled from Jenkins machine to your local machine (if they are on different machines) ?
      – Please also mention how far did you reach in this setup and what’s causing an issue ?
      Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top