【问题标题】:How to perform swipe using appium in Java for android native app如何在 Java 中使用 appium 为 android 本机应用程序执行滑动
【发布时间】:2014-06-13 22:04:52
【问题描述】:

我需要滑动我的应用程序(从左到右和从右到左),而我在 appium 中使用 Java 来实现 android 本机应用程序自动化。

我已经尝试过这个链接, Swipe method not working in android automation testing

但我不能,请任何其他链接分享或任何人帮助我。

【问题讨论】:

  • 你可以参考下面的 topic 和同样的上下文。

标签: android junit junit4 swipe appium


【解决方案1】:

这是我们的做法-

向左滑动-

appiumDriver.context("NATIVE_APP"); 
Dimension size = appiumDriver.manage().window().getSize(); 
int startx = (int) (size.width * 0.8); 
int endx = (int) (size.width * 0.20); 
int starty = size.height / 2; 
appiumDriver.swipe(startx, starty, endx, starty, 1000);

向右滑动-

appiumDriver.context("NATIVE_APP"); 
Dimension size = appiumDriver.manage().window().getSize(); 
int endx = (int) (size.width * 0.8); 
int startx = (int) (size.width * 0.20); 
int starty = size.height / 2; 
appiumDriver.swipe(startx, starty, endx, starty, 1000);

这里的appiumDriver是io.appium.java_client.AppiumDriver的一个实例

【讨论】:

  • 如果我们在 webview 上下文中怎么办?
【解决方案2】:

swipe() 方法已弃用。

所以我们可以使用下面的方法。

向左滑动将执行为:

new TouchAction(driver).longPress(250, 1200).moveTo(900, 1200).release().perform();

向右滑动将执行为:

new TouchAction(driver).longPress(1000, 450).moveTo(500, 450).release().perform();

这里,driver 是您的驱动程序,例如 AndroidDriverRemoteWebDriverAppiumDriver

而250、1200等是你的应用视图坐标,你可以在android-sdk平台工具下的UIAutomaterView批处理文件中看到。

【讨论】:

  • 如果您在 Appium 中使用 Selendroid 作为 automationName,该解决方案将不起作用
【解决方案3】:

使用以下代码在 Junit 中为 Android 原生应用使用 appium 进行 Swipe,

    public void swipe()
    {  
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, Double> swipeObject = new HashMap<String, Double>();
    swipeObject.put("startX", 0.95);
    swipeObject.put("startY", 0.5);
    swipeObject.put("endX", 0.05);
    swipeObject.put("endY", 0.5);
    swipeObject.put("duration", 1.8);
    js.executeScript("mobile: swipe", swipeObject);
     }

//在需要滑动的地方调用这个方法,

    swipe(); //it will call the swipe method

我已经尝试过了,并在android原生应用中成功刷卡。

这可能会有所帮助:

https://github.com/appium/appium/blob/master/docs/en/gestures.md

【讨论】:

    【解决方案4】:
    public void swipeUpElement(AppiumDriver<WebElement> driver, WebElement element, int duration){
        int bottomY = element.getLocation().getY()-200;
        driver.swipe(element.getLocation().getX(), element.getLocation().getY(), element.getLocation().getX(), bottomY, duration);
    }
    

    【讨论】:

    • 虽然这段代码可以回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用处。
    • 在上面的代码中 - 我得到要滚动的元素,然后是它的开始和结束位置以及应该执行滑动功能的持续时间。
    【解决方案5】:
    public void swipingHorizontally() throws MalformedURLException, InterruptedException{
            DesiredCapabilities();
            Dimension size = driver.manage().window().getSize();
             System.out.println(size);
             int startx = (int) (size.width * 0.70);
             int endx = (int) (size.width * 0.30);
             int starty = size.height / 2;
             driver.swipe(startx, starty, endx, starty, 2000); // it swipes from right to left
             driver.swipe(endx, starty, startx, starty, 2000); // it swiptes from left to right
              }
             Thread.sleep(2000);
        }
    

    【讨论】:

      【解决方案6】:

      @ABDUL SATHAR BEIGH:完全正确。只是补充一下,如果以上对您不起作用,我必须通过(AndroidDriver)驱动程序对它进行类型转换,以便我在 Android 上工作。以下与此修改一起使用。 这是向右滑动。

      ((AndroidDriver) driver).context("NATIVE_APP");
         Dimension size = driver.manage().window().getSize();
         int endx = (int) (size.width * 0.8);
         int startx = (int) (size.width * 0.20);
         int starty = size.height / 2;
         ((AndroidDriver) driver).swipe(startx, starty, endx, starty, 1000);
      

      【讨论】:

        【解决方案7】:
         public void leftRightSwipe(int timeduration) {
          // duration should be in milliseconds
          size = driver.manage().window().getSize();
          System.out.println(size);
          startx = (int) (size.width * 0.70);
          endx = (int) (size.width * 0.30);
          starty = size.height / 2;
          System.out.println("Start swipe operation");
          driver.swipe(endx, starty, startx, starty, timeduration);
        
         }
        
        public void rightLeftSwipe(int timeduration) {
        
          size = driver.manage().window().getSize();
          System.out.println(size);
          startx = (int) (size.width * 0.70);
          endx = (int) (size.width * 0.30);
          starty = size.height / 2;
          System.out.println("Start swipe operation");
          driver.swipe(startx, starty, endx, starty, timeduration);
        
         }
        

        如果您想详细了解请参考这里 - http://www.qaautomated.com/2017/10/swipe-rightleftup-down-using-appium.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-26
          • 1970-01-01
          • 1970-01-01
          • 2017-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-04
          相关资源
          最近更新 更多