Android原生应用中的滚动问题

时间:2017-05-18 08:30:23

标签: javascript android scroll appium

我正在使用appium和java来自动化我的应用程序。我需要在页面中滚动直到特定的文本/元素。我两天都在苦苦挣扎,并尝试了许多解决方案,但它有效。列出所有解决方案和场景:

Appium版本:1.4.0 Java客户端版本:4.1.2 驱动程序:RemoteWebDriver

Solution1.1: 尝试使用Java Script。这是代码:

JavascriptExecutor js = (JavascriptExecutor)driver;
HashMap scrollObjects = new HashMap();
scrollObjects.put("direction", "down");
scrollObjects.put("text", "Yes");
js.executeScript("mobile: swipe", scrollObjects)

解决方案1.2:

WebElement wb = driver.findElement(By.xpath("//*[@resource-id =           'com.practo.fabric:id/toolbar']/following-sibling::android.widget.LinearLayout"));
JavascriptExecutor js = (JavascriptExecutor)driver;
HashMap scrollObjects = new HashMap();
scrollObjects.put("direction", "down");
scrollObjects.put("text", "Yes");
scrollObjects.put("element",wb);
js.executeScript("mobile: swipe", scrollObjects);

这里的问题是我没有列表视图。我有线性布局,所以尝试过提供线性布局xpath一次,但没有运气。

溶液2:

org.openqa.selenium.Dimension size =driver.manage().window().getSize();     
int starty = (int) (size.height * 0.80); 
int endy = (int) (size.height * 0.20);
int startx = size.width / 2; 
driver.swipe(startx, starty, startx, endy, 3000); 
System.out.println("swiping is done ");

这里driver.swipe给出错误,因为我是RemoteWebDriver。当我尝试使用AndroidDriver时,它会被弃用。

在这种情况下我该怎么办?

2 个答案:

答案 0 :(得分:0)

由于appium服务器和java客户端库版本不匹配,您面临问题。请将appium服务器更新为1.5 / 1.6,否则可以使用java-client library version 2.1.0。

如果您选择将java-client2.1.0与appium 1.4一起使用,我们将使用名为“scrollTo”的方法滚动到特定文本。我也使用了这个组合中的解决方案2中提到的“滑动”。

driver.scrollTo("Like"); //Scrolls to Like text in the app

答案 1 :(得分:0)

我通过使用java-client中的AppiumDriver解决了这个问题。由于我使用的是最新的java-client版本(5.0.0-BETA6),因此称“刷卡”方法已被弃用。但它的确有效。

driver.swipe(300, 701, 300, 441, 3000);

也使用TouchAction类解决了它。

TouchAction action = new TouchAction(driver);   
action.press(300, 701).waitAction(2000).moveTo(300, 441).release();
action.perform();

注意:坐标是硬编码的。