My Test should run only on weekdays

Hi Team ,

Can anyone please help me here

I have test now it will get yesterday data when ever we run .

Now I want If I run my test on Monday it should get Friday data ,

Every week Saturday and Sunday are holidays no data will be available , I want to skip weekends

Present Pre-Request Script

const moment = require(‘moment’);
pm.collectionVariables.set(“data_date”, moment().subtract(1, ‘day’).format(“YYYY-MM-DD”));

Hi @bhasker.vade :wave:

So the tests are running based on the data_date is it?

you can use getDate() method to get the date values in numbers.

You can try the below snippet:

const moment = require('moment');
var myDate = new Date();
console.log(myDate);
//console.log(myDate.getDay());
if(myDate.getDay() == 6)
{
console.log("It's Saturday!!");
}
if(myDate.getDay() == 0)
{
console.log("It's Sunday!!");
}
if(myDate.getDay() == 1)
{
console.log("It's Monday!!");
pm.collectionVariables.set("data_date", moment().subtract(3, 'day').format("YYYY-MM-DD"));
}
else{ 
console.log("It's Weekday!!");
pm.collectionVariables.set("data_date", moment().subtract(1, 'day').format("YYYY-MM-DD"));
}

Saturday and Sunday can be clubbed under same loop, incase if you want to run Friday data on Saturday you can add one more line under the first if block.

If you are looking for something else, please provide us more details :slight_smile:

2 Likes

Thank you :slight_smile: @bpricilla
yes , tests are running based on the data_date

I have used below snippet

const moment = require('moment');

let today = moment().day();

if(today == 0 || today == 1){

    pm.environment.set("previous_tradedate",moment().subtract(6,'days').day(5).format("YYYY-MM-DD"));

}

else {

    pm.environment.set("previous_tradedate",moment().day(today - 1).format("YYYY-MM-DD"));

}
1 Like

It looks like you both have things covered here but I wanted to offer a suggestion :pray:

When I try and parse days of the week as numbers in my head, it kinda goes :exploding_head: maybe having them as actual days might help with the readability here?

let moment = require('moment'),
    today = moment().format('dddd');

if(today === 'Saturday' || today === 'Sunday') {
    // Do something 
}
else {
    // Do something else 
}
3 Likes

@danny-dainton thank you , I will try this now