How to write multiple strings into a file in protractor(js)


I am using protractor cucumber framework.I need to write some data into a file and read that data also.But it is not working as expected.The code i have tried is given below.

const fs = require('fs');
After(function(){    
  var content = ["kerala","asam"];
  var str=content.toString();
  var content1 = ["india","usa","uk"];
  var str1=content1.toString();

  if(str){
   fs.writeFile('test.txt', str, (err) => {
    if (err) {
      console.error(err);
      return
     }
   });
 }
 if(str1){
  fs.writeFile('test.txt', str1, (err) => {
    if (err) {
      console.error(err);
      return
    }
  });
 }
});

AfterAll(function(callback){
 console.log("afterall");
 fs.readFile('test.txt', 'utf-8', function(err, buf) {
   console.log(buf.toString());
 });
 callback();
});

some time the arrays may be empty(depending on the test execution).so if the array having data, i need to write that data into a file and read also.But the after writing into the file the, the file having data as given below

india,usa,uk

it should be

kerala,asam
india,usa,uk

How can i do this .Thanks in advance.

- - Source
comments powered by Disqus