Intelligent Technology's Technical Blog

株式会社インテリジェントテクノロジーの技術情報ブログです。

Appium Inspector編

野口です。

モバイルアプリのテスト自動化フレームワークAppiumのInspectorという機能を使ってみようと思います。Inspectorを使うことで、操作した内容を簡単にコードとして記述してくれます。今回はiOSネイティブアプリで試してみたいと思います。

Appiumの概要・テスト実行については以下の記事をご参照ください。iti.hatenablog.jp
iti.hatenablog.jp


実行環境

実行環境は以下の通りです。

ホストPC OS X Mavericks 10.9.5
Xcode 6.2
Appium Version 1.3.7 (Ophiuchus)
iOSシミュレータ 8.2.0

テストするアプリは以前と同様、Appium公式サイトのサンプルを使用します。

f:id:IntelligentTechnology:20150406094150p:plain

公式ドキュメント等には記載は見つけられなかったのですが、Ophiuchus(へびつかい座)というコードネーム(?)がついています。

Inspector起動

appiumの公式ページからAppium.appをインストールします。その中にInspectorも含まれています。

インストールしたAppiumを起動すると、以下のような画面が出てきます。

f:id:IntelligentTechnology:20150401112027p:plain
Appiumの開発は活発で頻繁にリリースが行われるので、インターフェイスも度々変わっているようです。

右上の「Launch」ボタンを押下するとAppiumサーバーが起動します。

f:id:IntelligentTechnology:20150401112315p:plain

Appiumサーバー起動後、虫眼鏡のアイコンのボタンを押下するとInspectorが起動します。ただし、必要な設定を行っていないと起動してくれません。

"Could not start a new session

Be sure the Appium server is running with an application opened by using
the "App Path" parameter in Appium.app (along with package and activity
for Android) or by connecting with selenium client and supplying this in
the desired capabilities object."

と言われてしまいます。

f:id:IntelligentTechnology:20150401114659p:plain

一度「Stop」ボタンを押下して、Appiumサーバーを停止します。
今回はiOSアプリを試すので、Appleアイコンを押下して、各種設定を行います。
実行するアプリのパス、デバイスの種類、プラットフォームバージョンを設定します。

f:id:IntelligentTechnology:20150401115140p:plain

設定が完了したら、再度Appiumサーバーを起動し、Inspectorを起動します。

f:id:IntelligentTechnology:20150401120121p:plain

今度は"Could not get list of session from Appium Server"と言われました。

調べてみると、どうやらproxyが設定されていることがよろしくなかったようです。Appiumサーバへのアクセスだったらproxyを使わないように設定したところ、Inspectorを起動できました。(Inspectorが0.0.0.0へアクセスしに行こうとしていたようなので、Macのネットワーク設定の「プロキシ設定を使用しないホストとドメイン」で0.0.0.0を追加しました。localhostや127.0.0.1では上手くいきませんでした。)

f:id:IntelligentTechnology:20150401131920p:plain

Inspector実行

操作を記録するにはまず、「Record」ボタンを押下します。押下すると、レコーディングパネルが表示され、操作を記録できるようになります。

f:id:IntelligentTechnology:20150401142004p:plain

操作する要素を選択します。真ん中にあるナビゲーターや右に表示されているアプリのプレビュー、左下のLocatorで操作したい要素を選択できます。
赤い枠で囲まれた要素が選択中の要素です。ここでは「show alert」というボタンが選択されています。

f:id:IntelligentTechnology:20150401143002p:plain

要素を選択したら、左下のアクションパネルから操作を選択します。Touchではタップなどの操作、Textではフォームなどへの文字入力操作、Miscではアラートボタンの操作が行えます。
「Tap」を押下して、タップ操作を実行してみます。すると、アラートが表示され、レコーディングパネルに以下のコードが追加されました。

wd.findElement(By.name("show alert")).click();

f:id:IntelligentTechnology:20150401143017p:plain

さらに、「Misc」→「Accept Alert」ボタンを押下すると、アラートが閉じ、レコーディングパネルに以下のコードが追加されます。

wd.switchTo().alert().accept();

f:id:IntelligentTechnology:20150406141535p:plain

レコーディングパネルの「Add Boilerplate」にチェックを入れると、Seleniumのインスタンスのセットアップに必要なコードを追加してくれます。

import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;

public class {scriptName} {
	public static void main(String[] args) {
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("appium-version", "1.0");
		capabilities.setCapability("platformName", "iOS");
		capabilities.setCapability("platformVersion", "8.2");
		capabilities.setCapability("deviceName", "iPhone 5s");
		capabilities.setCapability("app", "/Users/noguchisatoshi/appium/sample-code/apps/TestApp/build/Release-iphonesimulator/TestApp.app");
		wd = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
		wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		wd.findElement(By.name("show alert")).click();
		wd.switchTo().alert().accept();
		wd.close();
	}
}

f:id:IntelligentTechnology:20150406131547p:plain

また、記録する言語はレコーディングパネルの左上のドロップダウンから選択できます。上記のような操作を行った後でも、記録する言語の変更が可能です。
f:id:IntelligentTechnology:20150406135211g:plain

テスト実行

テストを行うにはさらにテストコードを書く必要があります。今回はjavaでテストを書いていきたいと思います。Inspectorで作成されたコードにパッケージ名、各種クラスのimport、クラス名、アサーションを追加したものが、以下のCodeMakerTest.javaファイルになります。

package jp.co.iti.appium;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import java.util.concurrent.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class CodeMakerTest {
    
    private AppiumDriver wd;
    
    @Before
    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("appium-version", "1.0");
		capabilities.setCapability("platformName", "iOS");
		capabilities.setCapability("platformVersion", "8.2");
		capabilities.setCapability("deviceName", "iPhone 5s");
		capabilities.setCapability("app", "/Users/noguchisatoshi/appium/sample-code/apps/TestApp/build/Release-iphonesimulator/TestApp.app");
		wd = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
		wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	}
    
    @After
    public void tearDown() throws Exception {
        wd.quit();
    }
    
    @Test
    public void testShowAlertButton() throws Exception {
        wd.findElement(By.name("show alert")).click();
        assertEquals("Cool title this alert is so cool.", wd.switchTo().alert().getText());
        wd.switchTo().alert().accept();
    }
    
}

Mavenを使って、テストを実行します。

> cd sample-code/examples/java/junit
> mvn test -Dtest=jp.co.iti.appium.CodeMakerTest

実行すると、以下のようにシュミュレータが起動し、Inspectorで行った操作が実行されます。(Appiumサーバー起動前にUDIDを設定しておくと、実機での動作確認が行えます。)

f:id:IntelligentTechnology:20150406165838g:plain

Inspectorを使えばアプリの動きを確かめながら、操作をコード化してくれるので便利そうです。

以上、Appium Inspector編でした。