Skip to content

Latest commit

 

History

History
99 lines (62 loc) · 2.42 KB

locators.md

File metadata and controls

99 lines (62 loc) · 2.42 KB

Locators

Locators in Appwright are used to select and interact with elements within your mobile app.

How to select an Element

In Appwright, you can select an element on the screen using the device object. The device object provides various methods to locate elements by text, ID, or XPath. Here's how you can select elements:

Get an element by Text

You can use the getByText method to select elements by their visible text on the screen.

const element = await device.getByText('Submit');

Above method defaults to a substring match, and this can be overridden by setting the exact option to true.

const element = await device.getByText('Submit', { exact: true });

We can also use the getByText method to select elements using Regex patterns.

const counter = device.getByText(/^Counter: \d+/);

Get an element by ID

You can use the getById method to select elements by their ID on the screen.

const element = await device.getById('signup_button');

Above method defaults to a substring match, and this can be overridden by setting the exact option to true.

const element = await device.getById('signup_button', { exact: true });

Get an element by XPath

You can use the getByXpath method to select elements by their XPath on the screen.

const element = await device.getByXpath(`//android.widget.Button[@text="Confirm"]`);

How to Take Actions on the Element

Tapping an element

To tap an element, you can use the tap method.

await device.getByText('Submit').tap();

Enter text in a text field

To enter text into an element, you can use the fill method.

await device.getByText('Search').fill('Wikipedia');

Sending key strokes to an element

To send key strokes to an element, you can use the sendKeyStrokes method.

await device.getByText('Search').sendKeyStrokes('Wikipedia');

Extracting text from an element

To extract text from an element, you can use the getText method.

const text = await device.getByText('Playwright').getText();

Check for visibility of an element

To check if an element is visible on the screen, you can use the isVisible method.

const isVisible = await device.getByText('Playwright').isVisible();

Scroll screen

To scroll the screen, you can use the scroll method.

await device.getByText("Playwright").scroll(ScrollDirection.DOWN);