0

I am using the Cypress to test one Web UI, and entering the menu requires the password. Password changes every day and it is the current date written backwards: Example: Todays date is 24.03. and my password is 3042.

I would appreciate the help

  const date = require('dayjs')
  cy.log(date().format('DDMM')); 

I am getting the current date, but typing it backwards makes the problem.

gelson
  • 13
  • 3
  • You can convert the date to a string and use `String.reverse()` method to reverse the date string – Joshua Mar 24 '23 at 09:06
  • But without a minimal and complete example, it's very hard to reproduce your issue and provide a helpful answer. Please add one: https://stackoverflow.com/help/minimal-reproducible-example – Joshua Mar 24 '23 at 09:07

2 Answers2

1

Please see How do you reverse a string in-place in JavaScript?

const dayMonth = '24.03'
const reverse = dayMonth.split('').reverse().join('').replace('.', '')
expect(reverse).to.eq('3042')

The principle is a string is an array of characters, and arrays can easily be reversed.

The result can be joined back into a string, then you need to deal with the decimal separator.

I'm assuming your dayjs example is yielding the "forward" date.

user16695029
  • 3,365
  • 5
  • 21
1
const dayjs = require('dayjs')
const todaysDate = dayjs().format('DDMM')
const password = todaysDate.split('').reverse().join('')
cy.visit(url);
.
.
.
cy.get('.ng-untouched').type(password);

I found the solution to do it like that. Thank you Very much :D

gelson
  • 13
  • 3