【问题标题】:How to convert Android Swipe to Android TouchAction?如何将 Android Swipe 转换为 Android TouchAction?
【发布时间】:2018-08-03 16:59:24
【问题描述】:

我正在使用 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 或将执行触摸和移动操作的工作命令?

【问题讨论】:

    标签: java android eclipse appium


    【解决方案1】:

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

    在 java 客户端 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 是你的坐标 duration 是以毫秒为单位的滑动时间(尝试 1000)

    【讨论】:

    • 我尝试使用 new TouchAction(driver).longPress(1046,1221).moveTo(1046,1504).perform().release();但它不执行操作,并且大多数 TouchAction 方法已被弃用:/
    • 在 Duration.ofMillis(1000) 上,我收到了这个:The method ofMillis(int, null) is undefined for the type Duration
    • 其导入 java.time.Duration;
    • 我按照你说的做了一切。代码没有显示任何错误,但是当我运行代码时它失败并显示此异常:源附件不包含文件 TouchAction.class 的源
    • 您没有正确的 java-client 库或未在类路径上正确加载它。例如,尝试使用 maven 为您管理依赖项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多