Trade Me-14-Jenkins-CI-process | Postman API automation testing

API automation testing CI process

Typical transactions selection -> Corresponding API selection -> Scripting -> Scenarios and reports -> CI(I’m here)

Typical transactions selection

Typical transactions selection refers to: Trade Me-2-typical transactions

Corresponding API selection and scripting

Corresponding API selection and scripting refers to:

Scenarios and reports

Scenarios and reports refer to: Trade Me-13-scenarios-and-reports .

Jenkins CI process Practice

  • Steps of Jenkins CI process

Preparations -> New a CI job -> Job configuration -> Job running -> Job results checking -> Email to stakeholders
Jenkins installation refers to: How to install Jenkins?
Git Bash installation refers to: How to install Git Bash?
  • Preparations - environment initiation

Initialize your local working folders.
rem Initialize your local working folders
pushd d:\Software && md postman\collections postman\data postman\reports\hisreports postman\postman_github

Set the Postman home directory to system environment parameter
rem set your postman home directory, this example is 'D:\Software\postman'
setx "POSTMAN_HOME" "D:\Software\postman" /M

rem install Newman-related and email-related Nodejs module
pushd %POSTMAN_HOME% && npm init -y && npm install -g newman && npm install -g newman-reporter-html && npm install moment && npm install nodemailer --save
  • Preparations - data initiation

Clone demo GitHub repository
rem clone demo GitHub repository 
pushd %POSTMAN_HOME%\postman_github && git init && git clone https://github.com/ifulltest/postman.git

Copy GitHub demo collections and data to your local working directory
rem copy GitHub demo collections and data to your local working directory
copy %POSTMAN_HOME%\postman_github\postman\collections\* %POSTMAN_HOME%\collections\ && copy %POSTMAN_HOME%\postman_github\postman\data\* %POSTMAN_HOME%\data\*

  • Preparations - data replacing or adding

Key messages replacing in collection files

“key”: “token”,“value”: “your token”, “key”: “tokenSecret”,“value”: “your tokenSecret”, “key”: “consumerSecret”,“value”: “your consumerSecret”, “key”: “consumerKey”,“value”: “your consumerKey”,

oauth_consumer_key="your consumerKey", oauth_token="your token", oauth_signature="your consumerSecret%26your tokenSecret"

Key messages replacing in nodemailer.js file

user: “sender email”, pass: “sender email token”

from: “sender email”, to: “receiver email”,

Key messages adding in ‘%POSTMAN_HOME%\reports\hisreports’ folder

Find two files that creating date more than seven days ago,

and name them with newmanrpt-his.html and newmanrpt-his.txt.

Then put them to the given folder.

Key messages adding in ‘%POSTMAN_HOME%\postman_github\postman.git’ file
[core]
	...
[remote "origin"]
	url = https://Github username:personal token@github.com/ifulltest/postman.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	...

How to get GitHub personal token and set it to local Windows server?

  • New a CI job

Start Jenkins by self-defined port number: java -jar jenkins.war –httpPort=8088

……

Create a Freestyle project

The example project named as: ‘TradeMe-API-Automation-Practice’

  • Job configuration - General

Discard old builds

  • Job configuration - Build - archive reports

Build Windows batch command, archive Newman reports that creating for more than seven days to the history folder.

rem Archive Newman reports that creating for more than seven days to the hisreports folder.
pushd %POSTMAN_HOME% && ping /n 3 127.0.0.1>nul
copy /y /b reports\hisreports\newmanrpt-his.html "%POSTMAN_HOME%\reports" && forfiles /p "%POSTMAN_HOME%\reports" /m *.html /d -7 /c "cmd /c move @path %POSTMAN_HOME%\reports\hisreports">nul && copy /y /b reports\hisreports\newmanrpt-his.txt "%POSTMAN_HOME%\reports" && forfiles /p "%POSTMAN_HOME%\reports" /m *.txt /d -7 /c "cmd /c move @path %POSTMAN_HOME%\reports\hisreports">nul
Before the archive operation, two files in the reports folder creating for more than 7 days will expected to be archived.

Run job and see the Jenkins log
Started by user GaryGao
Running as SYSTEM
Building on master in workspace D:\Software\Jenkins\workspace\TradeMe-API-Automation-Practice
[TradeMe-API-Automation-Practice] $ cmd /c call C:\Users\ADMINI~1\AppData\Local\Temp\jenkins7423834600478251788.bat

