问题
I'm having trouble with scrolling down to a certain element in an iOS and Android app. Since the update from Appium 1.6.3 to 1.7.1 and io.appium to 6.1.0 the swipe method is deprecated and the only solution for it is to use TouchActions.
I tried to solve it with TouchActions but it didn't scroll at all or the scroll direction was wrong.
My solution so far looks like this, maybe someone can explain what I'm doing wrong:
public void scrollDownUntilElementVisible(WebElement element){
TouchAction touchAction = new TouchAction(getDriver());
for(int i=0; i<dimensions.getHeight();i++){
if(element.isDisplayed()){
break;
}else{
touchAction.press(0,0).moveTo(element).release().perform();
}
}
}
It's not the complete code, but I hope you get the idea.
How would it work if I would use the x,y-coordinates instead of the webElement I look for in my example? It does not work like the swipe method from the version before, or I didn't do it right. Maybe someone can explain it.
回答1:
I needed scroll to find elements which are out of the screen. What I come up with is:
- Search for the element you need on the current/visible screen.
- If the element is not found (i.e. out of the screen) -
scrollDown
(in my case I was needed only scroll down) and go to step 1 again. I had limited the test with 4 iterations as it was enough in my case, so use your own condition here.
private void scrollDown() {
//if pressX was zero it didn't work for me
int pressX = driver.manage().window().getSize().width / 2;
// 4/5 of the screen as the bottom finger-press point
int bottomY = driver.manage().window().getSize().height * 4/5;
// just non zero point, as it didn't scroll to zero normally
int topY = driver.manage().window().getSize().height / 8;
//scroll with TouchAction by itself
scroll(pressX, bottomY, pressX, topY);
}
/*
* don't forget that it's "natural scroll" where
* fromY is the point where you press the and toY where you release it
*/
private void scroll(int fromX, int fromY, int toX, int toY) {
TouchAction touchAction = new TouchAction(driver);
touchAction.longPress(fromX, fromY).moveTo(toX, toY).release().perform();
}
P.S. you could get coordinates from the element and use it in scroll
.
P.S.S. I used appium 1.6.5
回答2:
On Latest Version of Appium need to add(PointOption.point while passing the coordinates) some code for scrolling using TouchAction:
private void scrollDown() {
//if pressX was zero it didn't work for me
int pressX = driver.manage().window().getSize().width / 2;
// 4/5 of the screen as the bottom finger-press point
int bottomY = driver.manage().window().getSize().height * 4/5;
// just non zero point, as it didn't scroll to zero normally
int topY = driver.manage().window().getSize().height / 8;
//scroll with TouchAction by itself
scroll(pressX, bottomY, pressX, topY);
}
private void scroll(int fromX, int fromY, int toX, int toY) {
TouchAction touchAction = new TouchAction(driver);
touchAction.longPress(PointOption.point(fromX, fromY)).moveTo(PointOption.point(toX, toY)).release().perform();
}
来源:https://stackoverflow.com/questions/44282417/how-to-scroll-with-appium-1-7-1-using-touchaction