LoginSignup
30
29

More than 5 years have passed since last update.

②Appium+JUnitで自動テストを書いてみた【iOS編】

Last updated at Posted at 2014-05-18

あわせて読みたい:
①Appiumを使ってみた【iOS編】
③Appiumでどこまで出来るか試してみた【iOS編】
④AppiumでWebアプリのテストを実機で実行する【iOS編】

1. はじめに

前回の投稿では、Appiumの導入〜簡単なテストの実行方法までについて学びました。
今回はより実践的な内容として JUnitで書かれたJavaのテストコード を使用した方法について記述していきたいと思います。
 
  

2. 事前準備

1. TestAppを実機にインストールする
前回に引き続き、アプリはAppiumのサンプルコードコードに同梱されているTestAppを使用します。
Xcodeを使用して実機へアプリのビルド&インストールを行います。※詳細な手順は前回の投稿を参照
 
2. Mavenをインストールする

Javaで書かれたテストの実行にはMavenが必要となりますので、あらかじめインストールしておきます。

brew install maven

 
 

3. 簡単なテストコードの例

JUnitで書かれた公式のサンプルコードを参考に、TestAppをテストするシンプルなテストコードを書いてみました。

MySimpleTest.java
package com.saucelabs.appium;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;

public class MySimpleTest {
  private AppiumDriver driver;

  // 各テストメソッド実行前に呼ばれる処理
  @Before
  public void setUp() throws Exception {
    // Appiumの設定
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("app", "io.appium.TestApp");

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

  // 各テストメソッド実行後に呼ばれる処理
  @After
  public void tearDown() throws Exception {
    driver.quit();
  }

  // テストメソッド
  @Test
  public void testCalc() throws Exception {
    // 要素の取得
    WebElement tf1 = driver.findElement(By.xpath("//UIATextField[1]"));
    WebElement tf2 = driver.findElement(By.xpath("//UIATextField[2]"));
    WebElement btn = driver.findElement(By.xpath("//UIAButton[1]"));
    WebElement sumLabel = driver.findElementsByClassName("UIAStaticText").get(0);

    // 要素を操作
    tf1.sendKeys("12");
    tf2.sendKeys("87");
    btn.click();

    // テスト結果の検証
    assertEquals("99", sumLabel.getText());
  }
}

ポイント

setUp()メソッドではAppiumの設定を行っています。
実機でテストを行う場合は、少なくても以下の項目を設定する必要があるようです。

  • platformName
  • app
  • udid
// 各テストメソッド実行前に呼ばれる処理
  @Before
  public void setUp() throws Exception {
    // Appiumの設定
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("app", "io.appium.TestApp");

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

※指定可能な項目の一覧はこちらを参照

これらの値はAppiumを起動するときの引数でも指定が可能です。
実際の運用を想定して、上記の例ではudidはテストコード内に記述していません。

なお、Appiumを起動する際の引数DesiredCapabilitiesの両方を指定した場合は、DesiredCapabilitiesの値が優先して使用されるようです。

 

JUnitのテストを実行するには

1.上記のソースコードをMySimpleTest.javaという名前で下記ディレクトリに保存します。

/appium/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium

2.AppiumをUDIDを指定して起動します。

appium -U "<実機のUDID>" &

3.junitフォルダにカレントディレクトリを移動します。

cd appium/sample-code/examples/java/junit

4.以下のコマンドでテストを実行します。

mvn -Dtest=com.saucelabs.appium.MySimpleTest test

 
 

4. 要素の指定方法

クラス名で指定

要素をクラス名(種類)で指定する方法です。

driver.findElementByClassName("UIAButton");
driver.findElement(By.className("UIAButton"));

クラス名には以下の値が指定可能です。※詳しくはiOS Developer Libraryを参照

UIAActionSheet
UIAActivityIndicator
UIAActivityView
UIAAlert
UIAApplication
UIAButton
UIACollectionView
UIAEditingMenu
UIAElement
UIAElementArray
UIAHost
UIAKey
UIAKeyboard
UIALink
UIALogger
UIANavigationBar
UIAPageIndicator
UIAPicker
UIAPickerWheel
UIAPopover
UIAProgressIndicator
UIAScrollView
UIASearchBar
UIASecureTextField
UIASegmentedControl
UIASlider
UIAStaticText
UIAStatusBar
UIASwitch
UIATabBar
UIATableCell
UIATableGroup
UIATableView
UIATarget
UIATextField
UIATextView
UIAToolbar
UIAWebView
UIAWindow

IDで指定

要素にaccessibilityIdentifieraccessibilityLabelが設定されている場合は、IDとして指定が可能です。

driver.findElementById("ComputeSumButton"));
driver.findElement(By.id("ComputeSumButton"));

XPathで指定

XPathを使用して指定することもできます。

driver.findElement(By.xpath("//UIANavigationBar//UIAButton[3]"));

複数の要素を取得する場合

複数の要素を取得するにはfindElementsByを使用します。

driver.findElementsByClassName("UIATextField");
driver.findElementsByClassName("UIAStaticText").get(0);

 
 

5. 要素の操作方法

操作系

タップ

driver.findElement(By.xpath("//UIAButton[1]")).button.click();

文字入力、クリア

driver.findElement(By.xpath("//UIATextField[1]")).sendKeys("12");
driver.findElement(By.xpath("//UIATextField[1]")).clear();

スワイプ

要素をスワイプするにはswipeコマンドを使用します。
durationの値は500〜6000(ミリ秒)の範囲で指定する必要があります。

swipe(int startX, int startY, int endX, int endY, int duration);
TableViewの3セル目をスワイプして削除ボタンを表示させる
WebElement cell = driver.findElement(By.xpath("//UIATableView[1]/UIATableCell[3]"));
Point pos = cell.getLocation();
driver.swipe(pos.getX() + 200 , pos.getY() + 10, pos.getX() + 50, pos.getY() + 10, 500);

 

取得系

テキストを取得

String str = driver.findElement(By.xpath("//UIATextField[1]")).getText()
Alert alert = driver.switchTo().alert();
String str = alert.getText();

指定したアトリビュート値を取得

WebElement slider = driver.findElement(By.xpath("//UIASlider[1]"));
String str = slider.getAttribute("value");

位置を取得

Point pos = driver.findElement(By.xpath("//UIAButton[1]")).getLocation();

サイズを取得

Dimension size = driver.findElement(By.xpath("//UIAButton[1]")).getSize();

表示されているかどうか

WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
assertEquals(true, button.isEnabled());

有効かどうか

WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
assertEquals(true, button.isDisplayed());

 
 

30
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
29