LoginSignup
4
4

More than 5 years have passed since last update.

iPhone SimulatorでJavaScriptの自動テストを動かす

Last updated at Posted at 2015-12-28

Karma, Jasmine のテストコードを,Appium, Protractor を用いてiPhone Simulatorで動かす.

前提・検証環境

Appiumの設定

npmでインストールする

$ npm install -g appium
$ appium -v
=> 1.4.13

シミュレータへのアクセスを許可する

$ authorize_ios

設定チェック

$ appium-doctor

image

今回はAndroidでは検証しないので「iOS checks were successful. 」だけ表示されればおk.

JSプロジェクトの設定

package.json

$ npm install grunt-contrib-connect --save-dev
$ npm install protractor --save-dev
$ npm install grunt-protractor-runner --save-dev

E2Eのテストフレームワーク protractor を使用する.
AngularJS向けのフレームワークだが,Angularを用いてないJSプロジェクトでも使用できる.

protractorはv3.0.0以上だと,テスト実行時にエラーが発生する可能性がある.
protractor Bug bug when started

image

そのため,v2.x系のバージョンに変更する.

package.json
"devDependencies": {
  "protractor": "^2.5.1"
}

protractor.conf.coffee

protractorの設定ファイルを新規作成する.

protractor.conf.coffee
exports.config =
  seleniumAddress: 'http://localhost:4723/wd/hub',

  capabilities:
    browserName: 'safari'
    'appium-version': '1.4.13'
    platformName: 'iOS'
    platformVersion: '9.2'
    deviceName: 'iPhone 5'

  framework: 'jasmine2'
  specs: [
    'spec/e2e/**/*.coffee'
  ]
  baseUrl: 'http://localhost:9000'
  jasmineNodeOpts:
    showColors: true
    defaultTimeoutInterval: 30000

'appium-version' はインストールしたappiumのバージョンを,platformVersion, deviceName はマシンにインストールされているシミュレータのバージョンを指定しないとテストが実行できない.

Gruntfile.coffee

Gruntfile.coffee
connect:
      options:
        open: true
      e2e: options:
        open: false
        port: 9000
 protractor:
      options:
        keepAlive: true
        noColor: false
      e2e:
        options:
          configFile: 'protractor.conf.coffee'

grunt.loadNpmTasks 'grunt-contrib-connect'
grunt.loadNpmTasks 'grunt-protractor-runner'

grunt.registerTask 'spec-e2e', ['connect:e2e', 'protractor:e2e']

テストコード

spec/e2e/spec.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,user-scalable=0" name="viewport" />
    <title>E2E template</title>
</head>
<body style="padding:0; margin:0">
<h1 id="title">E2E tempalte</h1>
</body>
</html>
spec/e2e/e2eSpec.coffee
describe 'E2E', ->
  describe 'Verify', ->
    beforeEach ->
      browser.ignoreSynchronization = true
      browser.driver.get('http://localhost:9000/spec/e2e/e2e-template.html')

    it 'should have title', ->
      expect(browser.driver.getTitle()).toEqual('E2E template')

テスト実行

Appniumを起動

$ appium &

image

(別コンソールで)テストコマンド実行

$ grunt spec-e2e

シミュレータが起動しすると,Welcomeページ(Lets's browser!)が表示された後,beforeEachで実行しているページ(e2e-template.html)が表示されテストが実行される.

-> -> -> ->

image

Appiumの実行終了する場合

$ killall -9 node

課題

現状,諸々の理由でプロダクションでは使用できていない.

  • テスト実行が遅い
  • Lets's browser!の画面で停滞してしまう時がある
  • bodyタグ内のDOM要素がうまく取得できない

参考

4
4
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
4
4