java Appium - 我们什么时候使用触摸动作?我可以用它输入文本的实际示例吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42288409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:31:09  来源:igfitidea点击:

Appium - When do we use touch action? Could I have actual example for inputing text with it?

javaautomated-testsappium

提问by Tel lui

I would like to ask when do we use touch action in Appium. I also want to ask: could we use touch action to tap/press the android.widget.EditText element and sendKeys to it. Could I have an workable example to test it?

我想问一下我们什么时候在Appium中使用触摸动作。我还想问:我们可以使用触摸动作来点击/按下 android.widget.EditText 元素并将其发送给它。我可以有一个可行的例子来测试它吗?

回答by Vinod

We use touch action whenever we want to click/tapon the particular element on the device.

每当我们想点击/点击设备上的特定元素时,我们都会使用触摸动作。

For Clicking

用于点击

You can use touch action for EditText element as bewlow :

您可以对 EditText 元素使用触摸操作,如下所示:

driver.findElement(By.xpath("your element xpath/id")).click();

For Typing

打字用

Also, You can use sendKeys for EditText Element as below : This internal clicks on the element, clears the text and types the string that you are sending.

此外,您可以将 sendKeys 用于 EditText 元素,如下所示:此内部单击元素,清除文本并键入您要发送的字符串。

driver.findElement(By.xpath("your element xpath/id")).sendKeys("textToBeTyped");

OR

或者

You can click and send keys separately without clearing the existing text inside the text element as below :

您可以单独单击和发送密钥,而无需清除文本元素内的现有文本,如下所示:

driver.findElement(By.xpath("your element xpath/id")).click();
driver.getKeyboard().sendKeys(textToBeTyped);

回答by akhilesh gulati

1.public void tap(int fingers, int x, int y, int duration) {
        appiumDriver.tap(fingers, x, y, duration);
    }

2. public   void swipe(int startx, int starty, int endx,int endy,int duration)
    {
        TouchAction touchAction = new TouchAction(appiumDriver);
        System.out.println(startx+" "+starty);
        System.out.println("Entering swipe");

            System.out.println("Swipe from "+startx +" " +starty +"to" +endx +" " +endy );
            touchAction.press(startx, starty).waitAction(duration).moveTo(endx,endy).release().perform();
    }

3. public void longClick(String element, int index, int clickCount, int X, int Y) {
    WebElement webElement = appiumDriver.findElement(By.xpath(element));

        TouchAction Action = new TouchAction(appiumDriver);
        Action.longPress(webElement).release().perform();

    }

4. public void drag( String element, int index, int xOffset, int yOffset) {
        WebElement webElement = appiumDriver.findElement(By.xpath(element));
        TouchAction drag=new TouchAction(appiumDriver);

        int startX=webElement.getLocation().getX();
        int startY=webElement.getLocation().getY();

        System.out.println("startX: "+startX+" startY: "+startY);
        drag.press(startX,startY).moveTo(xOffset,yOffset).release().perform();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

回答by avivamg

In all the appium client libraries, touch objects are created and are given a chain of events. First you must initialize TouchAction ovbject with your webdriver and then chain your steps in order to perform action. You can demonstrate variety of gestures such as :

在所有 appium 客户端库中,都创建了触摸对象并为其提供了一系列事件。首先,您必须使用您的 webdriver 初始化 TouchAction ovbject,然后链接您的步骤以执行操作。您可以演示各种手势,例如:

  • Tap
  • press(Long,Short)
  • Swipe (from coordinate a to b)
  • perform multi touch actions
  • 轻敲
  • 按(长,短)
  • 滑动(从坐标 a 到 b)
  • 执行多点触控操作

You can do it with given parameters such as duration per action, wait settings, set releasing and more.

您可以使用给定的参数进行操作,例如每次操作的持续时间、等待设置、设置释放等等。

examples:

例子:

in order to press an element with coordinates :

为了按下带有坐标的元素:

new TouchAction(driverObject).tap(PointOption.point(x,y)).perform();

or:

或者:

new TouchAction(driverObject).press(PointOption.point(x,y)).perform();

in order to swipe element from pointA to pointB with Z duration of millis

为了将元素从 A 点刷到 B 点,Z 持续时间为毫秒

//wait parameters for duration purposes
WaitOptions waitOptions = new WaitOptions();
waitOptions.withDuration(Duration.ofMillis(millis));

//The action
new TouchAction(driverObject).longPress.press(fromPoint)
                .waitAction(waitOptions).moveTo(toPoint).release().perform();