Appium一款开源自动化测试工具,可以非常快捷的为iOS和Android移动平台创建功能自动化测试用例。相比其他的移动自动化测试工具,Appium测试由于调用了Selenium的client库使其可以使用任意的语言,包括Python、Ruby、Node.js,Objective,java等。
  本文我们主要讨论如何通过junit java example tests测试完成iOS sample apps的测试(此处我们还会创建TestNG example Tests)
  当然在开始之前,我们首先需要下载Appium,下载地址为https://github.com/appium/appium,根据安装说明我们可以完成Appium的安装。
  运行以下命令行构建sample projects:
  grunt buildApp:TestApp
  grunt buildApp:UICatalog
  一旦sample projects完成构建,即可通过以下命令启动Appium:
  grunt appium
  变更工作目录到sample-code/examples/java/junit,运行测试
  mvn test
  或运行单个测试:
  mvn -Dtest=com.saucelabs.appium.SimpleTest test
  Java Appium测试与Selenium Test非常的相似,你可以创建一个RemoteWebDriver实例并指定DesiredCapabilities,脚本如下:
  @Before
  publicvoidsetUp()throwsException {
  // set up appium against a local application
  File appDir =newFile(System.getProperty("user.dir"),"../../../apps/TestApp/build/Release-iphonesimulator");
  File app =newFile(appDir,"TestApp.app");
  DesiredCapabilities capabilities =newDesiredCapabilities();
  capabilities.setCapability(CapabilityType.BROWSER_NAME,"iOS");
  capabilities.setCapability(CapabilityType.VERSION,"6.0");
  capabilities.setCapability(CapabilityType.PLATFORM,"Mac");
  //tell Appium where the location of the app is
  capabilities.setCapability("app", app.getAbsolutePath());
  //create a RemoteWebDriver, the default port for Appium is 4723
  driver =newRemoteWebDriver(newURL("http://127.0.0.1:4723/wd/hub"), capabilities);
  }
  完成以上脚本后即可直接通过类似selenium测试的方式完成测试脚本:
  @Test
  publicvoidexample()throwsException {
  // find an element by tag name
  WebElement button = driver.findElement(By.tagName("button"));
  button.click();
  // get the value of the element
  WebElement texts = driver.findElement(By.tagName("staticText"));
  assertEquals(texts.getText(),"some expected value");
  }