D:\Software\Jenkins\workspace\TradeMe-API-Automation-Practice>rem Archive Newman reports that creating for more than seven days to the hisreports folder. 

D:\Software\Jenkins\workspace\TradeMe-API-Automation-Practice>pushd D:\Software\postman   && ping /n 3 127.0.0.1 1>nul 

D:\Software\postman>copy /y /b reports\hisreports\newmanrpt-his.html "D:\Software\postman\reports"   && forfiles /p "D:\Software\postman\reports"  /m *.html /d -7 /c "cmd /c move @path D:\Software\postman\reports\hisreports"   && copy /y /b reports\hisreports\newmanrpt-his.txt "D:\Software\postman\reports"   && forfiles /p "D:\Software\postman\reports"  /m *.txt /d -7 /c "cmd /c move @path D:\Software\postman\reports\hisreports" 
        1 file(s) copied.

        1 file(s) moved.
        1 file(s) moved.
        1 file(s) moved.
        1 file(s) copied.

        1 file(s) moved.

D:\Software\postman>exit 0 
Finished: SUCCESS
After the archive operation, the given two files are archived in the hisreports folder.

Note: ‘newmanrpt-his.html’ and ‘newmanrpt-his.txt’ in the hisreports folder are two files created more than 7 days ago, making sure the archiving loop operation is not failed.

  • Job configuration - Build - generate cli & html reports

Build jobs for generating both positive and negative case CLI & HTML reports.

rem Using Newman to generate positive case CLI & HTML reports, dynamic report name stores in a temporary file.
pushd %POSTMAN_HOME%
if %time:~0,2% leq 9 (set h=0%time:~1,1%) else (set h=%time:~0,2%)
set y=%date:~0,4%& set m=%date:~5,2%& set d=%date:~8,2%& set mi=%time:~3,2%& set s=%time:~6,2%& set ms=%time:~9,2% && ping /n 3 127.0.0.1>nul
newman run collections\tradeMeSandBox.postman_collection.json -e data\tradeMeSandBox.postman_environment.json -r cli,html --reporter-html-export reports\newmanrpt-%y%%m%%d%%h%%mi%%s%.html>reports\newmanrpt-%y%%m%%d%%h%%mi%%s%.txt & echo |set /p="newmanrpt-%y%%m%%d%%h%%mi%%s%">%POSTMAN_HOME%\data\fn.txt & exit 0
rem Using Newman to generate negative case CLI & HTML reports, dynamic report name stores in a temporary file.
pushd %POSTMAN_HOME%
if %time:~0,2% leq 9 (set h=0%time:~1,1%) else (set h=%time:~0,2%)
set y=%date:~0,4%& set m=%date:~5,2%& set d=%date:~8,2%& set mi=%time:~3,2%& set s=%time:~6,2%& set ms=%time:~9,2% && ping /n 3 127.0.0.1>nul
newman run collections\tradeMeSandBox.postman_collection-negative.json -e data\tradeMeSandBox.postman_environment.json -r cli,html --reporter-html-export reports\newmanrpt-%y%%m%%d%%h%%mi%%s%-neg.html>reports\newmanrpt-%y%%m%%d%%h%%mi%%s%-neg.txt & echo |set /p="newmanrpt-%y%%m%%d%%h%%mi%%s%-neg">%POSTMAN_HOME%\data\negfn.txt & exit 0
Run job and see the Jenkins log

After the job, CLI & HTML reports and temporary data files are generated.

  • Job configuration - Build - upload local reports to remote GitHub repository

Copy newly generated Newman report to local GitHub repository

rem copy newly generated Newman report to local GitHub repository
pushd %POSTMAN_HOME%
set /P fn=<data\fn.txt && set /P negfn=<data\negfn.txt
copy /y reports\%fn%.* postman_github\postman\reports && copy /y reports\%negfn%.* postman_github\postman\reports
Run job and see the Jenkins log

After the job, given reports have been copied to local GitHub repository

Commit given reports local GitHub to public GitHub repository

rem Commit given reports local GitHub to public GitHub repository
pushd %POSTMAN_HOME%
set /P fn=<data\fn.txt && set /P negfn=<data\negfn.txt
pushd %POSTMAN_HOME%\postman_github\postman\reports && git pull origin main && git add %fn%.* %negfn%.* && git commit -m "txt and html report" && git push -u origin main
Run job and see the Jenkins log

