如何使用Java中的appium为Android本机应用程序执行滑动

时间:2014-04-28 11:20:09

标签: android junit junit4 swipe appium

我需要刷我的应用程序(从左到右,从右到左),这里我使用Java appium for android native app automation。

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

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

7 个答案:

答案 0 :(得分:10)

以下是我们的工作方式 -

向左滑动

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的一个实例

答案 1 :(得分:2)

不推荐使用

swipe()方法。

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

向左滑动将执行:

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

向右滑动将执行:

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

此处,驱动程序是您的驱动程序,如AndroidDriverRemoteWebDriverAppiumDriver

250,1200和其他是您的应用程序视图坐标,您可以在位于android-sdk platform-tools下的UIAutomaterView批处理文件中看到。

答案 2 :(得分:0)

使用以下代码在Junit for the android native app中使用appium轻扫,

    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

答案 3 :(得分:0)

$cat /etc/systemd/system/docker.service.d/http_proxy.conf 
[Service]
Environment="HTTP_PROXY=http://**.**.**.**:3128/"    "HTTPS_PROXY=http://**.**.**.**:3128/" "NO_PROXY=localhost,127.0.0.1,192.168.2.100,192.168.2.101,192.168.2.102,192.168.2.103,192.168.2.104,192.168.2.105,192.168.2.106,192.168.2.107,192.168.2.108,192.168.2.194,192.168.2.110"

答案 4 :(得分:0)

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);
    }

答案 5 :(得分:0)

@ABDUL SATHAR BEIGH:绝对正确。只是补充一点,对于我在Android上工作,我必须通过(AndroidDriver)驱动程序对其进行类型转换,如果上述方法不适合你。以下内容适用于此修改。 这是向右滑动。

((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);

答案 6 :(得分:0)

 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