java-client-6.1.0
package appium;
import java.net.MalformedURLException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import io.appium.java_client.TouchAction;
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.TapOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;
public class TouchActionSample extends Base {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
AndroidDriver<AndroidElement> driver = Base.capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Simple click
TouchAction t1 = new TouchAction(driver);
WebElement e1 = driver.findElementByAndroidUIAutomator("text(\"Views\")");
TapOptions o1 = new TapOptions().withElement(new ElementOption().withElement(e1));
t1.tap(o1).perform();
AndroidTouchAction t2 = new AndroidTouchAction(driver);
AndroidElement e2 = driver.findElementByAndroidUIAutomator("text(\"Drag and Drop\")");
TapOptions o2 = new TapOptions().withElement(new ElementOption().withElement(e2));
t2.tap(o2).perform();
// Drag and Drop
AndroidTouchAction t3 = new AndroidTouchAction(driver);
AndroidElement e3 = driver.findElementById("io.appium.android.apis:id/drag_dot_1");
PointOption o3 = new PointOption().withCoordinates(e3.getCenter().getX(), e3.getCenter().getY());
AndroidElement e4 = driver.findElementById("io.appium.android.apis:id/drag_dot_2");
PointOption o4 = new PointOption().withCoordinates(e4.getCenter().getX(), e4.getCenter().getY());
WaitOptions w1 = new WaitOptions().withDuration(Duration.ofSeconds(2));
t3.press(o3).waitAction(w1).moveTo(o4).release().perform();
}
}
package appium;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
public class Base {
public static AndroidDriver<AndroidElement> capabilities() throws MalformedURLException
{
AndroidDriver<AndroidElement> driver;
File appDir = new File("src");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_5X_API_27");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
return driver;
}
}
package appium;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
public class SelectorSample extends Base {
public static void main(String[] args) throws MalformedURLException {
AndroidDriver<AndroidElement> driver = Base.capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// XPath syntax : tagName[@attribute='value']
driver.findElementByXPath("//android.widget.TextView[@text='Preference']").click();
driver.findElementByXPath("(//android.widget.TextView[@resource-id='android:id/text1'])[3]").click();
driver.findElementById("android:id/checkbox").click();
driver.findElementsByClassName("android.widget.RelativeLayout").get(1).click();
driver.navigate().back();
driver.navigate().back();
// AndroidUIAutomator syntax : attribute("value")
driver.findElementByAndroidUIAutomator("text(\"WiFi settings\")").click();
// AndroidUIAutomator syntax : className(name)
// All method : https://stuff.mit.edu/afs/sipb/project/android/docs/tools/help/uiautomator/UiSelector.html
driver.findElementByAndroidUIAutomator("className(\"android.widget.EditText\")").sendKeys("Hello");
driver.findElementsByAndroidUIAutomator("className(\"android.widget.Button\")").get(1).click();
}
}