buttons

 

Appium Gestures-Perform ‘Tap’ Gesture on a element & mobile screen By Using/Running “adb Commands”

Get a step-by-step walkthrough in the video below!

Inthis Article, We will discuss “How to Perform ‘Tap’ Gesture on a element/ mobile screen By Using/Running “adb Commands” — Using Appium+java.

What you’ll learn:
1. Steps to follow from Client-Side Setting
2. Introduction to ‘Run arbitrary ADB commands via Appium’
3. Brief discussion on adb command — “adb shell input tap <x> <y>”
4. Appium Server-side Setting

Client Side Setting & Introduction to ‘Run arbitrary ADB commands via Appium’:

Run arbitrary ADB commands via Appium

.executeScript("mobile: shell", <arg>);

Basic Syntax:

driver.executeScript("mobile: shell", ImmutableMap.of(
"command", "input",
"args", Arrays.asList("tap", "135", "469")));

Example:

//Calculate the x & y co-ordinates values of Checkbox 1 element
System.out.println("Calculate the x & y co-oradinates values");
WebElement element=driver.findElement(AppiumBy.accessibilityId("Checkbox 1"));
int x=element.getRect().getX();
int y=element.getRect().getY();
int width=element.getRect().getWidth();
int height=element.getRect().getHeight();
int x1= (int)x+width/2;
int y1= (int)y+height/2;

//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1
System.out.println("//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1");
driver.executeScript("mobile: shell", ImmutableMap.of(
"command","input",
"args",Arrays.asList("tap",x1,y1)));

Brief discussion on adb command — “adb shell input tap <x> <y>”

The adb shell input tap command is used to simulate a touchscreen tap on an Android device from your computer via ADB (Android Debug Bridge). It is part of the adb shell input command suite, which allows you to simulate user input like taps, swipes, key events, etc.

Basic Syntax:

adb shell input tap <x> <y>

Explanation:

adb: Android Debug Bridge tool

shell: Executes a shell command on the device

input: Command to simulate user input

tap: Simulates a finger tap on the screen

<x>: Horizontal screen coordinate (in pixels)

<y>: Vertical screen coordinate (in pixels)

Example:

//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1 once again
System.out.println("//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1 once again");
driver.executeScript("mobile: shell", ImmutableMap.of(
"command","input",
"args",Arrays.asList("tap",x1,y1)));

Appium Server Setting: -

We Need to Start Appium Server with the “ — relaxed-security” flag

appium - relaxed-security

Or

appium - allow-insecure=adb_shell

With Appium running in this mode, we have access to a new “mobile:” command called “mobile: shell”.

The Appium “mobile:” commands are special commands that can be accessed using executeScript

Example:

“Complete Code: Optimized and Ready to Use”:

package com.appiumguide.gesturesUsingADBcommands;

import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;

import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.util.Arrays;

/**
* Script Details - Perform ‘Tap’ Gesture on a element & mobile screen By Using/Running “adb Commands”
*
* appium-java-client version: Latest
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/


public class TapDemoUsingADB {

private AndroidDriver driver;

@BeforeTest
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8");


driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(100));

}


@Test
public void test() throws InterruptedException, IOException {

//Open the Apidemos app
System.out.println("Open the Apidemos app");
driver.activateApp("io.appium.android.apis");
Thread.sleep(2000);
driver.findElement(AppiumBy.accessibilityId("Views")).click();
Thread.sleep(2000);
driver.findElement(AppiumBy.accessibilityId("Controls")).click();
Thread.sleep(2000);
driver.findElement(AppiumBy.accessibilityId("1. Light Theme")).click();
Thread.sleep(2000);

//Calculate the x & y co-ordinates values of Checkbox 1 element
System.out.println("Calculate the x & y co-oradinates values");
WebElement element=driver.findElement(AppiumBy.accessibilityId("Checkbox 1"));
int x=element.getRect().getX();
int y=element.getRect().getY();
int width=element.getRect().getWidth();
int height=element.getRect().getHeight();
int x1= (int)x+width/2;
int y1= (int)y+height/2;

//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1
System.out.println("//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1");
driver.executeScript("mobile: shell", ImmutableMap.of(
"command","input",
"args",Arrays.asList("tap",x1,y1)));

Thread.sleep(5000);

//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1 once again
System.out.println("//Perform Tap Gestures Using ADB Commands to Tap on the Checkbox-1 once again");
driver.executeScript("mobile: shell", ImmutableMap.of(
"command","input",
"args",Arrays.asList("tap",x1,y1)));


}


@AfterTest
public void teardown() {

if(driver!=null) {
driver.quit();
System.out.println("Test Completed");
}

}

}

GitHub Link:


No comments:

Post a Comment