package cn.shin.autotest.testcase;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.net.URL;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;

public class AndroidTest {
	private AndroidDriver driver;

	@Before
	public void setUp() throws Exception {
		File classpathRoot = new File(System.getProperty("user.dir"));
		File appDir = new File(classpathRoot, "src/test/resources/apk");
		File app = new File(appDir, "app-debug.apk");

		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
		capabilities.setCapability("platformName", "Android");
		capabilities.setCapability("deviceName", "ShinAndroid");

		capabilities.setCapability("platformVersion", "4.1.2");
		capabilities.setCapability("app", app.getAbsolutePath());

		capabilities.setCapability("appPackage", "cn.shin.androidstudiotest");
		capabilities.setCapability("appActivity", ".MainActivity");

		driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	}

	@Test
	public void findHelloWorld() {
		List<WebElement> textList = driver.findElementsByClassName("android.widget.TextView");
		WebElement webElement = textList.get(1);
		assertEquals(webElement.getText(), "Hello World!");
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
}

@@ -112,40 +112,43 @@
 ava.
-net.URL;%0Aimport java.util.List;%0A
+util.List;%0A%0Aimport org.junit.After;
 %0Aimp
@@ -158,32 +158,37 @@
  org.junit.After
+Class
 ;%0Aimport org.jun
@@ -215,20 +215,27 @@
 g.junit.
-Test
+BeforeClass
 ;%0Aimport
@@ -243,26 +243,18 @@
 org.
-openqa.selenium.By
+junit.Test
 ;%0Aim
@@ -324,16 +324,23 @@
 .remote.
+Desired
 Capabili
@@ -344,22 +344,21 @@
 ilit
-yType;
+ies;%0A
 %0Aimport 
 org.
@@ -357,42 +357,148 @@
 ort 
-org.openqa.selenium.remote.Desired
+io.appium.java_client.android.AndroidDriver;%0Aimport io.appium.java_client.android.AndroidElement;%0Aimport io.appium.java_client.remote.Mobile
 Capa
@@ -494,37 +494,38 @@
 .MobileCapabilit
-ies;%0A
+yType;
 %0Aimport io.appiu
@@ -538,37 +538,54 @@
 _client.
-android.AndroidDriver
+service.local.AppiumDriverLocalService
 ;%0A%0Apubli
@@ -632,66 +632,402 @@
 iver
- driver;%0A%0A%09@Before%0A%09public void setUp() throws Exception %7B
+%3CAndroidElement%3E driver;%0A%09private static AppiumDriverLocalService service;%0A%0A%09@BeforeClass%0A%09public static void beforeClass() throws Exception %7B%0A%09%09service = AppiumDriverLocalService.buildDefaultService();%0A%09%09service.start();%0A%09%7D%0A%0A%09@Before%0A%09public void setUp() throws Exception %7B%0A%09%09if (service == null %7C%7C !service.isRunning())%0A%09%09%09throw new RuntimeException(%22An appium server node is not started!%22);%0A
 %0A%09%09F
@@ -1297,16 +1297,22 @@
 ability(
+Mobile
 Capabili
@@ -1370,77 +1370,40 @@
 ity(
-%22platformName%22, %22Android%22);%0A%09%09capabilities.setCapability(%22deviceName%22
+MobileCapabilityType.DEVICE_NAME
 , %22S
@@ -1416,17 +1416,16 @@
 roid%22);%0A
-%0A
 %09%09capabi
@@ -1449,25 +1449,45 @@
 ity(
-%22platformVersion%22
+MobileCapabilityType.PLATFORM_VERSION
 , %224
@@ -1527,13 +1527,32 @@
 ity(
-%22app%22
+MobileCapabilityType.APP
 , ap
@@ -1573,17 +1573,16 @@
 ath());%0A
-%0A
 %09%09capabi
@@ -1606,20 +1606,40 @@
 ity(
-%22appPackage%22
+MobileCapabilityType.APP_PACKAGE
 , %22c
@@ -1699,21 +1699,41 @@
 ity(
-%22appActivity%22
+MobileCapabilityType.APP_ACTIVITY
 , %22.
@@ -1781,47 +1781,40 @@
 iver
-(new URL(%22http://127.0.0.1:4723/wd/hub%22
+%3CAndroidElement%3E(service.getUrl(
 ), c
@@ -1877,19 +1877,23 @@
 %0A%09%09List%3C
-Web
+Android
 Element%3E
@@ -2134,11 +2134,105 @@
 it();%0A%09%7D
+%0A%0A%09@AfterClass%0A%09public static void afterClass() %7B%0A%09%09if (service != null)%0A%09%09%09service.stop();%0A%09%7D
 %0A%7D%0A

Update the code.