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!