alezvic
alezvic

Reputation: 374

Appium fails to locate element off-screen using UiSelector

I'm starting to automate a test suite for a mobile app coded in NativeScript (it used to be a hybrid Cordova app) and it's proving difficult to locate some elements.

I'm trying to locate a TextView widget that's outside of the visible screen space (AKA viewport) using UiSelector:

@AndroidFindBy(uiAutomator = "new UiSelector().textContains(\"CFT\")")
private MobileElement labelCFT;

When I try to interact with such element, the result is the following message:

org.openqa.selenium.NoSuchElementException: Can't locate an element 
by this strategy: By.chained({By.AndroidUIAutomator: 
new UiSelector().textContains("CFT")})

The logic conclusion would be that the element does not exist or my locator strategy is faulty. But here is the thing, when I change the text to find for that of an element that's inside the visible space/viewport, the locator works flawlessly. Example:

@AndroidFindBy(uiAutomator = "new UiSelector().textContains(\"loans\")")
private MobileElement labelCFT;

And then:

public void whatText() {
    System.out.println("Text of the label: " + labelCFT.getText());
}

I get the correct "Text of the label: These are your loans".

Apparently, it's a limitation of the UiSelector or at least the way Appium works with it.

The only option I imagine is to scroll the whole screen and then trigger @AndroidFindBy, then repeat until there's no scroll left.

Is this suppose to be how UiSelector and textContains() work? Is it another solution for this?

Many thanks.

Upvotes: 4

Views: 2441

Answers (2)

akshay patil
akshay patil

Reputation: 688

In Android App you can only click or do any actions on the elements which are visible on the screen if you want to perform any actions on element which are not visible you can use touch action method where you need to specify x and y cordinated i think this might help you in my case this works

TouchAction ta = new TouchAction(driver);
            ta.press(PointOption.point(207, 582)).moveTo(PointOption.point(8, -360)).release().perform();

Upvotes: 0

Nauman Malik
Nauman Malik

Reputation: 188

I have faced similar type of problems in automation. The only way around I found was to scroll up or down till the element is visible on screen and then access the element by "name". you can use the following command for scrolling.

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"Your Element\"));");

After your desired element is visible on the screen you can access that element easily.

Thanks

Upvotes: 0

Related Questions