Writing Tests

Each test will have an Appetize session for you to interact with your device (like a pagearrow-up-right in Playwright). You can then assert the app based on some criteria, such as the existence of a UI element, a made network request, or a screenshot.

Your first test

Take a look at the following test example:

import { test, expect } from '@appetize/playwright'

test.setup({
    publicKey: '<PUBLIC KEY>',
    // ... other config
    // see https://docs.appetize.io/javascript-sdk/configuration
})

// reinstall app after each test to reset data
test.afterEach(async ({ session }) => {
    await session.reinstallApp()
})

test('logs in to the app', async ({ session }) => {
    // type username
    await session.tap({ element: { accessibilityIdentifier: 'username_field' } })
    await session.type({ value: 'jordan' })
    
    // type password
    await session.tap({ element: { accessibilityIdentifier: 'password_field' } })
    await session.type({ value: 'secretpassword' })
    
    // tap login button
    await session.tap({ element: { text: 'Login' } })
    
    // wait for an element with 'Hello Jordan' to exist on the screen
    await expect(session).toHaveElement({ text: 'Hello Jordan' })
})

This will load a hypothetical app with a login screen. It will tap on the username and password fields, type some credentials, log in, and assert that a UI element with text "Hello Jordan" exists.

Setup

It is required to call test.setup at the start of your test suite. It takes a configuration object to specify your publicKey, device, OS version, etc.

Typically you will call this at the top-level of your test file, but you can also nest them under test.describe()

Actions

Each test provides the session for your app. You can use Automation actions here to interact with it as you need.

Sequencing actions

All actions are promises that will await until the interaction has been played on the device. If your action targets an element, Appetize will wait for the element to appear before proceeding, which is helpful when your action results in a change in UI.

If you just need to wait for an element to appear, you can use waitForElement()

Assertions

expect

Playwright uses the expectarrow-up-right library for assertions. We have added custom async matchers for asserting on application state.

toHaveElement

Asserts that an element exists in the current application UI

It can take additional options to change the behaviour of the assertion:

If you want to assert that an element does not exist:

Screenshot comparisons

toMatchSnapshot()arrow-up-right works with session.screenshot(), allowing you to do screenshot comparisons of your app.

Note: screenshot tests are fragile by nature because any change in the UI will cause it to fail. It is always better to assert on a narrow piece of application state, such as toHaveElement, or on network/debug log output.

Network

An example of how you could assert that a network request was made.

See documentation for network events

Helpers

session contains a few helper methods for writing tests that may come in handy

waitForElement

Waits for an element to appear.

waitForEvent

Waits for an event to occur

waitForTimeout

Waits for the given time to elapse (in ms)

Last updated