Skip to main content

Attribute <Native_Element_Type>

Uses the actor’s ability to BrowseTheWeb to retrieve the value of the specified HTML attribute of a given PageElement.

Example widget

<ul id="shopping-list" data-items-left="2">
<li data-state="bought">Coffee<li>
<li data-state="buy">Honey<li>
<li data-state="buy">Chocolate<li>
</ul>

Retrieve an HTML attribute of a given PageElement

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

const shoppingList = () =>
PageElement.located(By.id('shopping-list'))
.describedAs('shopping list')

await actorCalled('Lisa')
.attemptsTo(
Ensure.that(
Attribute.called('data-items-left').of(shoppingList()),
equals('2')
),
)

Using Attribute as QuestionAdapter

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

const shoppingList = () =>
PageElement.located(By.css('#shopping-list'))
.describedAs('shopping list')

await actorCalled('Lisa')
.attemptsTo(
Ensure.that(
Attribute.called('id').of(shoppingList()).toLocaleUpperCase(),
equals('SHOPPING-LIST')
),
)

Using as filter in Page Element Query Language

import { actorCalled } from '@serenity-js/core'
import { Ensure, includes } from '@serenity-js/assertions'
import { Attribute, By, PageElements } from '@serenity-js/web'

class ShoppingList {
static items = () =>
PageElements.located(By.css('#shopping-list li'))
.describedAs('items')

static outstandingItems = () =>
ShoppingList.items()
.where(
Attribute.called('data-state'),
includes('buy')
)
}

await actorCalled('Lisa')
.whoCan(BrowseTheWebWithWebdriverIO.using(browser))
.attemptsTo(
Ensure.that(
Text.ofAll(ShoppingList.outstandingItems()),
equals([ 'Honey', 'Chocolate' ])
),
)

Learn more

Hierarchy

  • Question<Promise<string>>
    • Attribute

Implements

  • MetaQuestion<PageElement<Native_Element_Type>, Question<Promise<string>>>
  • Optional

Index

Methods

staticcalled

  • called<NET>(name: Answerable<string>): Attribute<NET>

as

  • as<O>(mapping: (answer: string) => O | Promise<O>): QuestionAdapter<O>
  • Maps this question to one of a different type.

    Question.about('number returned as string', actor => '42')   // returns: QuestionAdapter<string>
    .as(Number) // returns: QuestionAdapter<number>

    Type parameters

    • O

    Parameters

    • mapping: (answer: string) => O | Promise<O>

    Returns QuestionAdapter<O>

of

  • Resolves to the value of an HTML attribute of the pageElement.

    Learn more


    Parameters

    Returns MetaQuestionAdapter<PageElement<Native_Element_Type>, string>

answeredBy

  • answeredBy(actor: AnswersQuestions & UsesAbilities): Promise<string>
  • @inheritDoc

    Parameters

    • actor: AnswersQuestions & UsesAbilities

    Returns Promise<string>

isPresent

  • isPresent(): QuestionAdapter<boolean>
  • @inheritDoc

    Returns QuestionAdapter<boolean>

describedAs

  • describedAs(subject: string): Attribute<Native_Element_Type>
  • @inheritDoc

    Parameters

    • subject: string

    Returns Attribute<Native_Element_Type>

toString

  • toString(): string
  • @inheritDoc

    Returns string