はじめに
ゲームなみなさんこんにちは、Game Solutions Architect の森下です。
この投稿では、物理デバイスを管理せずにモバイルゲームの自動テストを実行する方法として、AWS Device Farm をご紹介します。
builders.flash メールメンバー登録
AWS for Games
AWS for Games ではより早い開発、よりスマートな運営、そしてより楽しいゲームへの成長という Build、Run、Grow の 3 つの柱に沿ってサポートします。本記事は Build の柱、 クラウドゲーム開発のお話になります。
アプリケーション開発における E2E テスト
自動化された E2E テストを構築する際の課題
AWS Device Farm とテストフレームワークによる自動テスト
サンプル: Android アプリを対象とした E2E テスト構築手順
本稿では、Android アプリを対象とした E2E テストをサンプルに、AWS Device Farm の使い方について解説していきたいと思います。なお、iOS アプリでもほぼ同様の流れになりますが、アプリ署名に一手間必要なため今回は割愛させていただきます。
Android アプリを利用した E2E テストの全体像は以下の通りです。
必要なものはアプリケーションファイル (.apk) とテストスイート (.zip) の 2 つになります。この 2 つを用意すれば、あとはマネジメントコンソール上から AWS Device Farm でテストを実行し、結果を得ることができます。
アーキテクチャ図
前提知識
構築手順に入る前に、本稿で利用しているテストフレームワークについて簡単に紹介します。
Appium はモバイルアプリケーションのための自動テストフレームワークです。AWS Device Farm では唯一クロスプラットフォームで対応しているフレームワークであり、iOS, Android 両方のテストを行いたい場合は第一選択肢になり得ます。なお後述しますが、Appium は簡単なパターンマッチングによるピクセルベースでのテストをサポートしており、本稿ではこちらを利用します。
WebdriverIO は、Node.js を基盤とするテストフレームワークであり、Web/モバイルの UI 操作を自動化するために利用されます。Appium は言語として JavaScript, Python, Java, Ruby をサポートしていますが、JavaScript を選択した場合のクライアント実装はこの WebdriverIO を利用することになります。
1. 準備
サンプルアプリのダウンロード
まず、テスト対象となるサンプルアプリをダウンロードします。本稿では、AWS Device Farm Workshop で利用されているサンプルアプリを利用します。
ダウンロードは こちら から。
なお、このアプリはゲームエンジンで作られているわけではなく、UI も構造化されていますが、本稿はモバイルゲームの自動テスト環境を想定しているため、あえてピクセルベースで組まれていると見立てて話を進めていきます。

テストスイートのダウンロード
次に、本稿で利用するテストスイートをダウンロードします。
ダウンロードは こちら から。
ダウンロードが完了したら、zip を解凍してください。テストスイートは以下のようなファイル構成です。
.├── package-lock.json├── package.json└── test ├── config │ ├── wdio.base.conf.js │ ├── wdio.devicefarm.conf.js │ └── wdio.local.conf.js ├── images │ ├── step1.png │ ├── step2.png │ └── step3.png └── specs └── example.e2e.js
2. テストスイートの内容について
実行に移る前に、テストスイートの主要なファイルの内容について、簡単に説明させていただきます。
test/config
このディレクトリ以下に、WebdriverIO の実行設定が格納されています。
wdio.base.con.js に基本的な設定が記述されており、それを拡張する形で wdio.local.conf.js にローカル実行の設定が、wdio.devicefarm.conf.js に AWS Device Farm 上における実行の設定が記述されています。
test/specs/example.e2e.js
こちらのファイルに、テストケースが記載されています。 基本的には browser.pause(3000) で待機しつつ、Appium の Image セレクター機能を利用して test/images/ 以下にある画像ファイルで要素の選択、クリックを行っています。 Appium の Image セレクター機能は、テンプレートマッチングを利用した要素検出を行う機能です。test/images/ 以下にはクリックして欲しい要素の画像が保存されており、うまく行けば順番に要素をクリックしてくれます。
describe('Nested View Test', () => {
it('should perform image selector test', async () => {
try {
browser.updateSettings({
fixImageTemplateSize: true,
fixImageTemplateScale: true,
});
await browser.pause(3000);
await $(`./test/images/step1.png`).click();
await browser.pause(3000);
await $(`./test/images/step2.png`).click();
await browser.pause(3000);
await $(`./test/images/step3.png`).click();
expect(true);
} catch (error) {
console.error('Test failed:', error);
throw error;
}
});
});
3. テストスイートの zip ファイル作成
AWS Device Farm では、テストスイートを .zip ファイルでアップロードします。また、Appium の言語として Node.js を選択する場合は、npm-bundle した結果をアップロードする必要があります。そのため、テストスイートの zip ファイルを作成するために以下の手順を実行していきます。
なお、この手順を実行するには Node.js のインストールが必要です。まだお済みでない場合は別途インストールをお願いいたします。
テストスイートのディレクトリへ移動
まず、手順 1 でダウンロードしたテストスイートのディレクトリへ移動します。
% ls
node_modules/ package-lock.json package.json test/
npm-bundle コマンドを実行
次に、テストスイートのディレクトリで npm-bundle コマンドを実行します。
% npm-bundle
androidsampletest-1.0.0.tgz
.zip ファイルを作成
最後に、zip コマンドを実行して .zip ファイルを作成します。
% zip MySampleAndroidTests.zip *.tgz
adding: androidsampletest-1.0.0.tgz (deflated 0%)
ディレクトリの内容を表示
ここまで実行した際のディレクトリの内容は以下の通りです。
% ls
MySampleAndroidTests.zip node_modules/ package.json
androidsampletest-1.0.0.tgz package-lock.json test/
4. AWS Device Farm 上での実行
それでは、AWS Device Farm 上でテストを実行したいと思います。
プロジェクトの作成
まず、AWS マネジメントコンソール上で AWS Device Farm のページへ移動します。
そうしたら、左側のメニューから「Projects」をクリックします。

