import {Text} from '@serenity-js/webdriverio/lib/screenplay/questions'
public class | source

Text

Resolves to the visible (i.e. not hidden by CSS) innerText of:

  • a given WebElement, represented by Answerable<Element>
  • a group of WebElements, represented by Answerable<ElementArray>

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

Examples:

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 element
 import { actorCalled } from '@serenity-js/core';
 import { Ensure, equals } from '@serenity-js/assertions';
 import { BrowseTheWeb, by, Target, Text } from '@serenity-js/webdriverio';

 const header = () =>
     Target.the('header').located(by.tagName('h1'))

 actorCalled('Lisa')
     .whoCan(BrowseTheWeb.using(browser))
     .attemptsTo(
         Ensure.that(Text.of(header()), equals('Shopping list')),
     )
Retrieve text of a multiple elements
 import { actorCalled } from '@serenity-js/core';
 import { Ensure, equals } from '@serenity-js/assertions';
 import { BrowseTheWeb, by, Target, Text } from '@serenity-js/webdriverio';

 const shoppingListItems = () =>
     Target.the('shopping list items').located(by.css('#shopping-list li'))

 actorCalled('Lisa')
     .whoCan(BrowseTheWeb.using(browser))
     .attemptsTo(
         Ensure.that(
             Text.ofAll(shoppingListItems()),
             equals([ 'Coffee', 'Honey', 'Chocolate' ])
         ),
     )
Find element with matching text
 import { actorCalled } from '@serenity-js/core';
 import { contain, Ensure } from '@serenity-js/assertions';
 import { BrowseTheWeb, by, CSSClasses, Target, Text } from '@serenity-js/webdriverio';

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

 actorCalled('Lisa')
     .whoCan(BrowseTheWeb.using(browser))
     .attemptsTo(
         Ensure.that(
             CSSClasses.of(shoppingListItemCalled('Honey)),
             contain('bought')
         ),
     )

See also:

Static Method Summary

Static Public Methods
public static

of(element: Answerable<Element<'async'>>): Question<Promise<string>> & MetaQuestion<Answerable<Element<'async'>>, Promise<string>>

Retrieves text of a single WebElement, represented by Answerable<Element>.

public static

ofAll(elements: Answerable<ElementArray>): Question<Promise<string[]>> & MetaQuestion<Answerable<Element<'async'>>, Promise<string[]>>

Retrieves text of a group of WebElements, represented by Answerable<ElementArray>

Static Public Methods

public static of(element: Answerable<Element<'async'>>): Question<Promise<string>> & MetaQuestion<Answerable<Element<'async'>>, Promise<string>> source

Retrieves text of a single WebElement, represented by Answerable<Element>.

Params:

NameTypeAttributeDescription
element Answerable<Element<'async'>>

Returns:

Question<Promise<string>> & MetaQuestion<Answerable<Element<'async'>>, Promise<string>>

public static ofAll(elements: Answerable<ElementArray>): Question<Promise<string[]>> & MetaQuestion<Answerable<Element<'async'>>, Promise<string[]>> source

Retrieves text of a group of WebElements, represented by Answerable<ElementArray>

Params:

NameTypeAttributeDescription
elements Answerable<ElementArray>

Returns:

Question<Promise<string[]>> & MetaQuestion<Answerable<Element<'async'>>, Promise<string[]>>