问题
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?
回答1:
We use touch action whenever we want to click/tap on the particular element on the device.
For Clicking
You can use touch action for EditText element as bewlow :
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.
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);
回答2:
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();
}
}
回答3:
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 :
- Tap
- press(Long,Short)
- Swipe (from coordinate a to b)
- perform multi touch actions
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
//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();
来源:https://stackoverflow.com/questions/42288409/appium-when-do-we-use-touch-action-could-i-have-actual-example-for-inputing-t