Drag And Drop Is 1 of the mutual activity of whatever android app. You volition come across many android mobile apps where you lot tin drag too drib chemical factor from 1 house to other place. If you lot are automating android mobile app where you lot cause got to perform drag too drib too hence you lot take away to use TouchAction class. TouchAction course of teaching provides us facility to automate mobile gestures of android app using appium. Let's cause got rattling unproblematic representative to perform drag too drib functioning on android application using appium.
PREREQUISITES : Previous eighteen steps (Listed In PART 1 too PART 2) of appium tutorials should endure completed.
Download Drag-Sort Demos app
We volition use Drag-Sort Demos app to perform drag too drib functioning inward android device. You tin download it from THIS PAGE.
Create New Folder Under Project Folder To Store APK File
You take away to practise novel folder "Apps" nether your projection folder too pose downloaded Drag-Sort Demos's .apk file within it.
To practise novel folder nether projection inward eclipse,
- Right click on project(MavenProject1 for me) folder -> New -> Folder.
- Give folder cite = Apps too click on Finish button. It volition add together Apps folder nether projection folder.
- Copy-paste downloaded Drag-Sort Demos apk file within it equally shown inward bellow image.
Aim Of This Appium Android Test
Our appium automation examine script will,
- Install Drag Sort Demos app inward android mobile device automatically.
- Then It volition launch Drag Sort Demos app inward your android mobile device.
- It volition tap on "Basic usage playground" text.
- Then It volition locate too drag tertiary chemical factor too drib it to sixth seat equally shown inward bellow images.
Before drag too drop
After drag too drop
This is our aim to accomplish from bellow given examine script.
Create And Run Test
Now you lot already knows what nosotros wants to practise In this test. Let's practise too piece of job examine inward eclipse.
PREREQUISITES :
- Android mobile device should endure connected amongst PC amongst USB debugging trend enabled. View THIS POST.
- Drag-Sort Demos app should endure at that topographic point inward Apps folder equally described above.
- There should endure plenty gratis infinite inward your mobile device to install Drag-Sort Demos app.
- appium node server should endure launched too running mode. View THIS POST.
- TestNG should endure installed inward eclipse. View THIS POST.
Now practise novel course of teaching file DragAndDropAction.java nether package(for me, bundle cite is Android) of your projection too re-create glue bellow given examine script inward it.
Note : Please supersede deviceName too platformVersion equally per you lot actual inward bellow given test.
Note : Please supersede deviceName too platformVersion equally per you lot actual inward bellow given test.
DragAndDropAction.java
package Android; import io.appium.java_client.MobileDriver; import io.appium.java_client.TouchAction; import io.appium.java_client.android.AndroidDriver; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; populace course of teaching DragAndDropAction { //Object reference of AndroidDriver. AndroidDriver driver; @BeforeTest populace void setUp() throws MalformedURLException { //Set Drag-Sort Demos app folder path. This contestation volition refer project's folder path. File classpathRoot = novel File(System.getProperty("user.dir")); //Set folder cite "Apps" where .apk file is stored. File appDir = novel File(classpathRoot, "/Apps"); //Set Drag-Sort Demos .apk file name. File app = novel File(appDir, "com.mobeta.android.demodslv-0.5.0-3_APKdot.com.apk"); // Created object of DesiredCapabilities class. DesiredCapabilities capabilities = novel DesiredCapabilities(); // Set android deviceName desired capability. Set your device name. capabilities.setCapability("deviceName", "ZX1B32FFXF"); // Set BROWSER_NAME desired capability. It's Android inward our representative here. capabilities.setCapability("browserName", "Android"); // Set android VERSION desired capability. Set your mobile device's OS version. capabilities.setCapability("platformVersion", "4.4.2"); // Set android platformName desired capability. It's Android inward our representative here. capabilities.setCapability("platformName", "Android"); //Set .apk file's path capabilities. capabilities.setCapability("app", app.getAbsolutePath()); // Set app Package desired capability of Drag-Sort Demos app. capabilities.setCapability("appPackage", "com.mobeta.android.demodslv"); // Set app Activity desired capability of Drag-Sort Demos app. capabilities.setCapability("appActivity", "com.mobeta.android.demodslv.Launcher"); // Created object of AndroidDriver too laid capabilities. // Set appium server address too port pose out inward URL string. // It volition launch Drag-Sort Demos app inward emulator. driver = novel AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); } @Test populace void dragDrop() { //Tap on Basic usage Playground. driver.findElementByName("Basic usage playground").click(); //Locate tertiary element(Chick Corea) from listing to drag. WebElement ele1 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(2); //Locate sixth chemical factor to drib dragged element. WebElement ele2 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(5); //Perform drag too drib functioning using TouchAction class. //Created object of TouchAction class. TouchAction activity = novel TouchAction((MobileDriver) driver); System.out.println("It Is dragging element."); //It volition concord tap on tertiary chemical factor too movement to sixth seat too and hence unloose tap. action.longPress(ele1).moveTo(ele2).release().perform(); System.out.println("Element has been droped at finish successfully."); } @AfterTest populace void End() { // Quit driver.quit(); } }
Comments are already at that topographic point amongst each judgement of examine script. Let me depict you lot few Important course of teaching too methods which are used inward higher upward examine script.
- TouchAction : This is course of teaching of Webdriver 3 which supply dissimilar methods(longPress, moveTo, perform, press, tap, etc..) to automate mobile gestures similar drag too drop, swipe etc. It volition aid us to generate activity chain of dissimilar actions.
- longPress : longPress is method to concord tap for long fourth dimension on given element.
- moveTo : moveTo is method to movement action.
- release : release is used to unloose from longPress tap action.
- perform : perform volition execute sum activity chain of drag too drop.
Also If you lot notice inward higher upward examine script, We cause got used AndroidDriver at house of RemoteWebDriver equally nosotros are automating android app. And likewise used findElementByName("") and findElementsById("") methods at house of findElement(By.Name("")) and findElements(By.Id("")) methods.
Run higher upward examine using testng too observe Drag And Drop inward your mobile device.
- It volition launch Drag-Sort Demos app.
- Tap on "Basic usage playground" text and
- Perform draga too drib functioning equally shown inward higher upward Images.
This agency nosotros tin automate mobile gestures inward appium android automation test. Same affair you lot tin practise amongst whatever app.