TestProject Forum
Powered by leading experts in the test automation community

How to save JavaScript code to parameter

I want to save a JavaScript code into a parameter so I can validate the most current date. This is how it looks:

d = new Date(); weekday = new Array(7); weekday[0] = “Sun”; weekday[1] = “Mon”; weekday[2] = “Tue”; weekday[3] = “Wed”; weekday[4] = “Thu”; weekday[5] = “Fri”; weekday[6] = “Sat”; the_weekday = weekday[d.getDay()]; month = new Array(); month[0] = “Jan”; month[1] = “Feb”; month[2] = “Mar”; month[3] = “Apr”; month[4] = “May”; month[5] = “Jun”; month[6] = “Jul”; month[7] = “Aug”; month[8] = “Sep”; month[9] = “Oct”; month[10] = “Nov”; month[11] = “Dec”; the_month = month[d.getMonth()]; the_day = d.getDate(); the_full_year = d.getFullYear(); d = "Updated: " + the_weekday + ", " + the_month + " " + the_day + ", " + the_full_year;

This is the text on the page: “Updated: Tue, Nov 12, 2019”.

Is it possible to do this or is there another solution I can do to validate the date? Thank you.

It is possible, you can use the action Execute JavaScript.
However please allow me to propose an alternative flow:


Assuming you have the actual string that contains the expected date in P1 parameter,
Let’s replace the ‘Updated: ‘ part with empty string – remove it.


Now let’s generate a date in the required format:


The result would have extra dot after day short name, let’s remove it:


Now let’s remove the extra dot after month name:


Great – now let’s compare:


This is the result:


Thanks, Marat. I tried to go the JavaScript code route right now but I’m getting a blank output.

I tried the code on the Dev Tools console and it was able to give me “Updated: Wed, Nov 13, 2019” so I think the code should work. Is there something I’m missing so I’m not getting any output?

Thank you for your help.

You should return the value, e.g.
return "Test";

And assign the output to a parameter.

1 Like