「Create mobile project」をクリック
プロジェクト一覧のページに遷移したら、「Create mobile project」をクリックします。

「Project Name」を設定
「Project Name」の欄に mobilegame-test などと入力して、「Create」をクリックします。
これでプロジェクトの作成が完了しました。
プロジェクトを作成すると、自動的にプロジェクト画面へ遷移します。別の画面にいる場合は、再度左のメニューから「Projects」をクリックし、先ほど作成したプロジェクトを選択してください。

テスト実行の開始
プロジェクト画面で、中央のタブが「Automated tests」であることを確認し、「Create run」をクリックします。

aws-devicefarm-sample-app.apk をアップロード
「Choose application」の画面では、「Choose File」をクリックし、手順 1 でダウンロードした aws-devicefarm-sample-app.apk をアップロードします。
アップロードが完了したら、「Next」をクリックします。

MySampleAndroidTests.zip をアップロード
「Configure」の画面では、「Appium Node.js」を選択し、「Choose File」をクリックして、手順 3 で作成した MySampleAndroidTests.zip をアップロードします。

「Create a TestSpec」をクリック
アップロードが完了したら、TestSpec の入力欄が現れます。右下の「Create a TestSpec」をクリックしてください。

TestSpec にコードを貼り付け
TestSpec の入力欄が表示されますので、以下の内容を貼り付けてください。
version: 0.1
# This flag enables your test to run using Device Farm's Amazon Linux 2 test host. For more information,
# please see https://docs.aws.amazon.com/devicefarm/latest/developerguide/amazon-linux-2.html
android_test_host: amazon_linux_2
# Phases represent collections of commands that are executed during your test run on the test host.
phases:
# The install phase contains commands for installing dependencies to run your tests.
# For your convenience, certain dependencies are preinstalled on the test host. To lean about which
# software is included with the host, and how to install additional software, please see:
# https://docs.aws.amazon.com/devicefarm/latest/developerguide/amazon-linux-2-supported-software.html
# Many software libraries you may need are available from the test host using the devicefarm-cli tool.
# To learn more about what software is available from it and how to use it, please see:
# https://docs.aws.amazon.com/devicefarm/latest/developerguide/amazon-linux-2-devicefarm-cli.html
install:
commands:
# The Appium server is written using Node.js. In order to run your desired version of Appium,
# you first need to set up a Node.js environment that is compatible with your version of Appium.
- devicefarm-cli use node 20
- node --version
# Use the devicefarm-cli to select a preinstalled major version of Appium.
- devicefarm-cli use appium 2
- appium --version
# The Device Farm service automatically updates the preinstalled Appium versions over time to
# incorporate the latest minor and patch versions for each major version. If you wish to
# select a specific version of Appium, you can use NPM to install it.
# - npm install -g appium@2.1.3
# For Appium version 2, Device Farm automatically updates the preinstalled UIAutomator2 driver
# over time to incorporate the latest minor and patch versions for its major version 2. If you
# want to install a specific version of the driver, you can use the Appium extension CLI to
# uninstall the existing UIAutomator2 driver and install your desired version:
- appium driver uninstall uiautomator2
- appium driver install uiautomator2@2.44.0
- appium plugin install images
# We recommend setting the Appium server's base path explicitly for accepting commands.
- export APPIUM_BASE_PATH=/wd/hub
# Install the NodeJS dependencies.
- cd $DEVICEFARM_TEST_PACKAGE_PATH
# First, install dependencies which were packaged with the test package using npm-bundle.
- npm install *.tgz
# Then, optionally, install any additional dependencies using npm install.
# If you do run these commands, we strongly recommend that you include your package-lock.json
# file with your test package so that the dependencies installed on Device Farm match
# the dependencies you've installed locally.
- cd node_modules/*
- npm install
# The pre-test phase contains commands for setting up your test environment.
pre_test:
commands:
# Appium downloads Chromedriver using a feature that is considered insecure for multitenant
# environments. This is not a problem for Device Farm because each test host is allocated
# exclusively for one customer, then terminated entirely. For more information, please see
# https://github.com/appium/appium/blob/master/packages/appium/docs/en/guides/security.md
# We recommend starting the Appium server process in the background using the command below.
# The Appium server log will be written to the $DEVICEFARM_LOG_DIR directory.
# The environment variables passed as capabilities to the server will be automatically assigned
# during your test run based on your test's specific device.
# For more information about which environment variables are set and how they're set, please see
# https://docs.aws.amazon.com/devicefarm/latest/developerguide/custom-test-environment-variables.html
- |-
appium --use-plugins=images --base-path=$APPIUM_BASE_PATH --log-timestamp \
--log-no-colors --relaxed-security --default-capabilities \
"{\"appium:deviceName\": \"$DEVICEFARM_DEVICE_NAME\", \
\"platformName\": \"$DEVICEFARM_DEVICE_PLATFORM_NAME\", \
\"appium:app\": \"$DEVICEFARM_APP_PATH\", \
\"appium:udid\":\"$DEVICEFARM_DEVICE_UDID\", \
\"appium:platformVersion\": \"$DEVICEFARM_DEVICE_OS_VERSION\", \
\"appium:chromedriverExecutableDir\": \"$DEVICEFARM_CHROMEDRIVER_EXECUTABLE_DIR\", \
\"appium:automationName\": \"UiAutomator2\"}" \
>> $DEVICEFARM_LOG_DIR/appium.log 2>&1 &
# This code will wait until the Appium server starts.
- |-
appium_initialization_time=0;
until curl --silent --fail "http://0.0.0.0:4723${APPIUM_BASE_PATH}/status"; do
if [[ $appium_initialization_time -gt 30 ]]; then
echo "Appium did not start within 30 seconds. Exiting...";
exit 1;
fi;
appium_initialization_time=$((appium_initialization_time + 1));
echo "Waiting for Appium to start on port 4723...";
sleep 1;
done;
# The test phase contains commands for running your tests.
test:
commands:
# Your test package is downloaded and unpackaged into the $DEVICEFARM_TEST_PACKAGE_PATH directory.
# When compiling with npm-bundle, the test folder can be found in the node_modules/*/ subdirectory.
- cd $DEVICEFARM_TEST_PACKAGE_PATH/node_modules/*
- echo "Starting the Appium NodeJS test"
# Enter your command below to start the tests. The command should be the same command as the one
# you use to run your tests locally from the command line. An example, "npm test", is given below:
- npm run wdio-device-farm
# The post-test phase contains commands that are run after your tests have completed.
# If you need to run any commands to generating logs and reports on how your test performed,
# we recommend adding them to this section.
post_test:
commands:
# Artifacts are a list of paths on the filesystem where you can store test output and reports.
# All files in these paths will be collected by Device Farm.
# These files will be available through the ListArtifacts API as your "Customer Artifacts".
artifacts:
# By default, Device Farm will collect your artifacts from the $DEVICEFARM_LOG_DIR directory.
- $DEVICEFARM_LOG_DIR
コードを保存
内容を貼り付けたら、右下の「Save as」欄に mobilegame-test と入力して「Save as New」をクリックします。

次へ進む
全ての工程が終わったら、ページの一番下にある「Next」をクリックします。

デバイスプールを選択
「Select mobile devices to test」では、Top Devices が選択されていることを確認して「Next」をクリックします。このデバイスプールでは、世界的によく利用される Android デバイスが 5 つ選択されています。

設定完了
「Specify device state」では、何も変更せずに「Next」をクリックします。
最後の「Review and start run」で、入力内容を確認しつつ、一番下の「Confirm and start run」をクリックすればテストが開始されます!

5. 結果の確認
6. 後片付け
本稿のハンズオンでは、後片付けするリソースはありません。
このように、自動テストを必要なときに必要なだけ実行でき、後片付けも不要という手軽さは AWS Device Farm の魅力と言って良いでしょう。
今後の展望
まとめ
今回は、AWS Device Farm を利用したモバイルゲームの自動テスト環境を構築しました。AWS Device Farm は物理デバイスを管理せずに自動テストを実行できるサービスであり、より効率的なテスト環境の構築に役立つと考えられます。
今回解説した内容が、皆様のゲーム開発の助けになれば幸いです。
筆者プロフィール
森下 真孝
アマゾン ウェブ サービス ジャパン合同会社
ソリューションアーキテクト
国内のモバイルゲーム開発会社でサーバーサイドからクライアントサイドまでを担当。
ゲーム業界向けのソリューションアーキテクトとしてゲーム開発に携わるお客様をご支援しております。

Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages