0

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!');
                  });
              }
Jonathan
  • 395
  • 2
  • 8
  • 25
  • I believe the problem is that you are setting `cycleStatus` but writing `cyclestatus` to the file. You could also improve this code by removing the if statement and directly passing `text` to the `cycleStatus` variable. – M. Hudson Dec 12 '17 at 17:00
  • Like that I just edited my post – Jonathan Dec 12 '17 at 17:04
  • can you give me an example of setting text to cycle statuts do you mena something like this cycleStatus = text ? – Jonathan Dec 12 '17 at 17:05
  • okay now when it write to the excel file it write undefined. Is there any to resolve this for it to actually write the text? I have been working on this for 2 days any addition help is appreciated – Jonathan Dec 12 '17 at 17:08
  • Did changing the misspelling fix the issue? – M. Hudson Dec 12 '17 at 17:09
  • No it didn't the console said write complete but in the cab file the program wrote the word undefined – Jonathan Dec 12 '17 at 17:14

2 Answers2

1

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/

rcodonnell
  • 26
  • 2
  • I can add more details and explanation later tonight, just wanted to get this to you now. – rcodonnell Dec 12 '17 at 20:43
  • The code worked! now i am going to spend all night studying and doing research so I can get this down. Also please add more details and explanation as well. – Jonathan Dec 12 '17 at 21:03
  • Thank for the article okay I have one more question. I am getting the text of another variable and writing it to file it is working and so now i have 2 statements written to the file however they are not separated in separate columns. I know i have to use comma separated values like foo+";"+ foo2 and ect... however how can i send them all at once to my csv writer?... usually i assign values to all the variables and then send them as comma separated values to my fsv file writer like cyclestaus+ ";" + clientid and ect – Jonathan Dec 12 '17 at 21:58
  • what happening is that its writing it all in one column in the csv file instead of seperate column in the csv file like row 1 col 1 and row 1 col 2. I looked this up and found this https://stackoverflow.com/questions/24915609/how-to-write-to-csv-file-in-javascript however it is not working..any sugestions – Jonathan Dec 13 '17 at 14:23
  • again it writing to the file problem is it is not separating the data into separate columns.. its like the values need to be split. – Jonathan Dec 13 '17 at 14:24
0

The reason you're now getting "undefined" is that you're also misspelling cycleStatus in the global declaration. If you fix that though, you're just going to end up back at where you started.

If you output cycleStatus rather than a custom message to the console, you might be able to debug better. You're also using thousand separators in your browser.wait, removing them may or may not fix the problem. I think you need to tidy this up a bit so you can see what you're doing.

Try this:

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);
           }
    });
});

If this does not work when you put it in context, you may have to use a promise to return cycleStatus to the csv writer.

M. Hudson
  • 889
  • 5
  • 11
  • I am at lunch at work but after I come back I will try this solution and get back to you stay tuned. And thx – Jonathan Dec 12 '17 at 17:30
  • Can you give me an example of a promise to pass to the csv writer just in case after I put it in context it doesn't work? – Jonathan Dec 12 '17 at 17:33
  • Okay it still wrote undifined to the csv file – Jonathan Dec 12 '17 at 18:58
  • I also change my global to global.cycleStatus="cycleStatus"; – Jonathan Dec 12 '17 at 19:07
  • You know I think it writing to file before it is doing the logic in my console it is saying write complete and it saying Entering payroll Information - Displayed I say this because after i allow the function to complete and go to the next spec i have written in my csv file (undefinedEntering Payroll information) – Jonathan Dec 12 '17 at 19:52