Automate gestures in Appium



How to automate gestures in Appium

Automate gestures in Appium

Gestures play an important role in how your app is being used. With lot of unique gesture supported on mobile devices, automation has its own challenge. Some of the common gestures are:


swipe

single tap
double tap
flick (left or right)
scroll down

scroll up
long press

Appium handles these gestures using TouchActions api they have created. It’s more like Actions class in Selenium. Apart from that they have also enabled JSON wire protocol server extensions.

TouchAction class is available from namespace OpenQA.Selenium.Appium.MultiTouch

We can pass in coordinates as parameters and specify the action we want. Sample code would look like as:

JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeObject = new HashMap<String, Double>();
swipeObject.put(“startX”, 0.01); //1% from X
swipeObject.put(“startY”, 0.5); //50% from Y
swipeObject.put(“endX”, 0.9);
swipeObject.put(“endY”, 0.6);
swipeObject.put(“duration”, 3.0); //Tap 3 secs
js.executeScript(“mobile: swipe”, swipeObject);

When we enter the coordinates in decimal, it actually specifies the percentage. So in above example it means, 1% from x and 50% from y coordinates. Duration basically specifies how long it will tap and is in seconds.

Some of the mobile methods to be used are:

mobile: tap
mobile: flick
mobile: swipe
mobile: scroll
mobile: shake

Prefix “mobile:” allows us to route these requests to the appropriate endpoint.

You might also want to explore TouchActions class provided by Selenium. It implements actions for touch devices and basically built upon the Actions class.

We have added some test in the git project to show how to use the gestures in automation.

The project would help you learn how to automate android app using Appium.

Appium let’s you do a scroll to the element text but sometimes it might not work depending on how app is and CSS structure are. You can write your own parallel code to perform scroll and here is the code snippet for the same.

JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> scrollObject = new HashMap<String, String>();
    scrollObject.put("direction", "down");
    js.executeScript("mobile: scroll", scrollObject);

here are the sample code snippet’s for different gestures..

//Click on x,y coordinates..

public void ClickonXY(IWebDriver driver)
{

int heightOfScreen =driver.Manage().Window.Size.Height;
int widthOfScreen =driver.Manage().Window.Size.Width;
int middleHeightOfScreen = heightOfScreen / 2;
// To get 50% of width
int x = (int)(widthOfScreen * 0.5);
// To get 50% of height
int y = (int)(heightOfScreen * 0.5);

TouchAction touchAction = new TouchAction((OpenQA.Selenium.Appium.Interfaces.IPerformsTouchActions)driver);
touchAction.Press(x, y)
//.waitAction(waitOptions(ofSeconds(seconds)))
.Release()
.Perform();
}

Horizantal Scroll


 public void HorizantalSwipe(AppiumDriver<IWebElement> driver)
{
ITouchAction swipe = new TouchAction(driver);
swipe.Press(972, 500)
.Wait(800)
.MoveTo(108, 500)
.Release()
.Perform();

}

Vertical scroll


 public void ScrollVertical(AppiumDriver<IWebElement> Driver)
{
var size = Driver.Manage().Window.Size;
int startX = size.Width / 2;
int startY = size.Height / 2;
int endX = 0;
int endY = (int)(startY * -1 * 1.30);
TouchAction action = new TouchAction(Driver);
action.Press(startX, startY)
.MoveTo(endX, endY)
.Release()
.Perform();
}

//Scroll by passing pixels
public void ScrollVertical(AppiumDriver<IWebElement> Driver,int startY,int endY)
{
var size = Driver.Manage().Window.Size;
int startX = size.Width / 2;
int endX = 0;
TouchAction action = new TouchAction(Driver);
action.Press(startX, startY)
.MoveTo(endX, endY)
.Release()
.Perform();
}

SwipeElement:


public static void SwipeElement(IWebElement element, SwipeDirections eSwipeDirection)
{
int startx, starty, endx, endy;

TouchAction tAction = new TouchAction(AppiumDriver.Instance);

System.Drawing.Size size = element.Size;

if (eSwipeDirection == SwipeDirections.DOWN)
{
// get the proper location of where to start to swipe from
startx = size.Width / 2;
starty = 100;

endx = startx;
endy = (int)(size.Height * 0.8) - 100;
}
else if (eSwipeDirection == SwipeDirections.UP)
{
// get the proper location of where to start to swipe from
startx = size.Width / 2;
starty = 100;

endx = startx;
endy = (int)(size.Height * 0.8) - 100;
}
else if (eSwipeDirection == SwipeDirections.LEFT)
{
// get the proper location of where to start to swipe from
startx = element.Location.X + (size.Width - 10);
starty = element.Location.Y + (int)(size.Height / 2);

endx = element.Location.X + 10;
endy = starty;
}
else // must be right then
{
// get the proper location of where to start to swipe from
startx = 200;
starty = size.Height / 2;

endx = (int)(size.Width * 0.8) - 50;
endy = starty;
}

// perform the swipe
AppiumDriver.Instance.Swipe(startx, starty, endx, endy, 1000);
}

Swiping Horizontal:

            Swipe Right to Left in Mobile Automation

Common Functions Utilized in Mobile Automation

tap(), swipe(), swipe Left to Right, swipe Right to Left, swipe Horizontal, swipe Vertical, pinch(), zoom(), getNamedTextField(), isAppInstalled(), installApp(), removeApp(), launchApp(), closeApp() are some actions used in mobile automation

Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18