[2020年2月版] MacでAppiumを動かしてみる (iOSシミュレーターでサンプル実行まで)

ヨメレバCSS
オリジナルCSS

 Appiumなる自動テストツールがありまして。その環境構築をしたときのメモです。

 これやってていろんなミドルウェアいれなきゃいけないし引っかかるところも時期によって違うだろうな…って思ったのでタイトルに年月入れてます。

スポンサーリンク
GoogleAdSence レクタングル(大)

環境

Mac OS 10.14.6
XCode 11.3.1

appiumのインストール

 appium本体を入れていきます。

node.js

$ brew install node

でインストールできます。

 これで最新が入ったはずなのですが、私の環境ではその後npmコマンドで

npm WARN npm npm does not support Node.js v13.8.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 6, 8, 9, 10, 11.
npm WARN npm You can find the latest version at https://nodejs.org/

とずっと言われてうまく動きませんでした。

$ node -v

 でv10.5.0が返ってくるので

$ where node

 をしてみると「(ホームディレクトリ)/.nodebrew/current/bin/node」を指しているようで、どうも昔にnodebrewを入れていたことがあったみたいでそちらが邪魔しているようでした。なのでnodebrewを削除してからbrew install nodeをします。

  • nodebrewの削除

    .zshrcで「.nodebrew/current/bin」へのPATHを設定していたので、それを削除します。

 また「$HOME/.nodebrew/」も削除しておきました。

appium

$ npm install -g appium
$ npm install -g wd

 wdもglobal(-g)にインストールしてしまいました。

 -gをつけない場合は

npm WARN saveError ENOENT: no such file or directory, open '(ホームディレクトリ)/package.json'
npm WARN enoent ENOENT: no such file or directory, open '(ホームディレクトリ)/package.json'

が表示され続けていたので、

npm init --yes

 で空のpackage.jsonを作ってやりました。

appium-doctor

 appiumの環境チェックツールであるappium-doctorはappiumとは別パッケージみたいなので、そちらを入れます。

npm install -g appium-doctor
$ appium-doctor --ios

 を実行してすべてにチェックが入るまでいろいろインストールを行なっていきます。

appium-doctorで引っかかったものインストール

 このあたりも人によって違うと思うので、ひとつの参考として。

opencv4nodejs

 npmでインストールしようとしたら「cmake: command not found」と言われてしまったので、先にcmakeを入れる

$ brew install cmake
$ npm install -g opencv4nodejs

(参考:C++初心者がMacでCMakeを利用してみる)

ffmpeg

$ brew install ffmpeg

mjpeg-consumer

$ npm install -g  mjpeg-consumer

set-simulator-location

$ brew install lyft/formulae/set-simulator-location

(参考:https://github.com/lyft/set-simulator-location)

idb

$ brew tap facebook/fb
$ brew install idb-companion
$ pip3 install fb-idb

Macはpipとpip3があり、python3を使うためにはpip3で入れます。

(参考:https://github.com/appium/appium-idb)

applesimutils

$ brew tap wix/brew
$ brew install applesimutils

(参考:https://github.com/wix/AppleSimulatorUtils)

ios-deploy

$ brew install ios-deploy

(参考:https://github.com/ios-control/ios-deploy/)

全部OKになったら

 最後に「AppiumDoctor Everything looks good, bye!」と表示されるので完了です!

設定と起動

 iOSシミュレータの起動許可をつけます。

$ sudo authorize-ios

 appiumを起動します。

$ appium &

iOSのサンプルを動かしてみる

サンプルコードの取得

 サンプルをgithubから取得してきます。

 サンプル実行についてはAppium入門を参考にしてpythonでやりました。

$ git clone https://github.com/appium/sample-code
$ git clone https://github.com/appium/ios-test-app
$ cd ios-test-app
$ xcodebuild -sdk iphonesimulator
$ cd ..
$ cp -R ios-test-app/build sample-code/sample-code/apps/TestApp/build
$ cd sample-code/sample-code/examples/python

 ここまではそのまま。

ios_simple.pyの修正

 テストに使用するスクリプトを修正します。

 まずappのパスが微妙に違ってて、「release」を「Release」に直す必要があります。

14c14
<         app = os.path.abspath('../../apps/TestApp/build/release-iphonesimulator/TestApp-iphonesimulator.app')
---
>         app = os.path.abspath('../../apps/TestApp/build/Release-iphonesimulator/TestApp-iphonesimulator.app')

 このreleaseとReleaseが意外と厄介で、エラーメッセージとしては

E       selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: simctl error running 'install': An error was encountered processing the command (domain=NSPOSIXErrorDomain, code=2):
E       Failed to install the requested application
E       An application bundle was not found at the provided path.
E       Provide a valid path to the desired application bundle.

 ってはっきり出てくるんですが、手動

$ xcrun simctl install (UDID) TestApp-iphonesimulator.app/

 みたいに試すとインストールできてしまう…Tabの補完機能がzshは強力なので勝手に「Release」にしてくれたりしてむしろ気がづかなかったりもして…

 それからdesired_capabilitiesの設定を修正します。

20,21c20,22
<                 'platformVersion': '10.1',
<                 'deviceName': 'iPhone 6'
---
>                 'platformVersion': '12.1',
>                 'deviceName': 'iPhone 6',
>                 'automationName': 'XCUITest'

 platformVersionは作成するシミュレータに合わせます。XCodeの「Window > Devices and Simulators」より設定したいタイプを開いて、書いてあるiOSのバージョンをplatformVersionに入れます。ここが間違っていると

E       selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not create simulator with name 'appiumTest-(UDID)-iPhone 6', device type id 'iPhone 6', with runtime ids 'com.apple.CoreSimulator.SimRuntime.iOS-6-1', '6.1'

といったエラーが出ます。

 「'automationName': 'XCUITest'」は

selenium.common.exceptions.SessionNotCreatedException: Message: A new session could not be created. Details: Appium's IosDriver does not support Xcode version 11.3.1. Apple has deprecated UIAutomation. Use the "XCUITest" automationName capability instead.

エラーが出る場合に追加。

python3でpytestを実行する

 Macのpythonは2.7でappiumモジュールがうごいてくれないため、python3で実行します。

 そのままだと

ImportError while importing test module '(ホームディレクトリ)/appnium/sample-code/sample-code/examples/python/ios_simple.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
ios_simple.py:7: in <module>
    from appium import webdriver
E   ImportError: No module named appium

 とappiumモジュールがなかったので、

$ pip3 install  Appium-Python-Client

 でpython3向けにインストール。

$ python3 -m pytest ios_simple.py

 とpytestを実行するとサンプルアプリの画面が出てきて動きました! 勝手に数字が入ったりしていくの結構面白い!

感想

 めんどい 。環境をDockerとかに閉じ込めたいんだけどシミュレータはどうにもならんかなあ…

 ひとまずiOSシミュレータに絞ってサンプル動かすところまで書いてみました。iPhone実機やandroidまでやるとごちゃごちゃしちゃうので…

 もともとミドルウェアが入っているかどうかで引っかかるポイントとかも違ってきそうですし、なんかバージョン上がったら動かないものとかも出てきそうなので、ひとつの事例として参考になればと思います。

参考

スポンサーリンク
GoogleAdSence レクタングル(大)

シェアする

スポンサーリンク
GoogleAdSence レクタングル(大)