In today’s newsletter we are going to discuss
Introduction
Add Maven parameter & execute tests using Command line.
Jenkins Installation & Setup
Jenkins Parameterization
Parameterizing Headless Browser
Schedule Jenkins Jobs
Introduction
Jenkins serve as Continuous Integration (CI) and Continuous Delivery (CD) tools to facilitate the automation of Selenium test suites.
Maven Parameter from Command line
In order to accept the maven command(e.g. mvn test -Dbrowser=chrome) from command line , we need to make code changes as shown below.
We will try to read the property “browser” from maven command. If that is not available then will try to read from Property file.
Test the application by running it from command line.
Jenkins Installation & Setup
Prerequisites:
A Windows operating system (Windows 7 or later is recommended).
A Java Runtime Environment (JRE) or Java Development Kit (JDK) installed. Jenkins requires Java to run. You can download Java from the official Oracle website or use OpenJDK.
Here are the steps to install Jenkins in Window machine.
Download Jenkins
Go to the official Jenkins website at https://www.jenkins.io/download/ and download the Windows installer package.Go to the path where this war file downloaded and open the command prompt.
Browse to “http://localhost:8080” and click on “Create an account”
Once Account is created, click on “New Item”.
Enter Job name and select “Freestyle Project” and click “ok”.
We can either use Github repo link or we can provide local repository path under “Use custom workspace”. Then under “Build”, select “Invoke top level Maven Targets” and provide maven command “test -Dbrowser=chrome”
Even though full Maven command is “mvn test -Dbrowser=chrome”, here we don’t need to specify “mvn”.
Now our Job is ready to be build. Click on “Build now” and it will start building automation project.
To view the latest build, click on “Console Output“ to view the logs.
Jenkins Parameterization
So far we are hardcoding “browser” parameter, which will always open the “chrome” browser.
We can improve upon this and make it customizable in Jenkins so that without code changes or doing any changes in Jenkins pipeline we can run different build on different browsers.
In “General” setting, click on “This project is parameterized” and select “choice parameter” and enter your choices.
Add above choice parameter name in build step where maven command is given.
Click on “Build with parameter” and select “Firefox” and click build. It will start build and start executing tests using Firefox browser.
Parameterizing Headless Browser
Similarly how we did parameterizing “browser”, we can also parameterize “headless” parameter to run tests either in headless mode or regular mode.
Create Boolean parameter in Jenkins under “General → Boolean Parameter” and select default value as “true” or “false”.
Make sure to update maven command to add headless parameter as shown below.
Schedule Jenkins Job
Let’s say, we want to automatically trigger build at 6 AM everyday. This can be achieved by scheduling Jenkins jobs. Instruction can be given to Jenkins in the form of cron expression and it will automatically trigger the build without any human intervention.
Go to Configure, click on Build triggers, and setup the schedule as shown below.
You can read more about cron expression here.
Thank you for the post.Did you also try by running Jenkins using docker container?
Very Helpful 👍