1

I have a command where I can enter a specific date for start date, please see below a part of the command I am using.

cy.commands

and when I am calling it in the test I need to enter a dynamic date like today <=(+30 days)

cy.create123((new Date().getDate() - 1), '2023-08-07')

ofc it did not work, but I have no idea how can I do it. How I can setup to cy.command to get always today-1 as startDate!

My issue is to make the dynamic enter date to work on Cypress.Commands()

Fody
  • 23,754
  • 3
  • 20
  • 37
Fah
  • 207
  • 3
  • 16

2 Answers2

1

TLDR

Install dayjs and use

const startDate = dayjs().add(-1, 'day').format('YYYY-MM-DD')

cy.create123(startDate, '2023-08-07')

The Custom Command and the cy.request() inside it are expecting the date as a string type.

Your calculated dynamic date (new Date().getDate() - 1) is giving you a number type.

But .toISOString() only works on Date types, not number types.

So after doing math on the Date(), you get a number which must be converted into a Date and then into a string.

const today = new Date()
const yesterday = new Date(today.setDate(today.getDate() -1))
const startDate = yesterday.toISOString()

But even that's not the end of the problems, because the timezone might give you invalid dates.

I recommend using dayjs as shown above.

Fody
  • 23,754
  • 3
  • 20
  • 37
0

You can do something like this. Instead of subtracting 1, I am decreasing the day with 24 hours.

cy.create123(new Date(Date.now() - (3600 * 1000 * 24)).getUTCDate(), '2023-08-07')

Considering today is 29 Aug, this will give the output as 28.

date

To get the date in the format yyyy-mm-dd use:

new Date(Date.now() - ( 3600 * 1000 * 24)).toISOString().slice(0, 10)

date formatted

Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • the code `cy.create123((new Date().getDay() - 1), '2023-08-07')` is not working. neither did `cy.create123(new Date(Date.now() - (3600 * 1000 * 24)).getUTCDate(), '2023-08-07')` – Fah Aug 29 '22 at 12:40
  • In which format do you want the date in your custom command ? – Alapan Das Aug 29 '22 at 12:41
  • the date format is YYYY/MM/DD – Fah Aug 29 '22 at 12:43
  • `cy.create123((new Date().Date.now() - 1), '2023-08-07')` then it get the today day and not the today -1 – Fah Aug 29 '22 at 12:47
  • please refer the updated answer. – Alapan Das Aug 29 '22 at 12:48
  • The date format is already saved in another place and it will always be in the correct format. what I mean it did not work was, it do not get the correct day. the Today - 1 ..eg today is 29 - 1 so the date would be 28th august. – Fah Aug 29 '22 at 12:50
  • `new Date(Date.now() - ( 3600 * 1000 * 24)).toISOString().slice(0, 10)` and `cy.create123(new Date(Date.now() - (3600 * 1000 * 24)).getUTCDate(), '2023-08-07')` get the date 1970-01-01 – Fah Aug 29 '22 at 12:52
  • Please check the code, it shouldn't give 1970-01-01. You can run the same code in the browser console. Attached a screenshot. – Alapan Das Aug 29 '22 at 12:55
  • well, it does work on console, however it does not work the same way on cypress command. And my issue is to make it to work on cypress.commands() – Fah Aug 29 '22 at 13:14