LoginSignup
0
0

More than 1 year has passed since last update.

setup & sample run android app with appium + ruby in Mac

Posted at

Outline

先日、java , python によるtest自動化するための構築,sample codeを記載した
Selenium WebDriver Java Simple Program - Login
setup & sample run android app with appium + python in Mac

今回は、NativeAPPをRubyでテストするための第一歩までを記述する。

install ruby 3.x

Mac はDefaultでrubyがinstallされている。ただ、2.6と古いversionであり、3.x系を手動でインストールする
いまのrubyのversionを確認

ruby -v

ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]

ruby3.xをinstallする

brew install rbenv ruby-build
rbenv -v

rbenv 1.2.8

ruby-build --version

ruby-build 20221124

install可能なrubyをlistupする

rbenv install --list

image.png

今回、3.1.3を選んだ。

rbenv install 3.1.3
rbenv global 3.1.3

次に、installしたruby3がdefaultのpathになるようにする。

rbenv init

image.png

実行のメッセージ通りに、terminal起動時にruby3が有効になるように、.bash_profileに以下を追記する

eval "$(rbenv init - bash)"

これで、terminalを立ち上げなおし、ruby versionを確認する

ruby -v

ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin21]

install liblary

appiumのテストにappium_capybaraを用いる。
そこで、これに必要となるlibraryをinstallする。

まず、Gemfileを作成する

Gemfile

source "https://rubygems.org"

# Run against this stable release
group :development, :test do
  gem 'capybara','~>3.36.0'
  gem 'appium_lib','~>12.0.1'
  gem 'selenium-webdriver'
  gem 'appium_capybara'
  gem 'site_prism'
  gem 'rspec-rails', '~> 6.0.0'
  #gem 'rspec-rails'
end

installを実行する

bundle install

成功したら、以下のようになる。
image.png

Script

https://github.com/appium/appium_capybara/tree/master/example
こちらを参考に単純なテストスクリプトを実装した。
以下3ファイル用意する

Helper

spec_helper.rb

require_relative 'capybara_init'

RSpec.configure do |config|
  config.after(:each) { Capybara.current_session.driver.quit }

  config.after do |result|
    Capybara.current_session.driver.save_screenshot 'error.png' if result.exception
  end
end

capabilitiesに関するファイル

appiumに接続するデバイス環境に応じて設定する

capybara_init.rb

require 'rspec'
require 'capybara'
require 'capybara/rspec'
require 'appium_capybara'
require 'site_prism'

Capybara.register_driver(:appium) do |app|
  Appium::Capybara::Driver.new app, capabilities: {
      'platformName' => 'android',
      'platformVersion' =>  '10.0',
      'appium:deviceName' => 'XPERIA5',
      'appium:automationName' => 'UiAutomator2',
      'appium:app' => '/Users/xxxxx/App/compose_playground.apk',
      'appium:wdaLaunchTimeout' => 600000,
    },
    appium_lib: { server_url: 'http://localhost:4723/wd/hub' },
    global_driver: false
end

Capybara.default_driver = :appium

テスト本体

今回は単純にボタンをクリックするテストである

android_sample.rb

require_relative 'spec_helper'

describe 'UICompose smoke test' do
  it 'should detect the nav bar and get some methods' do

    expect(Capybara.current_driver).to be :appium

    capy_driver = Capybara.current_session.driver.appium_driver

    el1 = capy_driver.find_element( :id, "com.example.jetpackcomposeplayground:id/simple_text_example" )
    el1.click

  end
end

execute test

以下コマンドを実行する

bundle exec rspec android_sample.rb 
0
0
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
0
0