Skip to main content

isPresent

Callable

  • isPresent<Actual>(): Expectation<Actual>

  • Creates an expectation that is met when the actual value is not undefined or null.

    Also, when the actual implements Optional, the expectation is met when calling Optional.isPresent() returns an Answerable that resolves to true

    Ensuring that a value is defined

    import { actorCalled } from '@serenity-js/core'
    import { CallAnApi, Send, GetRequest, LastResponse } from '@serenity-js/rest'
    import { Ensure, isPresent } from '@serenity-js/assertions'

    interface Product {
    name: string;
    }

    interface ProductsResponse {
    products: Product[];
    }

    await actorCalled('Apisitt')
    .whoCan(CallAnApi.at('https://api.example.org'))
    .attemptsTo(
    Send.a(GetRequest.to('/products')),
    Ensure.that(LastResponse.body<ProductsResponse>().products[0], isPresent()),
    )

    Checking if a PageElement is present

    import { actorCalled, Check } from '@serenity-js/core';
    import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright';
    import { By, Click, Navigate, PageElement } from '@serenity-js/web';
    import { Browser, chromium } from 'playwright';

    class NewsletterSubscription {
    static modal = () =>
    PageElement.located(By.id('newsletter-subscription'))
    .describedAs('newsletter subscription modal')

    static closeButton = () =>
    PageElement.located(By.class('.close'))
    .of(NewsletterSubscription.modal())
    .describedAs('close button')
    }

    const browser = await chromium.launch({ headless: true });

    await actorCalled('Isabela')
    .whoCan(BrowseTheWebWithPlaywright.using(browser))
    .attemptsTo(
    Navigate.to(`https://example.org`),
    Check.whether(NewsletterSubscription.modal(), isPresent())
    .andIfSo(Click.on(NewsletterSubscription.closeButton())),
    )

    Type parameters

    • Actual

    Returns Expectation<Actual>