package com.tridentqa.webdriver.appium;

import io.appium.java_client.ios.IOSDriver;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.sahagin.runlib.external.adapter.webdriver.WebDriverAdapter;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class IOSNativeTest {
    private Process process;
    private IOSDriver driver;
    
    @Before
    public void setUp() throws IOException, InterruptedException {
        File classpathRoot = new File(System.getProperty("user.dir"));
        System.out.println("SUDO_COMMAND: " + System.getenv("SUDO_COMMAND"));
        Runtime runtime = Runtime.getRuntime();
        File appiumLog = new File(classpathRoot, "appium.log");
        process = runtime.exec(new String[]{
                "/usr/local/bin/node",
                "/usr/local/lib/node_modules/appium/lib/server/main.js",
                "--log",
                appiumLog.getAbsolutePath()});
        Thread.sleep(10000);
        if (process != null) {
            System.out.println("Appium server started with " + appiumLog.getAbsolutePath());
        }
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "iPhone 5");
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("platformVersion", "8.2");
        File app = new File(classpathRoot, "../../apps/TestApp/TestApp.app");
        capabilities.setCapability("app", app.getAbsolutePath());
        driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        WebDriverAdapter.setAdapter(driver);
    }
    
    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
        if (process != null) {
            process.destroy();
        }
    }
    
    @Test
    public void 計算処理が正しく行われること() {
        driver.findElementByAccessibilityId("IntegerA").sendKeys("123");
        driver.findElementByAccessibilityId("IntegerB").sendKeys("456");
        driver.findElementByAccessibilityId("ComputeSumButton").click();
        assertThat(driver.findElementByAccessibilityId("Answer").getText(), is("579"));
    }
}

@@ -559,37 +559,8 @@
 t %7B%0A
-    private Process process;%0A
     
@@ -585,16 +585,16 @@
 driver;%0A
+
     %0A   
@@ -673,661 +673,8 @@
 n %7B%0A
-        File classpathRoot = new File(System.getProperty(%22user.dir%22));%0A        System.out.println(%22SUDO_COMMAND: %22 + System.getenv(%22SUDO_COMMAND%22));%0A        Runtime runtime = Runtime.getRuntime();%0A        File appiumLog = new File(classpathRoot, %22appium.log%22);%0A        process = runtime.exec(new String%5B%5D%7B%0A                %22/usr/local/bin/node%22,%0A                %22/usr/local/lib/node_modules/appium/lib/server/main.js%22,%0A                %22--log%22,%0A                appiumLog.getAbsolutePath()%7D);%0A        Thread.sleep(10000);%0A        if (process != null) %7B%0A            System.out.println(%22Appium server started with %22 + appiumLog.getAbsolutePath());%0A        %7D%0A
     
@@ -918,24 +918,95 @@
 n%22, %228.2%22);%0A
+        File classpathRoot = new File(System.getProperty(%22user.dir%22));%0A
         File
@@ -1344,29 +1344,25 @@
 ver);%0A    %7D%0A
-    
 %0A
+
     @After%0A 
@@ -1401,149 +1401,29 @@
     
-if (driver != null) %7B%0A            driver.quit();%0A        %7D%0A        if (process != null) %7B%0A            process.destroy();%0A        %7D
+driver.quit();
 %0A    %7D%0A
-    
 %0A   

Remove experimental Appium server launch code