Ask a Question

Verify text with changing data

JustinL22
New Contributor

Verify text with changing data

Hi,

I am creating a test where I need to verify some specific text is present so I am using the checkpoint wizard to verify the text. However in the middle of the text will be a date that will show whichever date the test is run so can change each time I run it and therefore the checkpoint fails. So for example I want to verify the text shows 'This is a test on 01/12/2022' but the next time I run it the text shows 'This is a test on 01/01/2023'.

The rest of text other than the date will always say the same so how can I check that but also cater for the date changing and always being todays date?

Thanks

3 REPLIES 3
Kitt
Regular Contributor

If you know the message will always start the same with, "This is a test on " - you could just capture the first 18 characters using [aqString.SubString] and supply today's date using your own function to validate. Here's how to return today's date and add that date to the end of your aqString.SubString for validation (sorry for bad formatting - inline code feature isn't working):

function getCurrentDate() {
var currentDate = aqDateTime.Today();
var today = aqConvert.DateTimeToFormatStr(currentDate, "%m/%d/%Y"); //MM/DD/YYYY
return today;
}

function mainMessageHandler() {
// set variables
var page = Sys.Browser("*").Page("*");
var date = getCurrentDate();
var aString = "This is a test on 01/01/2023"; //get text from obj
var aSubString = aqString.SubString(aString, 0, 18); //This_is_a_test_on_
var Res = aqString.Find(aqString.ToLower(aString), aqString.ToLower(aSubString));

// find subString and add today's date
Log.Message("LOG: full string --> " + aString);
Log.Message("LOG: substring --> " + aSubString);

if (!equal(Res, -1)) {
Log.Message("| Great Success | - substring '" + aSubString + "' was found");
Log.Checkpoint(aSubString + date);
}
else {
Log.Warning("WARNING: " + aSubString + " no found in: " + aString);
}
}

message-test.png

[[ kITt: keep IT testing ]]
JustinL22
New Contributor

Sorry the example given probably wasn't the best one as the date will fall within the middle of a set of text so getting the first x number of characters wouldn't be applicable. Is there a similar way if the date falls in the middle of a block of text?

Kitt
Regular Contributor

Well you can still look for that particular subString - the loop will fail if it's not found. So as long as you are supplying the object's text value as the string, and defining the subString you're looking for, the code still works to validate and post the new subString w/today's date. Just change 2 lines:

var aString = "This is a test on 01/01/2023"; //get text from obj
var aSubString = aqString.SubString(aString, 0, 18); //This_is_a_test_on_

to

var aString = page.FindElement("<xpath to your obj>").contentText; //get text from obj
var aSubString = "This is a test on"; // hard code what you're looking for

[[ kITt: keep IT testing ]]
cancel
Showing results for 
Search instead for 
Did you mean: