0

In one of my automation tests, I am importing a date format stored in the environment parameters, use it for my test and then increment it by one second after the test is executed. This is the code:

afterEach(()=>{
    var startTimeStamp = Cypress.env('aestartTimestamp');
    var startTimestampString = String(startTimeStamp);
    var epochStartTimeStamp = new Date(startTimestampString);
    var startYear = epochStartTimeStamp.getFullYear();
    var month = (epochStartTimeStamp.getMonth())+1;
    var realStartMonth = month <=9 ? '0'+month:month;
    var startDate = epochStartTimeStamp.getDate()<=9 ? '0'+epochStartTimeStamp.getDate():epochStartTimeStamp.getDate();
    var startHours = epochStartTimeStamp.getHours() <=9 ? '0'+epochStartTimeStamp.getHours():epochStartTimeStamp.getHours();
    var startMinutes = epochStartTimeStamp.getMinutes() <=9 ? '0'+epochStartTimeStamp.getMinutes():epochStartTimeStamp.getMinutes(); 
    var startSeconds = (epochStartTimeStamp.getSeconds()+1)<=9? '0'+(epochStartTimeStamp.getSeconds()+1):(epochStartTimeStamp.getSeconds()+1);
    var newStartTimeStamp = startYear+"-"+realStartMonth+"-"+startDate+" "+startHours+":"+startMinutes+":"+startSeconds;
    Cypress.env('aestartTimestamp',newStartTimeStamp);
    console.log(newStartTimeStamp);

    var endTimeStamp = Cypress.env('aeendTimestamp');
    var endTimeStampString = String(endTimeStamp);
    var epochEndTimeStamp = new Date(endTimeStampString);
    var endYear = epochEndTimeStamp.getFullYear();
    var endMonth = (epochEndTimeStamp.getMonth())+1;
    var realEndMonth = endMonth <=9 ? '0'+endMonth:endMonth;
    var endDate = epochEndTimeStamp.getDate()<=9 ? '0'+epochEndTimeStamp.getDate():epochEndTimeStamp.getDate();
    var endHours = epochEndTimeStamp.getHours() <=9 ? '0'+epochEndTimeStamp.getHours():epochEndTimeStamp.getHours();
    var endMinutes = epochEndTimeStamp.getMinutes() <=9 ? '0'+epochEndTimeStamp.getMinutes():epochEndTimeStamp.getMinutes();
    var endSeconds = (epochEndTimeStamp.getSeconds()+1) <=9 ? '0'+(epochEndTimeStamp.getSeconds()+1):(epochEndTimeStamp.getSeconds()+1);
    var newEndTimestamp = endYear+"-"+realEndMonth+"-"+endDate+" "+endHours+":"+endMinutes+":"+endSeconds;
    Cypress.env('aeendTimeStamp',newEndTimestamp);
    console.log(newEndTimestamp);
})

However, after running the tests, the date gets converted to NaN. This is what I see in the console:

enter image description here

The timestamp is converted to NaN.

When I run the code independently in any browser console, I see that the code works just perfectly. (The new time stamp is incremented by 1 second).

So I am mystified as to why the code works properly in the dev console but not in the Cypress. Test. Is there something missing?

srini
  • 201
  • 2
  • 11
  • 1
    Does this answer your question? [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Sebastiano Schwarz Jan 19 '22 at 09:55
  • If the date strings are not in [this format](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#date_time_string_format) then the behavior is implementation specific (your code might work in one browser but not another) – Salman A Jan 19 '22 at 10:07
  • @SalmanA—implementations must also parse the format produced by *Date.prototype.toString*. – RobG Jan 19 '22 at 11:23
  • @robg I forgot about that but since the code works in one engine but not another then it is in none of those formats. – Salman A Jan 19 '22 at 11:25

0 Answers0