How to run selenium tests in Jenkins
Jenkins configuration:
To run selenium scripts in Jenkins, Jenkins needs to started.
To make Jenkins server start, please use the following command in the Jenkins folder of your machine when Jenkins.war file is downloaded from internet…
- “java -jar jenkins.war” in DOS / command prompt mode.
- Then you need to bring the browser up and type “localhost:8080” to see jenkins web UI ready.
- Then navigate to Jenkins >> Manage Jenkins and click on “Configure system”
- Then scroll down to see the “JDK installations” section in the pag
- There you enter “JAVA_HOME” for “JDK name” field.
- Provide the Java-JDK file folder path for “JAVA_HOME” field.
Note: Java-JDK file folder path is usually “C:\Program files\Java\JDK….”
Note: Disable “Install Automatically” check box else it will update with new Java versions and your selenium program might get conflicts
Selenium script development:
- create a package and class in Eclipse
- and then create a small test script – like print some message based on UI or Browser title name etc
- you could create a TestNG project
- Then TestNG.xml file will be created in Eclipse
Note: to get the XML folder path in project home directory, you could go to Eclipse project and right click to see properties.
Note: you also need to put all the jar files required for selenium testing under the lib folder(you may need to create”lib” folder)
Just to test your TestNG scripts are working, you can navigate to Project home directly (XML located) through DOS/Command prompt and then type as below:
Note: before that you need to set class path as bin directory of Project home folder. Command : “set classpath=C:\Selenium\project\bin;” as an example for binary file
Note1: similarly lib files, Command : “set classpath=C:\Selenium\project\lib\*” as an example
or command: set classpath=C:\Selenium\project\bin;C:\Selenium\project\lib\*;
Then execute testng.xml file through command prompt
command : java org.testng.TestNG testng.xml
Batch file creation for Jenkin execution
Create a batch file using notepad – steps
- Type the below contents in Notepad without quotes.
‘ java -cp bi;lib/* org.testng.TestNG testng.xml ‘
2) save the notepad as “runSelenium.bat” which will create a bat file. Place it in project home directory.
Jenkins job creation:
- Open Jenkins, then click on “New item”
- Enter item name as “JenkinSeleniumTest”
- Select “Freestyle project” , click Ok
- Then click on “Advance” button under the “Advance projects options”
- Then select “Use custom workspace”, then give the project home directory path in “Directory” field.
- Then “Add build setup” under “Build” and select “Execute Windows batch Command”
- Then type “runSelenium.bat” in the command field, click apply and save it.
- Then in the “JenkinSeleniumTest”, then click on “Build Now” which will call the batch file and then Selenium script internally.
- Verify the “Console output” in “JenkinSeleniumTest” which will show the Selenium execution results.