页面滚动没有发生appium java client

时间:2017-09-15 11:18:41

标签: java appium

我正在为我的应用程序进行自动化移动测试。在输入用户名和输入后的登录页面中密码,键盘 弹出并屏蔽屏幕上的所有内容。所以它抛出使用给定的搜索参数org.openqa.selenium.NoSuchElementException,无法在页面上找到元素: 为了启用滚动或屏幕触摸我尝试了很多方法。

1)
WebElement element1 = driver.findElement(MobileBy.AccessibilityId("btnLogin"));
	  	  int x = element1.getLocation().getX();
	  	  int y = element1.getLocation().getY();
		  TouchAction action = new TouchAction((PerformsTouchActions) driver);
	  action.press(x,y).moveTo(x,y-90).release().perform();
    
    2)
    WebElement element1 = driver.findElement(MobileBy.AccessibilityId("btnLogin"));
	  ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);",element1);
    
    3)
    
     TouchAction action = new TouchAction((PerformsTouchActions) driver);
	  WebElement element1 = driver.findElement(MobileBy.AccessibilityId("com.a.b:id/imageView"));
	  WebElement element2 = driver.findElement(MobileBy.AccessibilityId("com.a.b:id/btnLogin"));
	  action.press(element1).moveTo(element2).release();
    
    4)
    JavascriptExecutor js = (JavascriptExecutor) driver;
	  HashMap<String, String> scrollObject = new HashMap<String, String>();
	  scrollObject.put("direction", "up");
	  scrollObject.put("element", "btnLogin");
	  js.executeScript("mobile: scroll", scrollObject);
    
    5)
    ((AndroidDriver) driver).context("NATIVE_APP");
	  WebElement element = driver.findElementById("btnLogin");
	  int x = element.getSize().getWidth();
    int xEnd = 0;
	  int yStart = element.getSize().getHeight()/2;
	  ((AndroidDriver) driver).swipe(x, yStart, xEnd, yStart, 500);

我无法解决此问题..请您使用一些示例代码向我建议以解决此问题。

2 个答案:

答案 0 :(得分:1)

要隐藏键盘,您可以使用以下方法driver.hideKeyboard()。这项工作与AppiumDriver。

输入您的用户名和密码后,您可以使用以下代码:

driver.hidekeyboard();

答案 1 :(得分:0)

public static void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.width * anchorPercentage);
    int startPoint = (int) (size.height * startPercentage);
    int endPoint = (int) (size.height * finalPercentage);
    new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(anchor, endPoint).release().perform();
}

调用上述方法,如:

向上滚动:swipeVertical((AppiumDriver)driver,0.9,0.1,0.5,3000);

向下滚动:swipeVertical((AppiumDriver)driver,0.1,0.9,0.5,3000);