public static void main(String[] args) throws MalformedURLException {
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability("platformName", "android");
    cap.setCapability("app", "e:/example.apk");
    cap.setCapability("unicodeKeyboard", "true");
    cap.setCapability("resetKeyboard", "true");
    //        cap.setCapability("deviceName", "huawei-plk_ul00-W8R0215813002079");
    cap.setCapability("deviceName", "192.168.120.91:5555");

    AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.findElementByAndroidUIAutomator("new UiSelector().text(\"排行榜\")").click();

    //        根据父元素获取同级元素
    driver
        .findElementByAndroidUIAutomator(
            "new UiSelector().text(\"排行榜\")." + "fromParent(new UiSelector().text(\"应用\"))")
        .click();

    //        获取子元素
    driver
        .findElementByAndroidUIAutomator(
            "new UiSelector().resourceId(\"com.wandoujia.phoenix2:id/tab_host\")"
                + ".childSelector(new UiSelector().className(\"android.widget.TextView\").text(\"游戏\"))")
        .click();

    driver
        .findElementByAndroidUIAutomator(
            "new UiSelector().resourceId(\"com.wandoujia.phoenix2:id/search_image\")")
        .click();

    driver.navigate().back();

    driver
        .findElementByAndroidUIAutomator(
            "new UiSelector().resourceId(\"com.wandoujia.phoenix2:id/search_box_close\")")
        .click();
    driver
        .findElementByAndroidUIAutomator(
            "new UiSelector().resourceId(\"com.wandoujia.phoenix2:id/search_box_edit\")")
        .sendKeys("腾讯新闻");
    driver
        .findElementByAndroidUIAutomator(
            "new UiSelector().resourceId(\"com.wandoujia.phoenix2:id/search_button\")")
        .click();
    WebElement ele =
        (WebElement)
            driver
                .findElementsByAndroidUIAutomator(
                    "new UiSelector()" + ".className(\"android.widget.TextView\").text(\"安装\")")
                .get(0);
    ele.click();
    driver.openNotifications();
    driver.quit();
  }
  @Test
  public void launchingWatsapp() throws IOException, InterruptedException {

    stopAppiumServer();
    startAppiumServer();

    capabilities = new DesiredCapabilities();
    capabilities.setCapability("automationName", "Appium");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "4.4.4");
    capabilities.setCapability("deviceName", "Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920");
    capabilities.setCapability("app", appPath);
    capabilities.setCapability("appPackage", "com.whatsapp");
    capabilities.setCapability("appActivity", "com.whatsapp.Main");

    System.out.println("Trying to start the driver");
    driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    Thread.sleep(5000);
    if (driver.findElement(By.id("com.whatsapp:id/parentPanel")).isDisplayed()) {
      driver.findElement(By.id("android:id/button2")).click();
      System.out.println("Successfully clicked on the \"OK\" button");
    }
    // driver.findElement(By.partialLinkText("Agree and continue")).click();
    driver
        .findElementByAndroidUIAutomator("new UiSelector().textMatches(\"Agree and continue\")")
        .click();

    if (!(driver
        .findElement(By.id("com.whatsapp:id/registration_country"))
        .getText()
        .equalsIgnoreCase("United States"))) {
      driver.findElement(By.id("com.whatsapp:id/registration_country")).sendKeys("United States");
    }

    driver.findElement(By.id("com.whatsapp:id/registration_phone")).sendKeys("1234567890");
    driver.findElement(By.id("com.whatsapp:id/registration_submit")).click();

    Thread.sleep(30000);
    try {
      if (driver.findElement(By.id("android.widget.LinearLayout")).isDisplayed()) {
        driver.findElement(By.id("android:id/button1")).click();
      }
    } catch (Exception e) {

    }
  }