AndroidDragAndDrop.java
Posted 超级宝宝11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于AndroidDragAndDrop.java的主要内容,如果未能解决你的问题,请参考以下文章
Java中普通代码块,构造代码块,静态代码块区别及代码示例2 构造代码块