Skip to main content

By

By produces a Selector used to locate a PageElement or PageElements on a web page. Selectors can be defined using a static value or a Question to be resolved at runtime.

Defining a selector using a static value

import { PageElement, By } from '@serenity-js/web'

class LoginForm {
static usernameField = () =>
PageElement.located(By.id('username')) // locate element by its id
.describedAs('username field')

static passwordField = () =>
PageElement.of(By.css('[data-test="password"]')) // locate element using a CSS selector
.describedAs('password field')
}

Defining a selector using a Question

Each method on this class accepts an Answerable to allow for dynamic resolution of the selector. This can be useful when the selector is not known at the time of writing the test, or when the selector needs to be calculated based on the state of the system under test.

The example below demonstrates how to use q to define a selector that includes a dynamic value.

import { q } from '@serenity-js/core'
import { PageElement, By } from '@serenity-js/web'

class FormField {
static withTestId = (id: Answerable<string>) =>
PageElement.of(By.css(q`input[data-test-id="${ id }"]`))
.describedAs('form field')
}

Learn more

Index

Constructors

constructor

  • new By(): By
  • Returns By

Methods

staticcss

  • css(selector: Answerable<string>): Question<Promise<ByCss>>

staticcssContainingText

  • cssContainingText(selector: Answerable<string>, text: Answerable<string>): Question<Promise<ByCssContainingText>>

staticdeepCss

  • deepCss(selector: Answerable<string>): Question<Promise<ByCss>>

staticid

  • id(selector: Answerable<string>): Question<Promise<ById>>
  • Locates a PageElement using its id.


    Parameters

    • selector: Answerable<string>

    Returns Question<Promise<ById>>

statictagName

  • tagName(selector: Answerable<string>): Question<Promise<ByTagName>>

staticxpath

  • xpath(selector: Answerable<string>): Question<Promise<ByXPath>>