busyguy
busyguy

Reputation: 277

Cypress - Increment a field by one each time?

I have some automation in Cypress that I want to essentially increment by one each times it's ran, I want to add it to a pipeline so it's ran daily, my code is:

cy.get('publicField').click().type('Day 1')

So when it's ran again it's day 2, day 3, etc..

What would be the best way to implement this?

Thanks!

Upvotes: 1

Views: 506

Answers (2)

Fody
Fody

Reputation: 32034

One way, set your start date in a fixture and calculate the difference to current date.

I would add the Day.js package for that, but you could also do it with plain javascript.

start-date.json

"2022-03-09"
import dayjs from 'dayjs'

it('types todays sequence day', () => {

  cy.fixture('start-date.json').then(start => {

    const date1 = dayjs(start)
    const date2 = dayjs(Date.now())
    const diffDays = date2.diff(date1, 'day')

    cy.get('publicField').type(`Day ${diffDays}`)
  })
})

Upvotes: 2

agoff
agoff

Reputation: 7165

If you were only running your code once daily, a possible solution would just be to generate the date that the test is ran.

cy.get('foo')
  .click()
  .type(new Date().toISOString().slice(0, 10));
  // The above creates a string that is something like 20220309 (YYYYMMDD).
  // You could play around with formatting for the Date string to suit your needs.

Upvotes: 1

Related Questions