Validate element text

Validate that the expected text is visible

An Element Text validation is similar to an Element Visible validation in that it makes the step dependent on the existence of a specific element. However, for the Element Text validation, you also specify a particular text value that must appear in the specified element. You can easily validate multiple text elements as well.
The text being validated can be given as a specific string or a range of values represented as a Regex expression, a short JS expression, or a parameter. See Advanced text validation.

📘

Note (Web Only):

You can use a keyboard shortcut to record text validation from your application during recording. Just use Ctrl +'v' and select the text you want to validate.

Adding a Validate element text step (Web)

To add an Element Text validation:

  1. Hover over the > (arrow symbol) where you want to add the validation.
2484

The action options are displayed.

300
  1. Click on the Toggle Breakpoint button.
400
  1. Click on the Play Scenario button, to run the test until the breakpoint.
2466
  1. Hover over the position again and click on the "M" (Testim predefined steps).
    The Predefined steps menu opens.
300
  1. Click on Validations.
    The Validations section expands.
300
  1. Scroll down through the menu and select Validate element text.

📘

Alternatively, you can use the search box at the top of the menu to search for Validate element text.

  1. In the AUT window, select the relevant element that you wish to validate by clicking on it.
    The step is created, and a thumbnail of the selected element is shown in the step.
3615

📘

Additionally, you can select multiple text elements to validate by pressing the ‘Ctrl’ key and clicking on multiple elements. A reusable group will be created, which includes all of the validations.

  1. Click on the Toggle Breakpoint button after the Validation step to remove the breakpoint.

Adding a Validate element text step (Mobile)

To add an Element Text validation:

  1. Hover over the > (arrow symbol) where you want to add the validation and click the Testim predefined steps button.
606
  1. Select Validate element text under the Validations group.
442

📘

Note:

Alternatively, you can use the search box at the top of the menu to search for Validate element text.

  1. The AUT will open. Select the text element on the screen you want to validate.
305
  1. The Text Validation step is created, and a thumbnail of the selected element is shown in the step.
907

Modifying a Validate element text step (Mobile & Web)

If you want to change the element or text you selected, you don’t need to delete and re-record the step. Instead, you can just reassign the element with a different element, or you can edit the text of the initial element that is being validated.

Reassigning the selected element

To reassign the selected element in a Validate element text step:

  1. Hover over the position to the left of the step for which you want to reassign the element and click on the Toggle Breakpoint button.
  2. Click on the Play Scenario button, to run the test until the breakpoint.
  3. Hover over the step for which you want to reassign the element and click on the Show Properties () icon.
2468

The Properties panel opens on the right-hand side.
4. Hover over the Target element thumbnail.

300

The Target element options are shown.

300
  1. Click Reassign.
300
  1. In the AUT browser, identify the relevant element and click on it to select it.
    The selected element is shown in the Target element box in the Properties panel.
  2. Click on the Toggle Breakpoint button to the left of the step for which you reassigned the element to remove the breakpoint.

Editing the validation text

To edit the validation text of the initial element:

  1. Hover over the step for which you want to edit the validation text and click on theShow Properties () icon.
2468

The Properties panel opens on the right-hand side.
2. In the Expected value field, enter the new validation text.

300

Advanced text validation (Mobile & Web)

In many cases it is difficult to specify an entire text string that will be matched in its entirety to the text that you want to validate. In the Expected Value field, you can create text validations by combining:

  • Regular expressions (partial strings)
  • JavaScript expressions
  • Parameters

Using Regular Expression (Regex)

Testim supports Regex inside the Expected Value input. These are the most common cases:

Starts with

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

234

Ends with

Validates that the text that 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 that 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.

👍

Tips

  • RegexOne is a great place to learn and practice regular expressions.
  • RegularExpressions 101 is a great tool for developing and testing a regular expression.

Using JavaScript expression

In some cases, you may 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 Testim will compare the calculated string result of this expression. See a failed validation:

948

Using a parameter

You can also use parameters, which were defined in the test or suite level or were defined in the config file to validate the text element. If you use a parameter that was created in a different step, you will have to export it to the test level (more about exporting parameters).

Parameter only

There are 2 types of parameters:
HTML: Allows you to refer to HTML Elements in your app.
JS (JavaScript): Allows you do define any JS expression.

To use a parameter in the Expected Value field:

  1. Define a parameter in one of the following ways:

1920

Click on the gif to enlarge

  1. If the scope of the parameter was define in a step level, you need to pass the parameter to the Element text validation step or to the test level, by exporting the parameter. For detailed instructions, see Exports Parameters.
    For example, to export a username parameter with the value Hello, John, we will add a new custom action step and type the following in the editor:
exportsTest.usename= "Hello, John"
1920

Click on the gif to enlarge

  1. Create an Element text validation step and add the parameter to the Expected value field.
1920

Click on the gif to enlarge

After running the test you will see that Element text validation step will match the expected parameter value.

1842

Click on the image to enlarge

You can also combine the parameter with an exact string by adding a '+' between them. For example, instead of setting the username parameter value to Hello, John, we can define it as John and then specify the Expected Value as:
`'Hello ' + userName

Combining a parameter with Regex.

It is possible to combine the parameter and with Regex by defining a regex function in the Expected value field.
For example a combination that start with the parameter will look like this:

new RegExp("^" +userName)

A combination that ends with the parameter will look like this:

new RegExp(userName+"$")