Many time when you want to validate a text string, simple "exact match" is not enough. Here we'll show how to create advanced Text Validations, without the need of writing complete custom validations in JavaScript.

Using Regular Expression (Regex)

The Testim Editor supports Regex inside the Expected Value input. Let's cover the most common cases:

Starts with: Validates that the text starts with a certain word, allowing the rest of the text to be dynamic, and still pass the validation:
/^My text/

Ends with: Validates that the text ends with a certain word, allowing the rest of the text to be dynamic, and still pass the validation:
/my text$/

Contains: Validates that the text contains a certain word, allowing the rest of the text to be dynamic, and still pass the validation:
/my text/

Of course you can create and use any other valid Regex that you may need.

Tip RegexOne is a great place to learn and practice regular expressions.

Using JavaScript expression

In other cases, you'd want to validate that a text equals the result of a JavaScript expression. For example, making sure that the the text holds the current date. In this case you can set the Expected Value to this simple expression: new Date().toDateString(), and The Testim Editor will compare the calculated string result of this expression. See a failed validation:

Using a parameter

Another advantage of the JavaScript expression, is that you can use a parameter created previously in a different step (more about exporting parameters).

For example I have exported from a prior step the user name to the parameter userName.
Now I want to validate that we greet the user accordingly - so we'll add a text validation on the greeting label:

And set it's value in the Properties panel to this JavaScript expression:
'Hello ' + userName + '!' 

Did this answer your question?