如何将Android Swipe转换为Android TouchAction?

时间:2018-02-23 15:29:09

标签: java android eclipse appium

我正在使用AppiumStudio工具在我创建的Android Applicative上运行一些测试。 该工具自动生成Java代码,我将该代码用于在Eclipse上进行测试。但是当我粘贴代码时,Java无法识别driver.swipe();即使完成了所有进口。 这是工具生成的代码

package Appium;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.junit.*;
import java.net.URL;
import java.net.MalformedURLException;

public class Cadastro_base {
 private String reportDirectory = "reports";
 private String reportFormat = "xml";
 private String testName = "Cadastro_base";
 protected AndroidDriver<AndroidElement> driver = null;

 DesiredCapabilities dc = new DesiredCapabilities();

 @Before
 public void setUp() throws MalformedURLException {
  dc.setCapability("reportDirectory", reportDirectory);
  dc.setCapability("reportFormat", reportFormat);
  dc.setCapability("testName", testName);
  dc.setCapability(MobileCapabilityType.UDID, "5210ce98fa7eb4b3");
  driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), dc);
 }

 @Test
 public void testCadastro_base() {
  driver.findElement(By.xpath("//*[@id='emailView']")).sendKeys("teduspwrpbrasil@mailinator.com");
  driver.swipe(500, 596, 518, 478, 471);
  driver.swipe(434, 468, 471, 312, 417);
  driver.findElement(By.xpath("//*[@id='password_view']")).sendKeys("Smart2000");
  new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='confirm_password_view']")));
  driver.findElement(By.xpath("//*[@id='confirm_password_view']")).sendKeys("Smart2000");
  driver.swipe(734, 368, 737, 212, 813);
  driver.swipe(290, 987, 346, 571, 2003);
  driver.findElement(By.xpath("//*[@id='name_and_surname_view']")).sendKeys("TED USP");
  driver.swipe(409, 518, 493, 256, 1468);
  driver.findElement(By.xpath("//*[@id='cpfView']")).sendKeys("41801452865");
  driver.swipe(531, 465, 637, 175, 1724);
  driver.findElement(By.xpath("//*[@id='dobView']")).click();
  driver.findElement(By.xpath("//*[@text='OK']")).click();
  driver.swipe(650, 481, 756, 215, 1627);
  driver.findElement(By.xpath("//*[@id='phoneView']")).sendKeys("13982133161");
  driver.swipe(446, 800, 468, 675, 1359);
  driver.findElement(By.xpath("//*[@id='generView']")).click();
  driver.findElement(By.xpath("//*[@text='Feminino']")).click();
  driver.swipe(584, 712, 590, 609, 1013);
  driver.findElement(By.xpath("//*[@text='Cadastrar']")).click();
 }

 @After
 public void tearDown() {
     driver.quit();
 }
}

我的问题是:要正确运行测试,我需要在屏幕上的字段之间滑动。如何将命令driver.swipe转换为TouchAction或将执行触摸和移动操作的工作命令?

1 个答案:

答案 0 :(得分:0)

您应该使用TouchAction(https://appium.github.io/java-client/io/appium/java_client/TouchAction.html

在java client 6 beta中使用当前已弃用的方法:

new TouchAction(driver).press(startX, startY).waitAction(Duration.ofMillis(duration)).moveTo(endX, endY).release().perform();

使用新方法:

new TouchAction(localdriver).press(point(startX, startY)).waitAction(waitOptions(Duration.ofMillis(duration)))
            .moveTo(point(endX, endY)).release().perform();

startX,startY,endX,endY是你的坐标 持续时间是以毫秒为单位的滑动时间(尝试1000)