After the job, given reports have been committed to public GitHub repository

  • Job configuration - Build - Email to stakeholders

rem Email to stakeholders the given testing report
pushd %POSTMAN_HOME% && node collections\nodemailer.js && del %POSTMAN_HOME%\data\*fn.txt

Run job and see the Jenkins log

  • How to use Node.js realize the email function

The above example mainly uses node.js the ‘nodemailer’ module to realize the email function.

Install the Node.js in the Postman home directory or directly move the ‘node_moudules’ in.

How to install Node.js please refer to: How to install Node.js

Extended module ‘nodemailer’ and ‘moment’ installation have been included in ‘Preparations - environment initiation’.

Moule installation command:

npm install nodemailer or yarn add nodemailer –save

npm install moment or yarn add moment

Email realizing by ‘nodemailer.js’ - get current time for later email title to use.
// moment module which can get current time for later email title to use.
const moment = require('moment')

let currentTime =  moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
Email realizing by ‘nodemailer.js’ - file operation and concatenate the email content.
// file systme module which can operate the file
const fs = require("fs");

const path_fn = "D:/Software/postman/data/fn.txt"; // file path

const isExist_fn = fs.existsSync(path_fn); // check whether file exist

const filedata_fn = isExist_fn ? fs.readFileSync(path_fn, "utf-8") : "";

const fs_neg = require("fs");

const path_negfn = "D:/Software/postman/data/negfn.txt";

const isExist_negfn = fs_neg.existsSync(path_negfn);

const filedata_negfn = isExist_negfn ? fs_neg.readFileSync(path_negfn, "utf-8") : "";

console.log("Positive cases report name: "+filedata_fn);

console.log("Negative cases report name: "+filedata_negfn);

// Concatenate the email content
var arr=new Array();
arr.push("<p><font size='5'>Positive cases HTML report: </font></p>");

arr.push("<p><a href=https://ifulltest.github.io/postman/reports/"+filedata_fn+".html><b><font size='4'>https://ifulltest.github.io/postman/reports/"+filedata_fn+".html</font></b></a></p>");

arr.push("<p><font size='5'>Positive cases TXT report: </font></p>");

arr.push("<p><a href=https://ifulltest.github.io/postman/reports/"+filedata_fn+".txt><b><font size='4'>https://ifulltest.github.io/postman/reports/"+filedata_fn+".txt</font></b></a></p>");

arr.push("<p><font size='5'>Negative cases HTML report: </font></p>");

arr.push("<p><a href=https://ifulltest.github.io/postman/reports/"+filedata_negfn+".html><b><font size='4'>https://ifulltest.github.io/postman/reports/"+filedata_negfn+".html</font></b></a></p>");

arr.push("<p><font size='5'>Negative cases TXT report: </font></p>");

arr.push("<p><a href=https://ifulltest.github.io/postman/reports/"+filedata_negfn+".txt><b><font size='4'>https://ifulltest.github.io/postman/reports/"+filedata_negfn+".txt</font></b></a></p>");

var htmltxt = arr.join("");

console.log("htmltxt: "+htmltxt);
Email realizing by ‘nodemailer.js’ - sending the email
// Delay a specific time to ensure stakeholders can access the GitHub report details after receiving the email.
sleep(60000);
console.log("Delay a specific time to ensure stakeholders can access the GitHub report details after receiving the email.");
	
// nodemailer module which can operate email
var nodemailer = require("nodemailer")

var transport = nodemailer.createTransport({
	
    host: "smtp.gmail.com",
	
    secureConnection: true,
	
    port: 465,
	
    auth:{
		
        user: "sender email",
		
        pass: "sender email token"
		
    }
	
})

var mailOptions = {
	
    from: "sender email",
	
    to: "receiver email",
	
    subject: "Daily env check: "+currentTime,
	
    text: "Hello-text",
	
    html: htmltxt,
	
}

transport.sendMail(mailOptions, function(err, response) {
	
	if(err) console.log(err)
		
    else console.log(response)
	
})

// sleep time: mm
function sleep(delay) {
  for (var t = Date.now(); Date.now() - t <= delay;);
  return sleep;
}
  • Other CI practices that waiting to be extended

The above CI example uses the Windows batch command to realize the CI process.

Using ‘Execute shell’ and ‘Pipe line’ mode to realize the CI process will be displayed on later blog.

Copyright

This work is licensed under CC BY-NC-ND 4.0 .