Data driven testing using csv in Protractor



  • I am trying to run data-driven testing using csv file in protractor:

    I have created a read csv promise module:

        let readSync = async function (path1) {
        console.log(path1)
          const csv = require('csv-parser')
          const fs = require('fs')
          const results = [];
    
      function getIdRequest(path2) {
        console.log(path2)
        return new Promise((resolve, reject) => {
    
         
          fs.createReadStream(path2)
            .pipe(csv(path2))
            .on('data', (data) => results.push(data))
            .on('end', () => {
              resolve(results);
            })
        });
      }
      console.log(path1)
      return getIdRequest(path1);
    }
    // to get the request body 
    
      module.exports = readSync;
    
    
    <p><strong>Now i am calling the same from my test file:</strong></p>
    <pre class="lang-javascript prettyprint-override"><code>    let readCSV = require('../commons/readSync.js');
        
        describe('Validate stage 1 behaviour', function () {
        
          let testdata=await readCSV(browser.params.testFile);
        
          for (let a of testdata) {
         
            it('test {Regression} {Sanity} {Sanity}', async function () {
              //check title
              console.log("inside")
              await mainPage.goto();
              await mainPage.clickElement('inputField')
    

    But i cannot use await inside describe, so I tried using :

        let readCSV = require('../commons/readSync.js');   
    
    describe('Validate stage 1 behaviour', function () {
    
      readCSV(browser.params.testFile).then(function(testdata){
    
      for (let a of testdata) {
        console.log("outside")
        it('test {Regression} {Sanity} {Sanity}', async function () {
          //check title
          console.log("inside")
        });
    
      }
    
    
    <p>but in this,  'it' function is not getting executed, its printing only 'outside'</p>
    <p><strong>Challenges</strong></p>
    <ol>
    <li><p>Cannot use await inside describe as it accepts only synchronous function</p>
    </li>
    <li><p>Cannot use <code>promise.then(describe(function(){}</code> because 'IT' and 'Describe' is not recognized when called inside the callback. I get 0 spec executed message.</p>
    </li>
    <li><p>Tried calling the <strong>testdata</strong> in before hooks but this also fails as the for loop is outside the It block . So, the compiler tries to run for loop even before 'before hook' is executed .</p>
    </li>
    <li><p>I tried using nested describe and calling await testdata in before each and before all . But even that fails with the same reason , refer:
    <a href="https://stackoverflow.com/q/48344674/6793637">https://stackoverflow.com/q/48344674/6793637</a></p>
    </li>
    </ol>
    <p><strong>Questions:</strong></p>
    <ol>
    <li><p>So is there a better way to run data driven testing using csv in
    protractor ?</p>
    </li>
    <li><p>Is there a way to synchronously make the suggest solution work?</p>
    </li>
    <li><p>Why is describe or IT block doesn't work inside a callback ? Eg Promise.then(describe()) won't execute describe</p>
    </li>
    </ol>


  • Hi i found another csv module : csv-parse : https://csv.js.org/parse/ (Its used in postman)

    const parse = require('csv-parse/lib/sync');
    

    Use this module for data-driven testing instead of below-step

    Below method is deprecated:

    Made it work.

    Please suggest any better ways if anyone knows:

    Solution:

    Install system-sleep module:

    npm install system-sleep
    

    .............Updated..........

    Now run your tests as:

    let testdata=readSync(browser.params.testFile);
    var sleep = require('system-sleep');
    
    describe('Validate dfsfdsf 1 behaviour', function () {
    
    //Assign the value when promise gets resolved
    //This is asynchronous so we will add a implicit wait
    testdata.then(function(b){testdata=b}); 
    
    //sleep till the test resolves
      while(typeof testdata.then === 'function'){
        sleep(1)
      }     
    
      for(let a of testdata){
        it('test {Regression} {Sanity} {Sanity}', async function () {
          console.log(a)
        });
      }
    

    Update:

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

    Happy days guys I have created an npm package for this, anyone is free to use it

    Now data driven testing is as simple as :

    let parser= require('csv-parser-sync-plus-promise');
    let testdata = parser.readCsvSync(browser.params.testFile);
    
    
    
    describe('Validate dfsfdsf 1 behaviour', function () {
    
    
      for(let a of testdata){
        it('test {Regression} {Sanity} {Sanity}', async function () {
          console.log(await parser.readCsvPromise(browser.params.testFile))
        });
      }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2