Skip to content

Instantly share code, notes, and snippets.

@Kesin11
Created December 5, 2017 01:48
Show Gist options
  • Save Kesin11/aaf56873bd22a17069332a4e3f532cd6 to your computer and use it in GitHub Desktop.
Save Kesin11/aaf56873bd22a17069332a4e3f532cd6 to your computer and use it in GitHub Desktop.
iOS TestNight #6の公開用サンプルコード
# recordVideoをバックグラウンドで実行
xcrun simctl io booted recordVideo screenshots/test.mov &
# プロセスIDを保存
PID=`echo $!`
# テスト実行
bundle exec rspec spec/scenario_test.rb
# バックグラウンドのrecordVideoにSIGINTシグナルを送信
kill -2 $PID
require 'open3'
require 'json'
class Recorder
attr_reader :udid, :pid, :file_path
def initialize(driver)
@udid = _get_udid(driver)
end
def _get_udid(driver)
device_name = driver.caps[:deviceName]
platform_version = driver.caps[:platformVersion]
# appiumが実行しているシミュレータのUDIDを取得
stdout, _stderr, _status = Open3.capture3('xcrun simctl list --json devices')
json = JSON.parse(stdout)
booted_device = json.dig('devices', "iOS #{platform_version}").find do |device|
device['state'] == 'Booted' && device['name'].match(device_name)
end
booted_device['udid']
end
def start(file_path)
raise 'UDID is null' unless @udid
raise 'Already started recording' if @pid
# バックグラウンドで録画を開始
@pid = spawn("xcrun simctl io #{@udid} recordVideo #{file_path}", out: '/dev/null', err: '/dev/null')
@file_path = file_path
Process.detach(@pid)
end
def stop
raise 'Any recording process started' unless @pid
# 録画終了
killed_process_num = Process.kill('SIGINT', @pid)
raise "Kill pid: #{@pid} did not end correctly" unless killed_process_num.positive?
# たまに終了に時間がかかる場合があるので待つ。既に終了している場合はエラーになるのでrescueで無視する
begin
Process.waitpid(@pid)
rescue Errno::ECHILD
end
@pid = nil
end
def remove_video
raise 'file_path is null' unless @file_path
File.delete(@file_path)
@file_path = nil
end
end
RSpec.configure do |config|
config.before(:each) do |example|
@driver = Appium::Driver.new(capability)
@driver.start_driver
@recorder = Recorder.new(@driver)
@recorder.start("#{ROOT}/screenshots/#{example.description}.mov")
end
config.after(:each) do |example|
@recorder.stop
# テストが通った場合は録画を消す(=失敗したものは残る)
@recorder.remove_video unless example.exception
@driver.driver_quit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment