-1

I have a "Date" field on my page I want to automate. Now, when I enter invalid value or invalid format, then its displays an error like:

"This is invalid value for "Date" field. Please enter date between '02/18/2023' and "02/25/2023".
" This is invalid format for "Date" field. Please enter date in format "mm/dd/yyyy".

Now, the date field range is variable. If I configure it to be 1 - 30 days or 15 - 60 days from the current date, then the error message range will be changed. How can I automate that? Also, the Field name is variable, it can be "DateTest', Field1, or any user-defined field.

How can I assert that the correct error message is displayed on the screen based on Date range, field name, etc....?

1
  • 2
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented Feb 18, 2023 at 20:09

1 Answer 1

0

the date field range is variable. If I configure it to be 1 - 30 days or 15 - 60 days from the current date, then the error message range will be changed. How can I automate that?

First, that's great that you chose to put the date range in as a variable. As you attest, this means you can change it easily as needed!

Now, it seems like you want to always ensure the variable content of the date range is tested when changed. You don't need to do this.

The test is to ensure the error message is present when incorrect dates are inputted.

It sounds like your test is like this (a bit of pseudocode):

expect(expectedError).toEqual('This is invalid value for "Date" field. Please enter date between '02/18/2023' and "02/25/2023".')

But if you change the date range, do you have to change the text in that test assertion? If you hardcode the dates, yes, you have to keep changing the test data!

There are better ways!

In your assertion, be sure to also use date variables that match your feature code!

expect(expectedError).toEqual('This is invalid value for "Date" field. Please enter date between startDate and endDate.') where startDate and endDate are variables that equate to real date range you are using.

If you're using the generic format of Please enter date in format "mm/dd/yyyy" then you can change the assertion to Please enter date in format "date regex" where you assert against a regular expression for your valid date format.

There are so many more options, but these are two very common ways you can use.

A lot of people think you have to use the exact string in the test assertion. You don't. Keep in mind that test code is real code. So, if you can use a date variable in your feature code, you can use a date variable or regex in your test code! I've used new Date() or a regex date format in many tests before. This will keep test maintenance low, more readable, and more flexible!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.