Press button using appium

Example - Pressing button in Android emulator using Appium


package org.softpost.android.simulator;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */
public class PressKeyTest {
    WebDriver driver;
    @Test
    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");

        caps.setCapability("deviceName", "Pixel 9");
        caps.setCapability("appPackage", "com.android.calculator2");
        caps.setCapability("appActivity","com.android.calculator2.Calculator");
        caps.setCapability("automationName","uiautomator2");

try {
    driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


    driver.findElement(By.xpath("//android.widget.Button[@resource-id='com.android.calculator2:id/digit_2']")).click();

    driver.findElement(By.xpath("//*[@text='+']")).click();

    ((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.HOME));


}catch (Exception ex) {
    ex.printStackTrace();


}finally {

    driver.quit();
}
    }

}

Example - Pressing button in iPhone Simulator using Appium


package org.softpost.ios.simulator;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */

public class PressKeyTest {
    AppiumDriver driver;
    @Test
    public void testKey() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("platformVersion", "11.0");
        caps.setCapability("deviceName", "iPhone 8");
        caps.setCapability("AutomationName" , "XCUITest");
        caps.setCapability("headless" , true);
        caps.setCapability("avd" , "44059DAD-FFCD-47EF-9B9A-0713AC9B6422");



        caps.setCapability("wdaLocalPort", 8100);
       // caps.setCapability("app", "/Users/admin/iosapps/softpost.app");


        caps.setCapability("bundleId", "com.apple.MobileAddressBook");

        try {
            driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

            WebElement e = driver.findElement(By.xpath("//XCUIElementTypeButton[@name="Add"]"));

            driver.executeScript("mobile: pressButton", ImmutableMap.of("name", "home"));


            Thread.sleep(2000);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            driver.quit();
        }

    }
}

Complex problems, Simple Solutions