Data driven testing per request without using data file

Create a environment variable called “csv” and copy the below content and paste it as value:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

Now in pre-request add :

//we use variables as it will get destroyed after the run . So if index returns null then it means it’s the first iteration
if (!pm.variables.get("index")) {
    const parse = require('csv-parse/lib/sync')
//Environmental variable where we copy-pasted the csv content
const input = pm.environment.get("csv");
const records = parse(input, {
columns: true,
skip_empty_lines: true
})
    pm.variables.set("index", 0)
pm.variables.set("records", records)
}
records = pm.variables.get("records")
index = pm.variables.get("index")
//we are setting variable 
if (index !== records.length) {
for (let i of Object.entries(records[index])) {
pm.variables.set(i[0], i[1])
}
pm.variables.set("index", ++index)
pm.variables.get("index")===records.length?null:postman.setNextRequest(pm.info.requestName)
}

Now you can run data driven for that one particular request:

Note that here we used pm.variables. this is because it creates local variable that get destroyed after the collection run completes .

So for each run , first value will be null and then will be set to 0

Eg collection:

https://www.postman.com/collections/eb144d613b7becb22482

use the same data as environment variable content , now run the collection using collection runner or newman.

You can import this collection by clicking file >import >and then choosing links

Output:

Reading CSV file in Postman

CSV:

a,b,c
1,2,3
1,5,6
2,4,6

CSV parse output:

Approach 1: store CSV content as an environmental variable

Just copy the CSV file content and create an environmental variable and paste the content inside it

Then in test or pre-requisite step:

console.log(pm.environment.get("data"))
const parse = require('csv-parse/lib/sync')
//Environmental variable where we copy-pasted the csv content
const input =pm.environment.get("data");
const records = parse(input, {
 columns: true,
 skip_empty_lines: true
})

console.log(records)

Approach 2: Passing the CSV content to an environmental variable through Newman (Powershell):

Then in test or pre-requisite step use the same code as above:

> $a= Get-Content -raw .\1.csv
> newman run .\test.postman_collection.json -e .\test.postman_environment.json -k -r "cli,htmlextra" --env-var data=$a

Protractor Data-Driven Testing

Now Drive your test using test data

Now you can drive your protractor tests using CSV data:

Demo CSV: (just copy it to notepad and save as 1.csv)

a,b,c
1,2,3
1,5,6
2,4,6

Install csv-parser node module:

run it under the protractor project so that it will be installed as project’s local module ( Run the command where the package.json file is present)

npm install csv-parse

Output after parsing:

This module will parse the csv as below column as the key and value as the value on the specific row

Data-driven testing:

'use strict';
//use fs to read file
let fs = require('fs');
//use csv-parse sync to parse the file : https://csv.js.org/parse/
const parse = require('csv-parse/lib/sync');
//read the csv file
let a = fs.readFileSync('1.csv');
//parse the csv file
const testdata = parse(a, {
columns: true,
skip_empty_lines: true
})
console.log(testdata);
describe('Validate dfsfdsf 1 behaviour', function () {
for (let i of testdata) {
it('test {Regression} {Sanity} {Smoke}', async function () {
console.log(i);
console.log(i.a);
console.log(i.b);
console.log(i.c);
expect(Number(i.a) + Number(i.b)).toBe(Number(i.c))
});
}
});

Ignore below steps:

Deprecated:

npm package:

https://www.npmjs.com/package/csv-parser-sync-plus-promise

Usage:

importing:

let parser = require('csv-parser-sync-plus-promise')

Use as sync:

let a=parser.readCsvSync('')

Use as Promise:

let b=parser.readCsvPromise('')
it('test {Regression} {Sanity} {Sanity}', async function () {
console.log(await b);
});

Protractor test:

Use the demo csv ‘1.csv’

'use strict';
let parser = require('csv-parser-sync-plus-promise')
let testdata=parser.readCsvSync('<full_path>/1.csv');
describe('Validate dfsfdsf 1 behaviour', function () {

for(let i of testdata){
it('test {Regression} {Sanity} {Sanity}', async function () {
console.log(i.a);
console.log(i.b);
console.log(i.c);
expect(Number(i.a)+Number(i.b)).toBe(Number(i.c))
});
}
});

Output: