Locators in Appwright are used to select and interact with elements within your mobile app.
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:
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+/);
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 });
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"]`);
To tap an element, you can use the tap
method.
await device.getByText('Submit').tap();
To enter text into an element, you can use the fill
method.
await device.getByText('Search').fill('Wikipedia');
To send key strokes to an element, you can use the sendKeyStrokes
method.
await device.getByText('Search').sendKeyStrokes('Wikipedia');
To extract text from an element, you can use the getText
method.
const text = await device.getByText('Playwright').getText();
To check if an element is visible on the screen, you can use the isVisible
method.
const isVisible = await device.getByText('Playwright').isVisible();
To scroll the screen, you can use the scroll
method.
await device.getByText("Playwright").scroll(ScrollDirection.DOWN);