- 29 Mar 2023
- 4 Minutes to read
- Print
- DarkLight
Running Tests using Jenkins
- Updated on 29 Mar 2023
- 4 Minutes to read
- Print
- DarkLight
Overview
Jenkins is an open-source continuous integration/continuous delivery and deployment (CI/CD) automation software DevOps tool. It is used to implement CI/CD workflows, called pipelines. Users can use Jenkins to execute automation on the pCloudy platform to enable CI/CD.
Pre-requisites
- The user should be registered on the pCloudy platform
- Jenkins installed on the local machine
- Maven and JDK path should be configured in Jenkins
- Maven should be installed on the local machine
- Set the required capabilities in the Appium script
- Copy the project path and keep it handy
Let's see the Approaches
- Freestyle
- Pipeline
Steps to run jenkins using the Freestyle approach
- Go to Dashboard and Click on New Item
- Enter “project name”, select “Freestyle project” and click “OK”
- Under General scroll down and Go to Build and click on the Add build step
- Select "Execute Windows batch command" for windows or Execute Shell from the drop-down
Note: This needs to be selected depending on OS
- Enter the maven command to run the project and save it
Note: Here user needs to give the path of the project where it is located
E.g. cd C:\Users\Admin\eclipse-workspace\Chapter4-TestNgwithpCloudyonSingleDeviceIOS2
mvn test
Note: Make sure pCloudy capabilities should be set in Runner.java
- Set app capability
**Steps to Set App Capability **- Login to your registered account.
- On the "Start page" navigate to "Capabilities" in the tools section.
- On the Capabilities Configurator Page, you can select OS and the Automation Type.
- Next, you can you can enable different capabilites like Capturing Logs, Enabling local testing or Wildnet, Capturing Video, and capturing Performance Data.
- Next, you can you can enter the Device Details such as region, Device Full name, Manufacturer or Device Version.
- You can select the Device Full Name from the drop down of available devices. However, it is advisable to select the Device Manufacturer and Version instead of the full name of the device to avoid failure of execution due to non-availability of a specific device that you might select in the Device Full name field. Providing a Device Manufacture and Version, gives you a broader reach when your script starts to look for available devices on the platform.
- Once you have selected the Device Details, simply fill in the duration of the execution in mins, selection the App from the drop down, please ensure that you have uploaded the app beforehand on the My Data section. The app package and activity.
- Click on Generate Capabilites, once all the details are filled in.
- The Capabilities will get generated according to the details and specifications that the user mentions. A success notification will appear as well.
- Now you can simply click on the Copy icon to copy the Capabilites and paste it in your appium script and execute the program.
- To startover for a different specification or scenario, simply hit the reset button on the bottom and follow the same steps mentioned above.
- Click on "Build Now" and go to console output to check the status.
- Click on “Console Output”
Steps to run jenkins using the Pipeline approach
Prerequisite:
- Install jq package (This package helps to get the output response)
- jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text. It is written in portable C.
- Maven should be configured in Jenkins
- User should have a Github account: this is needed incase if the repository is private
Note: This sample project is for reference with respect to the ubuntu machine
Steps
- Create a GitHub account and upload your project
- For the public repository, users don't need to configure Github in Jenkins
- For a private repository, users need to configure Github in Jenkins
- Login into your Jenkins and click on “New Item”
- Enter the Project name and Select pipeline style.
- If the Project in Github is private, then the user needs to configure Github credentials in Jenkins.
Steps to access Github from Jenkins:-- once the build is created, go to the Jenkins dashboard and click on pipeline syntax to configure GitHub credentials
- In the “sample step” select the “Git” option from the dropdown.
- Enter the repository URL from GitHub.
- Enter the branch name
- Configure GitHub credentials in Jenkins
Note: If Git is not working from the password, generate a git token from GitHub and place it in the password field.
- Click on Generate pipeline script.
Note: Above process is used to clone the Git repository to the Jenkins workspace.
- Go to the Dashboard and click on the created build and select configure
- Write the code to run the pipeline script and click on SAVE
pipeline {
agent any
stages {
stage('Clonning Git') {
steps {
git credentialsId: '', url: 'https://github.com/satyamraii/pCloudySample.git'
}
}
stage('Authentication') {
steps {
script {
env.authtoken = sh( script: "curl -u : https://device.pcloudy.com/api/access | jq -r .result.token",returnStdout: true).trim()
echo "My authotoken: ${env.authtoken}"
}
}
}
stage('UploadFile') {
steps {
sh "curl -X POST -F file= -F source_type=raw -F token=${env.authtoken} -F filter=all https://device.pcloudy.com/api/upload_file"
}
}
stage('runtestcases') {
steps {
dir(env.WORKSPACE){
sh "mvn test"
}
}
}
}
}
Code definition:- There are four stages as mentioned below:-
- Jenkins stage 1:- Git clone-> For example, we have considered pCloudy-sample-project chapter-1: Which is for a single device(Android)
Here is a reference link for the same.
Note: We have updated the capabilities in GitHub as well.
- Jenkins stage 2:- In this stage, we are using the authentication API which is present in content.pcloudy.com
Here the user needs to enter the email and API key to generate the token.
Note: The user can also use the Jq package to help extract the token value from the output
Jenkins stage 3:- Upload stage-> The user needs to enter the local file path in the API
Jenkins stage 4 :- Run mvn test.
- Run the build.