Search code examples
javascriptjasmineprotractorjasmine-nodejasmine2.0

Protractor writing element inner text to a file


I am trying to grab an elements inner text and write the inner text to a file however i am having no success. the program runs and writes to csv file but just writes the what the global variable equals instead of the inner text of the element this is what I have done:

Config file with globals:

enter image description here

it('Get CycleStatus, Paydate, Weeknumber, Clientid - completed', function () {

      //https://stackoverflow.com/questions/44362880/protractor-if-else-with-expectcondition
      var cycle = $('#cycleStatusID');
      browser.wait(EC.elementToBeClickable(cycle), 20000).then(function() {
          cycle.getText().then(function(text){
            if (['Cycle Complete',
                 'Entering Payroll Information',
                 'Correcting Input',
                 'UnderReview'].indexOf(text) >= 0) {
                   cycleStatus= text;
                   console.log(cycleStatus+ ' - Displayed');
                 } else {
                   cycleStatus= 'Cycle unknown';
                   console.log(cycleStatus);
                 }
          });
      });


        var fs = require('fs');
        fs.appendFile('/Users/hoflerj/Desktop/Protractor/WFN/psreport.csv',cycleStatus, function (err) {
          if (err) throw err;
          console.log('write complete!');
        });


      });//spec function

enter image description here

seems like it is writing before it is actually storing the info in the variable it should be Entering payroll Information then say write complete.


problem it is writing to file but putting it in all one column.

 const fs = require('fs');
              const cycle = $('#cycleStatusID'); // cycle status
              const client = $('#clientID'); // clientid
              const writeValueToFile = (value) => {
                  return fs.appendFile('/Users/hoflerj/Desktop/Protractor/WFN/psreport.csv', value + ";" + "hellow world", function (err) {
                      if (err) throw err;
                      console.log('write complete!');
                  });
              }

Solution

  • So I think you have a misunderstanding how promises and variable scoping work.

    const fs = require('fs');
    
    const cycle = $('#cycleStatusID');
    
    const writeValueToFile = (value) => {
        return fs.appendFile('/Users/hoflerj/Desktop/Protractor/WFN/psreport.csv', value, function (err) {
            if (err) throw err;
            console.log('write complete!');
        });
    }
    
    const acceptableValues = ['Cycle Complete', 'Entering Payroll Information', 'Correcting Input', 'UnderReview'];
    
    // Cleaned your code up
    browser.wait(EC.elementToBeClickable(cycle), 20000)
        .then(() => {
            const cycleStatus;
            cycle.getText()
                .then((text) => {
                    if (acceptableValues.indexOf(text) >= 0) {
                        cycleStatus = text;
                        console.log(cycleStatus+ ' - Displayed');
                    } else {
                        cycleStatus = 'Cycle unknown';
                        console.log(cycleStatus);
                    }
                })
                .then(() => {
                    writeValueToFile(cycleStatus);
                });
        });
    
    //Here it is cleaner but could be confusing for you since you don't fully understand promises yet.
    browser.wait(EC.elementToBeClickable(cycle), 20000)
        .then(() => {
            return cycle.getText()
                .then((text) => {
                    if (acceptableValues.indexOf(text) >= 0) {
                        return text;
                    } else {
                        return 'Cycle unknown';
                    }
                })
                .then(writeValueToFile);
        });
    

    EDIT: Promise: The way the code was originally written, you code called the browser.wait() and never ran any of the code in your then block, executed your write function, then it ran the code in the then block. This caused your variable to be undefined. because it was set after the write function is being ran. This is a great article I used when I was first trying to understand promises https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html

    You could also have a variable hoisting issue but maybe not. I see you are setting the variable for cycleStatus inside your then block but never see where you define it. This could lead to a global variable or that variable being undefined when you get to the part where you are writting to a file because the variable was never defined in the current scope. https://www.designingforscale.com/understanding-hoisting-in-javascript/