The second demo with TouchAction is a drag and drop, and here I prepared the test case as a drag and drop adding the TouchActions, Appium driver, and all the desired capabilities.
Here I have also prepared a test to start writing our code directly.
package Android;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.touch.offset.ElementOption;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class drag_drop_test {
public AppiumDriver driver;
public AndroidTouchAction actions;
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "9.0");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", System.getProperty("user.dir") + "/apps/ApiDemos.apk");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}
@Test
public void drag_drop() {
}
@AfterTest
public void tearDown() {
if (null != driver) {
driver.quit();
}
}
}
For the drag and drop here in our "API Demos" application, I can open it.
Under "Views", I have "Drag and Drop", and here I have three dots.
If you longPress on this one and try to drag this circle, you can drop it here, and then you will see this notification that you dragged from this point to this point.
We need to do this with Appium and our client script using Java.
I will start by adding our elements. \
First I need to click on "Views", and if the "Drag and Drop" is not displayed, I will use scroll down like in the previous example, but it's already displayed.
I will click the "Drag and Drop" and here I need to find these two elements, this one to drag from, and then to drag to, so we have a start point and we have the end point also here.
With the "Drag and Drop", I will start by adding an AndroidElement views
, and this will equal driver.findElementsByAccessibilityId()
and I will pass "Views".
Then I will just cast this from a WebElement
to an AndroidElement
.
Then I will set actions
to new AndroidTouchActions()
and pass driver
.
After that, I will use actions
to tap
on the ElementOption.element()
and the element is views
, then I will call .perform()
.
This is for the click, and also you can use the normal click with the driver.
So, I'm clicking on "Views", and then I need to find another element to be able to use it again.
So here I will add another AndroidElement
for the "Drag and Drop" views, so I will call this drag_drop
, and set this equal to driver.findElementsByAccessibilityId()
with the accessibility ID is "Drag and Drop" - just make sure that the text is correct.
Again, actions.tap()
on ElementOption.element()
and the element is drag_drop
and then .perform()
.
We just cast the element to an AndroidElement
.
@Test
public void drag_drop() {
AndroidElement views = (AndroidElement) driver.findElementByAccessibilityId("Views");
actions = new AndroidTouchAction(driver);
actions.tap(ElementOption.element(views)).perform();
AndroidElement drag_drop =
(AndroidElement) driver.findElementByAccessibilityId("Drag and Drop");
actions.tap(ElementOption.element(drag_drop)).perform();
}
Now I click on the "Views" and then "Drag and Drop" in the app.
Here, I need to find additional two elements in this one.
Let's add another AndroidElement
, which is the drag
point, so we will use driver.findElementsByAccessibilityId()
, but in this case, we need to make sure there is an accessibility ID.
So let's open an inspection session quickly and check our application.
Here I have already set desired capabilities and in Android, I have the APIDemos application set up so I can directly start and check the application.
The application is displayed here, so from here, I can click on "Tap" and then I can "Drag and Drop" then click "Tap", and here I have the elements, so I will start inspecting these elements.
Here I have three elements.
For this one, it's already including an "id" so there is no accessibility ID for this one, so I will copy this ID and start adding it in our test.
So in this case, it will be findElement(By.id())
and the id will be drag_dot_1
, and also I need to cast it to an AndroidElement
.
Also, for another element, I will add an AndroidElement
drop
for the second one and use driver.findElement(By.id())
and we will also check this one and get the id drag_dot_2
.
Then we will cast this to an AndroidElement
.
@Test
public void drag_drop() {
AndroidElement views = (AndroidElement) driver.findElementByAccessibilityId("Views");
actions = new AndroidTouchAction(driver);
actions.tap(ElementOption.element(views)).perform();
AndroidElement drag_drop =
(AndroidElement) driver.findElementByAccessibilityId("Drag and Drop");
actions.tap(ElementOption.element(drag_drop)).perform();
AndroidElement drag = (AndroidElement) driver.findElement(By.id("drag_dot_1"));
AndroidElement drop = (AndroidElement) driver.findElement(By.id("drag_dot_2"));
}
So then we will use an action to drag the value from the first point and leave it or release it in the second point.
From actions
, I will click on longPress
, because I mentioned when I was describing the examples that I long press on the element and then drag it the element to another place.
So we will use another TouchAction
here, which is the longPress
and this takes an ElementOption.element()
, and I will start from drag
- this is the first point - then .waitAction()
, then .moveTo()
- I need to move to another element option and pass the element - drop
in this case.
Then .release()
to simulate that my finger is leaving this element, and then .perform()
and a semicolon.
This is the simple code for drag and drop from different elements.
package Android;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.touch.offset.ElementOption;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class drag_drop_test {
public AppiumDriver driver;
public AndroidTouchAction actions;
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "9.0");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", System.getProperty("user.dir") + "/apps/ApiDemos.apk");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}
@Test
public void drag_drop() {
AndroidElement views = (AndroidElement) driver.findElementByAccessibilityId("Views");
actions = new AndroidTouchAction(driver);
actions.tap(ElementOption.element(views)).perform();
AndroidElement drag_drop =
(AndroidElement) driver.findElementByAccessibilityId("Drag and Drop");
actions.tap(ElementOption.element(drag_drop)).perform();
AndroidElement drag = (AndroidElement) driver.findElement(By.id("drag_dot_1"));
AndroidElement drop = (AndroidElement) driver.findElement(By.id("drag_dot_2"));
actions.longPress(ElementOption.element(drag))
.waitAction().moveTo(ElementOption.element(drop))
.release()
.perform();
}
@AfterTest
public void tearDown() {
if (null != driver) {
driver.quit();
}
}
}
So you can drag and drop from X and Y, you can use different things using a longPress
and moveTo
.
Let's run our test and check what happened.
Let's close the inspection session and start checking our test while running and connecting it to the Appium server and to check if there is any problem in our test script.
The application is running and here we have one problem.
The problem here is in casting - we have a problem with ArrayList - it can't be cast to an AndroidElement
.
So what is that ArrayList?
We didn't use any ArrayLists, so let's visit our code again, and check if we have any problems.
Here it says the problem is on line 39:
actions.tap(ElementOption.element(drag_drop)).perform();
The problem I found is that we are using findElements
for views, which will return an array list.
So we are searching for different elements, but in my case, I want to find one element, so I will change findElementsByAccessibilityId
to findElementByAccessibilityId
.
So I just need to cast one element to be an AndroidElement
.
I fixed my script, and then I will run my code again.
Let's start our server and our test and check if the problem is fixed.
The application is open, we click on "Views", "Drag and Drop", and then longPress with the first dot, and then drag the value, and our test is passed successfully.
So in this demo, we used the drag and drop using TouchAction
and Appium and Android.
Quiz
The quiz for this chapter can be found in Chapter 5.10