zoukankan      html  css  js  c++  java
  • AndroidDragAndDrop.java

    以下代码使用ApiDemos-debug.apk进行测试

     1 package com.saucelabs.appium;
     2 
     3 import io.appium.java_client.AppiumDriver;
     4 import io.appium.java_client.MobileElement;
     5 import io.appium.java_client.TouchAction;
     6 import io.appium.java_client.android.AndroidDriver;
     7 import junit.framework.Assert;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.remote.DesiredCapabilities;
    13 
    14 import java.io.File;
    15 import java.net.URL;
    16 
    17 /**
    18  * Created by saikrisv on 26/04/16.
    19  */
    20 public class AndroidDragAndDrop {
    21     private AppiumDriver<WebElement> driver;
    22 
    23     @Before
    24     public void setUp() throws Exception {
    25         //File classpathRoot = new File(System.getProperty("user.dir"));
    26         File appDir = new File("E:/package");
    27         File app = new File(appDir, "ApiDemos-debug.apk");
    28         DesiredCapabilities capabilities = new DesiredCapabilities();
    29         capabilities.setCapability("deviceName","XiaoMiShouJi");
    30         capabilities.setCapability("platformVersion", "4.4");
    31         capabilities.setCapability("app", app.getAbsolutePath());
    32         capabilities.setCapability("appPackage", "io.appium.android.apis");
    33         capabilities.setCapability("appActivity", ".ApiDemos");
    34         driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    35     }
    36 
    37     @After
    38     public void tearDown() throws Exception {
    39         driver.quit();
    40     }
    41 
    42     @Test
    43     public void testDragAndDrop() throws InterruptedException {
    44         driver.findElementByXPath(".//*[@text='Views']").click();
    45         driver.findElementByXPath(".//*[@text='Drag and Drop']").click();
    46         MobileElement calc = (MobileElement) driver.findElementById("io.appium.android.apis:id/drag_dot_1");
    47         TouchAction touchAction = new TouchAction(driver);
    48         touchAction.press(calc).perform();
    49         try {
    50             Thread.sleep(5000);
    51         } catch (InterruptedException e) {
    52             e.printStackTrace();
    53         }
    54         touchAction.moveTo(driver.findElementById("io.appium.android.apis:id/drag_dot_2")).release().perform();
    55         Thread.sleep(5000);
    56         Assert.assertEquals(driver.findElementById("io.appium.android.apis:id/drag_result_text").getText(),"Dropped!");
    57     }
    58 }

    上面的代码运行报错,根据appium的问题单Cant perform drag'n'drop using appium #3104得知:drag-and-drop过程中,不能有wait操作,所以去掉这里的sleep。更改后代码如下:可以正常运行。

     1 package com.saucelabs.appium;
     2 
     3 import io.appium.java_client.AppiumDriver;
     4 import io.appium.java_client.MobileElement;
     5 import io.appium.java_client.TouchAction;
     6 import io.appium.java_client.android.AndroidDriver;
     7 import junit.framework.Assert;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.remote.DesiredCapabilities;
    13 
    14 import java.io.File;
    15 import java.net.URL;
    16 
    17 /**
    18  * Created by saikrisv on 26/04/16.
    19  */
    20 public class AndroidDragAndDrop {
    21     private AppiumDriver<WebElement> driver;
    22 
    23     @Before
    24     public void setUp() throws Exception {
    25         //File classpathRoot = new File(System.getProperty("user.dir"));
    26         File appDir = new File("E:/package");
    27         File app = new File(appDir, "ApiDemos-debug.apk");
    28         DesiredCapabilities capabilities = new DesiredCapabilities();
    29         capabilities.setCapability("deviceName","XiaoMiShouJi");
    30         capabilities.setCapability("platformVersion", "4.4");
    31         capabilities.setCapability("app", app.getAbsolutePath());
    32         capabilities.setCapability("appPackage", "io.appium.android.apis");
    33         capabilities.setCapability("appActivity", ".ApiDemos");
    34         driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    35     }
    36 
    37     @After
    38     public void tearDown() throws Exception {
    39         driver.quit();
    40     }
    41 
    42     @Test
    43     public void testDragAndDrop() throws InterruptedException {
    44         driver.findElementByXPath(".//*[@text='Views']").click();
    45         driver.findElementByXPath(".//*[@text='Drag and Drop']").click();
    46         MobileElement calc = (MobileElement) driver.findElementById("io.appium.android.apis:id/drag_dot_1");
    47         MobileElement target = (MobileElement) driver.findElementById("io.appium.android.apis:id/drag_dot_2");
    48         TouchAction touchAction = new TouchAction(driver);
    49         touchAction.longPress(calc,5000).moveTo(target).release().perform();
    50         Thread.sleep(5000);
    51         Assert.assertEquals(driver.findElementById("io.appium.android.apis:id/drag_result_text").getText(),"Dropped!");
    52     }
    53 }

    用到的定位方式:

    Xpath: driver.findElementByXPath(".//*[@text='Views']").click(); 

    ID: MobileElement calc = (MobileElement) driver.findElementById("io.appium.android.apis:id/drag_dot_1"); 

    用到的操作:

    click: driver.findElementByXPath(".//*[@text='Drag and Drop']").click(); 

    drag-and-drop: touchAction.longPress(calc,5000).moveTo(target).release().perform(); 

  • 相关阅读:
    边界扫描的测试原理及九大指令
    边界扫描(boundary scan)
    setup&hold
    使用InstallShield打包windriver驱动-转
    一个完整的Installshield安装程序实例-转
    高通Trustzone and QSEE介绍
    Linux ALSA声卡驱动之五:移动设备中的ALSA(ASoC)
    Linux驱动中completion接口浅析(wait_for_complete例子,很好)
    dmesg 的时间戳处理
    linux popen函数
  • 原文地址:https://www.cnblogs.com/superbaby11/p/6115120.html
Copyright © 2011-2022 走看看