1

I'm trying to create a report for my scenario, I want to execute some validations and add the retults in a string, then, write this string in a TXT file (for each validation I would like to add the result and execute again till the last item), something like this:

it ("Perform the loop to search for different strings", function() 
{        
browser.waitForAngularEnabled(false);

browser.get("http://WebSite.es");

//strings[] contains 57 strings inside the json file

    for (var i = 0; i == jsonfile.strings.length ; ++i) 
    {
        var valuetoInput = json.Strings[i];
        var writeInFile;
        browser.wait;
        httpGet("http://website.es/search/offers/list/"+valuetoInput+"?page=1&pages=3&limit=20").then(function(result) {
            writeInFile = writeInFile + "Validation for String: "+ json.Strings[i] + " Results is: " + expect(result.statusCode).toBe(200) + "\n";
        });

        if (i == jsonfile.strings.length)
        {
            console.log("Executions finished");
            var fs = require('fs');
            var outputFilename = "Output.txt";
            fs.writeFile(outputFilename, "Validation of Get requests with each string:\n " + writeInFile, function(err) {
                if(err) 
                {
                    console.log(err);
                }
                else {
                    console.log("File saved to " + outputFilename);
                }
                });
        }
      };
});

But when I check my file I only get the first row writen in the way I want and nothing else, could you please let me know what am I doing wrong?

*The validation works properly in the screen for each of string in my file used as data base

**I'm a newbie with protractor

Thank you a lot!!

Alexandre
  • 432
  • 1
  • 3
  • 14

1 Answers1

0

writeFile documentation

Asynchronously writes data to a file, replacing the file if it already exists

You are overwriting the file every time, which is why it only has 1 line.

The easiest way would probably (my opinion) be appendFile. It writes to a file without overwriting existing data and will also create the file if it doesnt exist in the first place.

You could also re-read that log file, store that data in a variable, and re-write to that file with the old AND new data included in it. You could also create a writeStream etc.

There are quite a few ways to go about it and plenty of other answers on SO specifically on those functions that can provide more info.

Node.js Write a line into a .txt file

Node.js read and write file lines

Final note, if you are using Jasmine you can also create a custom jasmine reporter. They have methods that contain exactly what you want (status Pass/Fail, actual vs expected values etc) and it's fairly easy to set up with Protractor

Gunderson
  • 3,263
  • 2
  • 16
  • 34
  • Worked with appendFile! I had to change the code a little bit but now I'm able to get what I want. thank you! – Alexandre Apr 18 '18 at 08:38