Reputation: 1559
I would like to know how to scroll down to click the element in Android using appium and java?
I am having a list of elements inside "android.support.v7.widget.RecyclerView
". Since it has more than 10 elements, we need to swipe the screen to see the below elements.
Each element has same id which is "com.osanda.exampleapp/textViewTitle
". But their texts are different like "Apple", "Orange", "Grapes"......
All I need is to scroll and click the relevant element using its text("Apple", "Orange", "Grapes".....)
I have followed many tutorials but couldn't do it properly. I have managed to scroll down the screen. But it won't work when the element is in the middle position of the scroll.
When I listed the element names it only shows the visible elements, not all elements.
List<WebElement> elements = androidDriver.findElementByClassName("android.support.v7.widget.RecyclerView").findElements(By.id("com.osanda.exampleapp:id/textViewTitle"));
for(WebElement element : elements) {
System.out.println(element.getText());
}
Thank You.
Upvotes: 8
Views: 71584
Reputation: 2050
If you are using the Appium Java client version above 6, you can't use findElementByAndroidUIAutomator
method on the Appium driver. MobileBy
is deprecated too, so a workable solution today is:
public void scrollAndClick(String visibleText) {
androidDriver.findElement(AppiumBy.androidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))")).click();
}
}
Upvotes: 0
Reputation: 81
You could use this one
public void scrollAndClick(String visibleText) {
androidDriver.findElementByAndroidUIAutomator
("new UiScrollable(new UiSelector().scrollable(true).
instance(0)).scrollIntoView(
new UiSelector().textContains(\""+visibleText+
"\").instance(0))").click();
}
}
or this one:
String uiSelector = "new UiSelector().textMatches
(\"" + text+ "\")";
String command = "new UiScrollable(
new UiSelector().scrollable(true).
instance(0)).scrollIntoView("+ uiSelector + ");";
driver.findElementByAndroidUIAutomator(command);
It will scroll till the text is visible.
Upvotes: 0
Reputation: 162
This Works for me, scroll will stop when the text is visible. here my Text is : "Progress Bar", Just replace according to your text. it will work.
String scrollElement = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Progress Bar\").instance(0))";
driver.findElementByAndroidUIAutomator(scrollElement).click();
Upvotes: 0
Reputation: 1559
I tried this solution and it is worked for me.
public void scrollAndClick(String visibleText) {
androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))").click();
}
}
Upvotes: 21
Reputation: 1868
In new versions of Appium you can use this:
TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();
element.click();
Upvotes: 4
Reputation: 240
Use the below while loop which looks for the contain by swiping down through the screen till the expected element appears
While (driver.findElements(By.id(“id”)).size()==0){
size = driver.manage().window().getSize();
int starty = (int) (size.height * 0.80);
int endy = (int) (size.height * 0.20);
int startx = size.width / 2;
System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);
driver.swipe(startx, starty, startx, endy, 3000);
Thread.sleep(2000);
driver.swipe(startx, endy, startx, starty, 3000);
Thread.sleep(2000);
}
once your element detected while exits and you can execute your action.
Upvotes: 0
Reputation: 786
public AndroidElement ScrollToElement(String by, String using) {
AndroidElement element = null;
int numberOfTimes = 10;
Dimension size = driver.manage().window().getSize();
int anchor = (int) (size.width / 2);
// Swipe up to scroll down
int startPoint = (int) (size.height - 10);
int endPoint = 10;
for (int i = 0; i < numberOfTimes; i++) {
try {
new TouchAction(driver)
.longPress(point(anchor, startPoint))
.moveTo(point(anchor, endPoint))
.release()
.perform();
element = (AndroidElement) driver.findElement(by, using);
i = numberOfTimes;
} catch (NoSuchElementException ex) {
System.out.println(String.format("Element not available. Scrolling (%s) times...", i + 1));
}
}
return element;
}
In your test use as follows:
Example:
String usingWebView = “//android.widget.TextView[@text=‘Spinner’]”;
String by = “xpath”;
MobileActions mbAct = new MobileActions(driver); //This is just a class which has above function code.
AndroidElement webViewElement = mbAct.ScrollToElement(by, usingWebView, 250);
webViewElement.click();
Upvotes: 1
Reputation: 39
findElementByAndroidUIAutomator--AndroidUiAutomater
is depriciated in latest release,
Where as Now code which is working looks like
MobileElement element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"com.android.vending:id/tab_recycler_view\")).getChildByText("
+ "new UiSelector().className(\"android.widget.TextView\"), \"Games We Are Playing\")"));
//Perform the action on the element
element.click();
Upvotes: 0
Reputation: 215
Please use the code below. It will scroll till the text is visible.
String uiSelector = "new UiSelector().textMatches(\"" + text
+ "\")";
String command = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("
+ uiSelector + ");";
driver.findElementByAndroidUIAutomator(command);
Now you could perform the click action after this.
Upvotes: 4