When you are using Jenkins to achieve CI/CD, you might be using Selenium for Automated Testing.
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
It has started the execution and its in process to open Chrome
It has successfully opened Chrome browser
Its opening the provided website and running the automated test cases.
And confirming the Test execution status
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.
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
Locate hosts file and open it
And add the host entry of the selenium server
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
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
Configure it according to your requirement
Use the default “Launch agent via Java Web start” – To know how to add Windows Slave to Jenkins, please refer article
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.
You can check the status for new Jenkins Node
Now, you need to tied this newly created agent to the Selenium Jenkins job
Verify the TestNG Results file
Save it and Run the build again
The build has started
For testing purpose, I have kept the server’s session open and you will see how Jenkins is executing the tests on Chrome browser
It has opened the Chrome browser and trying to login to website to execute the automated tests
You can verify through Jenkins console as well- the Selenium tests are executing without any issue.
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
You can also see the Selenium Tests status on Jenkin’s Project Dashboard
And verify the TestNG results to explore more about passed/failed tests.
So now you should be able to execute your Selenium Test cases with Jenkins without any issue 🙂