LoginSignup
31
26

More than 5 years have passed since last update.

AppiumのテストをRuby(RSpec)で書く

Last updated at Posted at 2014-11-21

Ruby環境の構築

Rubyのインストールがまだの場合は、はじめにRuby環境を構築します。
Macの場合はデフォルトでRubyがインストールされていますが、せっかくなので最新バージョンをインストールしましょう。

インストールに rbenv を使用すると、あとあとRubyのバージョンを簡単に切り替えられるので便利です。

rbenvをインストール
$ brew update
$ brew install rbenv ruby-build

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

$ source ~/.bash_profile
$ rbenv --version
# rbenv 0.4.0
rbenvでrubyをインストール
$ rbenv install --list
Available versions:
      ・
      ・
  2.1.1
  2.1.2
  2.2.0-dev
  2.2.0-preview1
  jruby-1.5.6
  jruby-1.6.3
      ・
      ・
$ rbenv install 2.1.2
使用するRubyバージョンを設定
$ rbenv global 2.1.2

参考になる記事:
http://dev.classmethod.jp/server-side/language/build-ruby-environment-by-rbenv/

 

Ruby(RSpec)でテストを書く

ディレクトリ構成

root
├ app
│ └ MyApp.app       # アプリのバイナリ(実機の場合は不要)
├ spec
│ ├ spec_helper.rb  # ヘルパーファイル
│ └ login_spec.rb   # テスト本体
└ Gemfile

Gemfile

appium_lib はテストを書く際の便利メソッドを集めたWebDriverのラッパーです。

Gemfile
source 'https://www.rubygems.org'

gem 'rspec',                 '~> 3.0.0'
gem 'appium_lib',            '~> 4.1.0'
gem 'selenium-webdriver',    '~> 2.43.0'

spec_helper.rb

このファイルでは、テスト実行時に起動する AppiumDriver の設定を行います。

ちなみに desired_caps の内容は、 Appiumサーバ起動時のオプション でも指定可能です。
(両方指定した場合はスクリプト内で指定したdesired_capsの内容が優先されるようです)

詳しい設定項目は公式ドキュメントの下記章を参照してください。
Appium server capabilitiesServer flags

spec_helper.rb(iOSの場合)
require "rubygems"
require "appium_lib"

def desired_caps
  caps: {
    platformName:  "iOS",
    versionNumber: "7.1",
    deviceName:    "iPhone Retina (3.5-inch)",
    app:           "app/MyApp.app",
  },
  appium_lib: {
    wait: 10
  }
end

RSpec.configure { |c|
  c.before(:each) {
    @driver = Appium::Driver.new(desired_caps).start_driver
    @driver.manage.timeouts.implicit_wait = 5
    Appium.promote_appium_methods Object
  }

  c.after(:each) {
    @driver.quit
  }
}

Androidの場合の desired_caps

spec_helper.rb(Androidの場合)
・・・
省略
・・・

def desired_caps
  caps: {
    platformName:  "Android",
    versionNumber: "4.1",
    deviceName:    "Android Emulator",
    app:           "app/MyApp.apk",
    appActivity:   ".main.ToMainActivity",
  },
  appium_lib: {
    wait: 10
  }

・・・
省略
・・・
end

login_spec.rb

このファイルに実際のテスト内容を書いていきます。

下記は、正常にログインできることを確認するテストコードの例です。
ログイン操作後に画面に表示されるであろう文字列を取得することで、テスト結果の判定を行っています。

login_spec.rb
require 'spec_helper'

describe "ログイン機能" do
  context "正しいユーザ/パスワードの場合" do
    it "ログイン出来ること" do
      find_element(:name, "username").send_keys("user@example.com")
      find_element(:name, "password").send_keys("1234")
      find_element(:name, "submit").click

      title = find_element(:xpath, "//UIAScrollView[1]/UIAStaticText[1]").text
      expect(title).to eq("ようこそ")
    end
  end
end

 

テストを実行する

Appiumサーバを起動

Appiumのテストを実行するには、あらかじめAppiumサーバを起動しておく必要があります。
下記いずれかの方法でAppiumサーバを起動します。

GUI
Appium.appをダブルクリックして起動し、 Launch ボタンをクリックします。

CUI
ターミナルで下記コマンドを実行します。

Appiumサーバを起動
appium &

ちなみにターミナルからAppiumを終了する場合は、下記コマンドを実行します。

Appiumサーバを終了
killall -9 node

RSpecでテストを実行

テストの実行自体は通常のRSpecのテストと同じです。

specファイルを指定してテストを実行
rspec spec/login_spec.rb

--color--format documentation オプションを付けるとテスト結果が見やすく表示されます。

テスト結果を"色付き"かつ"ドキュメント形式"で出力
rspec --color --format documentation spec/login_spec.rb
31
26
1

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
31
26