Skip to main content

Text

Uses the actor’s ability to BrowseTheWeb to retrieve the visible (i.e. not hidden by CSS) innerText of:

The result includes the visible text of any sub-elements, without any leading or trailing whitespace.

Example widget

<h1>Shopping list</h1>
<ul id="shopping-list">
<li>Coffee<li>
<li class="bought">Honey<li>
<li>Chocolate<li>
</ul>

Retrieve text of a single PageElement

import { actorCalled } from '@serenity-js/core'
import { Ensure, equals } from '@serenity-js/assertions'
import { By, PageElement, Text } from '@serenity-js/web'

const header = () =>
PageElement.located(By.css('h1'))
.describedAs('header')

await actorCalled('Lisa')
.whoCan(BrowseTheWebWithWebdriverIO.using(browser))
.attemptsTo(
Ensure.that(Text.of(header()), equals('Shopping list')),
)

Retrieve text of multiple PageElement

import { actorCalled } from '@serenity-js/core'
import { Ensure, equals } from '@serenity-js/assertions'
import { By, PageElement, Text } from '@serenity-js/web'

const shoppingListItems = () =>
PageElements.located(By.css('#shopping-list li'))
.describedAs('shopping list items')

await actorCalled('Lisa')
.attemptsTo(
Ensure.that(
Text.ofAll(shoppingListItems()),
equals([ 'Coffee', 'Honey', 'Chocolate' ])
),
)

Using as filter in Page Element Query Language

import { actorCalled } from '@serenity-js/core'
import { contain, Ensure } from '@serenity-js/assertions'
import { By, CssClasses, PageElement, Text } from '@serenity-js/web'

const shoppingListItemCalled = (name: string) =>
PageElements.located(By.css('#shopping-list li'))
.describedAs('shopping list items')
.where(Text, equals(name))
.first()

await actorCalled('Lisa')
.attemptsTo(
Ensure.that(
CssClasses.of(shoppingListItemCalled('Honey)),
contain('bought')
),
)

Learn more

Index

Constructors

Methods

Constructors

constructor

Methods

staticof

staticofAll