LoginSignup
49
54

More than 3 years have passed since last update.

appiumでAndroid実機の自動受け入れテストをRubyで書く

Last updated at Posted at 2014-02-14

環境

  • ruby 2.1.0
  • npm 1.1.62
  • Android 4.2.2
  • Mac OS X 10.9.1
  • appium 0.15.0
  • selenium webdriver android-server 2.38.0

事前準備

手順

appiumをインストール

appiumインストール&起動
> npm install -g appium
> npm install wd
> appium &

selenium webdriver android-serverの準備

  • Androidのデバッグモードを有効にする
    • Android 4.2以上はデフォルトで開発者向けオプションがない
    • 「設定」=>「端末情報」=>「ビルド番号」を7回タップすると「設定」に開発者向けオプションが表示される
    • 「デバッグモード」をonにする
  • AndroidをUSBケーブルでMacにつないで、adbコマンド使えるようにする
    • Driverがなければインストール
    • PATHを通す
bashrc
$ vim ~/.bashrc
# AndroidSDK
export ANDROID_HOME="/Applications/adt-bundle-mac-x86_64-20131030/sdk/"
export PATH="/usr/local/heroku/bin:/Applications/adt-bundle-mac-x86_64-20131030/sdk/tools:/Applications/adt-bundle-mac-x86_64-20131030/sdk/platform-tools:$PATH"
  • selenium webdriver android-serverのインストール
    • AndroidをUSBケーブルでつないでおく
androidDriver
$ wget https://selenium.googlecode.com/files/android-server-2.38.0.apk
$ adb devices
List of devices attached
XXXXXXXXXX    device

$ adb -s XXXXXXXXXX -e install -r /foo/bar/hoge/android-server-2.38.0.apk
4000 KB/s (1591405 bytes in 0.388s)
        pkg: /data/local/tmp/android-server-2.38.0.apk
Success
  • port forwarding
    • Selenium Webdriverで使用するportをforwardingする
adb
$ adb -s HT36TS905424 forward tcp:8080 tcp:8080
// ちなみに解除
$ adb -s HT36TS905424 forward --remove tcp:8080

Androidの操作をrubyで記述する

  • 今回はrubyで使います
    • Java, javascript(node), .Net, php, pythonでもかけます
    • sample code
appium起動
$ git clone https://github.com/appium/appium.git ~/hogehoge
$ cd ~/hogehoge/appium/sample-code/examples/ruby
$ bundle install
  • 以下の様な感じでテストをかく
    • 以下のテストは「test」って文言でググる
test.rb
require 'test/unit'
require 'selenium-webdriver'

def init()
        driver = Selenium::WebDriver.for(:android)
    driver
end

class SettingsTest < Test::Unit::TestCase
        def setup
        device = `adb devices`
        device_hash = device.delete("\n").match(/\w+\tdevice$/).to_s.gsub(/\tdevice$/, '')
        `adb -s #{device_hash} shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity`
        `adb -s #{device_hash} forward tcp:8080 tcp:8080`
                @driver=init
        end
        def test_settings
        login_site = "http://google.com"
        @driver.navigate.to login_site
        @driver.find_element(:id, "lst-ib").send_keys("test")
        @driver.find_element(:name, "btnG").submit
        sleep 10
        end
        def teardown
                @driver.quit
        end
end
  • テストを実行
テスト実行
$ rspec test.rb
  • とすると、AndroidDriverが立ち上がり、googleにアクセスしてtestという文言で検索されます

参照

49
54
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
49
54