Writing Tests
Each test will have an Appetize session for you to interact with your device (like a page in Playwright). You can then assert the app based on some criteria, such as the existence of a UI element, a network request, or do a screenshot test.
Your first test
Take a look at the following test example:
import { test, expect } from '@appetize/playwright'
// 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: {
attributes: {
accessibilityIdentifier: 'username_field'
}
}
})
await session.type('jordan')
// type password
await session.tap({
element: {
attributes: {
accessibilityIdentifier: 'password_field'
}
}
})
await session.type('secretpassword')
// tap login button
await session.tap({
element: {
attributes: {
text: 'Login'
}
}
})
// assert that an element with 'Hello Jordan' exists on the screen
await expect(session).toHaveElement({
attributes: {
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.
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.
All actions that target an element will wait up to 10 seconds for an element to appear. If you need to wait for an element to appear without interacting on it, you can use waitForElement()
These timeouts are also configurable (see Timeouts.)
Assertions
expect
Playwright uses the expect 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:
not.toHaveElement
Asserts that an element does not exists in the current application UI
Screenshot comparisons
toMatchSnapshot() works with session.screenshot(), allowing you to do screenshot comparisons of your app.
Note: screenshot tests are fragile by nature as any change in the UI could 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 can 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)
Real life applications
The Common Testing Scenarios page highlights typical challenges mobile developers face when writing tests and explains how to use key features covered on this page, such as waiting for network events, validating elements, and comparing screenshots.
Last updated