Skip to content

Latest commit

 

History

History
99 lines (74 loc) · 4.21 KB

APIRef.waitFor.md

File metadata and controls

99 lines (74 loc) · 4.21 KB
id title
APIRef.waitFor
Manual Synchronization Using `waitFor`

In most cases, tests should be automatically synchronized with the app. When synchronization doesn't work, you have a fail-safe by using waitFor. This API polls using the given expectation continuously until the expectation is met. Use manual synchronization with waitFor only as a last resort. Polling for expectations isn't exactly a best practice.

Test async code with waitFor.
Hang the test until an expectation is met.

Methods

NOTE: Every waitFor call must set a timeout using withTimeout(). Calling waitFor without setting a timeout will do nothing.

NOTE: waitFor will not throw when reaching timeout, instead it will just continue to the next line. To make sure your tests work as you expect them to add expect() at the following line.

toBeVisible()

Test will hang until expectation is met or a timeout has occurred. Should be accompanied with expect.toBeVisible()
Wait for the view to be at least 75% visible.

await waitFor(element(by.id('UniqueId204'))).toBeVisible().withTimeout(2000);
await expect(element(by.id('UniqueId204'))).toBeVisible();

toBeNotVisible()

Test will hang until expectation is met or a timeout has occurred. Should be accompanied with expect.toBeNotVisible()
Wait for the view to not be visible.

await waitFor(element(by.id('UniqueId205'))).toBeNotVisible().withTimeout(2000);
await expect(element(by.id('UniqueId205'))).toBeNotVisible();

toExist()

Test will hang until expectation is met or a timeout has occurred. Should be accompanied with expect.toExist()
Wait for the view to exist in the UI hierarchy.

await waitFor(element(by.id('UniqueId205'))).toExist().withTimeout(2000);
await expect(element(by.id('UniqueId205'))).toExist();

toNotExist()

Test will hang until expectation is met or a timeout has occurred. Should be accompanied with expect.toNotExist()
Wait for the view to not exist in the UI hierarchy.

await waitFor(element(by.id('RandomJunk959'))).toNotExist().withTimeout(2000);
await expect(element(by.id('RandomJunk959'))).toNotExist();

toHaveText(text)

Test will hang until expectation is met or a timeout has occurred. Should be accompanied with expect.toHaveText(text)

  • In React Native apps, expect UI component of type <Text> to have text.
  • In native iOS apps, expect UI elements of type UIButton, UILabel, UITextField or UITextViewIn to have inputText with text.
await waitFor(element(by.id('UniqueId204'))).toHaveText('I contain some text').withTimeout(2000);
await expect(element(by.id('UniqueId204'))).toHaveText('I contain some text');

toHaveValue(value)

Test will hang until expectation is met or a timeout has occurred. Should be accompanied with expect.toHaveValue(value)

  • In React Native apps, expect UI component to have testID with that id.
  • In native iOS apps, expect UI element to have accesibilityIdentifier with that id.
await waitFor(element(by.id('uniqueId'))).toHaveValue('Some value').withTimeout(2000);
await expect(element(by.id('uniqueId'))).toHaveValue('Some value');

withTimeout(millis)

Waits for the condition to be met until the specified time (millis) have elapsed.

await waitFor(element(by.id('UniqueId336'))).toExist().withTimeout(2000);
await expect(element(by.id('UniqueId336'))).toExist();

whileElement(element)

Performs the action repeatedly on the element until an expectation is met.

await waitFor(element(by.text('Text5'))).toBeVisible().whileElement(by.id('ScrollView630')).scroll(50, 'down');
await expect(element(by.text('Text5'))).toBeVisible();