首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将swipe更改为TouchAction()方法

如何将swipe更改为TouchAction()方法
EN

Stack Overflow用户
提问于 2019-02-07 12:59:19
回答 3查看 2.4K关注 0票数 4

我一直有一个错误时,试图使用刷,但它似乎是贬值。建议使用TouchAction(),但我不确定如何将swipe转换为TouchAction()

下面是代码:

代码语言:javascript
运行
复制
public void obj_scrollToObjectByCoOrds(int coOrds) throws IOException {
        try {

            List<WebElement> elements = driver.findElements(By.className("android.widget.FrameLayout"));
            Point point = elements.get(1).getLocation();
            Dimension size = elements.get(1).getSize();

            int elementCenterX = point.getX() + Math.round(size.getWidth() / 2);
            int elementCenterY = point.getY() + Math.round(size.getHeight() / 2);

            String originalContext = driver.getContext();
            driver.context("NATIVE_APP");

            driver.swipe(elementCenterX, elementCenterY, elementCenterX, elementCenterY + coOrds, 1500);
            driver.context(originalContext);




        } catch (Exception e) {
            e.printStackTrace();
        }

    }

下面是一个很难实现的示例touchAction,所以我现在把它作为示例。我想了解转换是如何发生的,因此,通过将下面的代码与答案进行比较,我发现了如下问题:

代码语言:javascript
运行
复制
Dimension screenSize = driver.manage().window().getSize();
new TouchAction(driver)
  .press((int) (screenSize.width * 0.6), 130)
  .waitAction(500)
  .press((int) (screenSize.width * 0.3), 130)
  .release()
  .perform();
EN

回答 3

Stack Overflow用户

发布于 2019-02-11 06:14:36

我又举了一个例子,帮助你理解我在android应用程序中从左向右滑动,希望你能理解。

代码语言:javascript
运行
复制
package Android;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SwipeAction {
 AndroidDriver driver;
 Dimension size;
 WebDriverWait wait;
 @BeforeTest
 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "com.fortysevendeg.android.swipelistview");
  capabilities.setCapability("appActivity","com.fortysevendeg.android.swipelistview.sample.activities.SwipeListViewExampleActivity");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  wait = new WebDriverWait(driver, 300);
  wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.RelativeLayout")));
 }


 @Test
 public void swipingHorizontal() throws InterruptedException {
  //Get the size of screen.
  size = driver.manage().window().getSize();
  System.out.println(size);

  //Find swipe x points from screen's with and height.
  //Find x1 point which is at right side of screen.
  int x1 = (int) (size.width * 0.20);
  //Find x2 point which is at left side of screen.
  int x2 = (int) (size.width * 0.80);

  //Create object of TouchAction class.
  TouchAction action = new TouchAction((MobileDriver)driver);

  //Find element to swipe from right to left.
  WebElement ele1 =  (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/front").get(3);  
  //Create swipe action chain and perform horizontal(right to left) swipe.
  //Here swipe to point x1 Is at left side of screen. So It will swipe element from right to left.
  action.longPress(ele1).moveTo(x1,580).release().perform();

  //Find element to swipe from left to right.
  WebElement ele2 =  (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/back").get(3);
  //Create swipe action chain and perform horizontal(left to right) swipe.
  //Here swipe to point x2 Is at right side of screen. So It will swipe element from left to right.
  action.longPress(ele2).moveTo(x2,580).release().perform();
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}
票数 2
EN

Stack Overflow用户

发布于 2019-02-11 11:29:48

您可以创建您自己的自定义滑动功能如下;

代码语言:javascript
运行
复制
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

public static void swipe(int startX, int endX, int startY, int endY) {
        try {
            new TouchAction(driver).press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(ofSeconds(1)))
                    .moveTo(PointOption.point(endX, endY)).release().perform();
        } catch (Exception e) {
            System.out.println("unable to swipe");
        }
    }

where (startX,startY)表示您的起点

代码语言:javascript
运行
复制
Dimension size = driver.manage().window().getSize();
int startX = (int) ((size.getWidth()) * 0.80);
int startY = (int) ((size.getWidth()) * 0.80);
int endX = startX; //for your case this may be same 
int endY = (int) ((size.getWidth()) * 0.20);
//now you can call swipe function
swipe(int startX, int endX, int startY, int endY);

查看此链接以了解不同的appium滚动策略

票数 2
EN

Stack Overflow用户

发布于 2019-02-14 06:34:48

您可以使用以下代码向左滑动:

代码语言:javascript
运行
复制
public static void swipeLeft(AndroidDriver<?> driver)
  {
    Dimension size = driver.manage().window().getSize();
    int startx = (int)(width * 0.9D);
    int endx = (int)(width * 0.1D);
    int starty = height / 2;

    TouchAction<?> action = new TouchAction(driver);
    action.press(PointOption.point(startx, starty)).moveTo(PointOption.point(endx, starty)).release().perform();
  }

SwipeRight:

代码语言:javascript
运行
复制
 public static void swipeRight(AndroidDriver<?> driver)
  {
    Dimension size = driver.manage().window().getSize();
    int endx = (int)(width * 0.9D);
    int startx = (int)(width * 0.1D);
    int starty = height / 2;

    TouchAction<?> action = new TouchAction(driver);
    action.press(PointOption.point(startx, starty)).moveTo(PointOption.point(endx, starty)).release().perform();
  }

使用的类的引用:

TouchAction

维度

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54573934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档