Android devices comes in variety of sizes and dimensions and as UiAutomator is completely decoupled from the source code of your app, unless it can see something on the screen, it has no idea that it even exists. Tests need to be run on a variety of devices, what may be immediately visible on one device may not be on another device. So to avoid “object not found exception “we must script the object smartly and always think of this scenario while building UI Automator scripts.
Note: Technique below is just a sample and works in most of the cases especially where ListViews and RelativeLayouts are concerned, You may have to implement different techniques depending on your application.
Note: Technique below is just a sample and works in most of the cases especially where ListViews and RelativeLayouts are concerned, You may have to implement different techniques depending on your application.
The way we normally code UI objects having the text “Test “
new UiObject(
new UiSelector().text(“Test”)
).click();
new UiObject(
new UiSelector().text(“Test”)
).click();
However, there will almost certainly be devices that are too small for the section containing this text to appear in the first page, so UiAutomator won’t know it exists and will return a exception ”Object not found”. So we always use UIScrollable with objects and declare object as
new UiScrollable(
new UiSelector().scrollable(true)
).scrollIntoView(
new UiSelector().text(“Text”)
);
new UiObject(
new UiSelector().text(“Text”)
).click();
new UiScrollable(
new UiSelector().scrollable(true)
).scrollIntoView(
new UiSelector().text(“Text”)
);
new UiObject(
new UiSelector().text(“Text”)
).click();
UiAutomator will now scroll through entire page until it finds the text “Test”. If the text “Test” is already visible on the first page, it’ll click. This method will have a short performance hit over the initial method while it looks for the text but this is an incredibly simple extra step to take and a great habit to get into for robust tests.
No comments:
Post a Comment