Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Calabash + Gearman:打造多手機同步測試機制

Calabash-Android 是 Android 的 UI 自動化測試框架,但它本身不支援多手機同步。本文利用任務分發系統 Gearman 的訊息同步能力,搭配 Calabash 湊出一套多機同步測試方案。

一、背景

Gearman 是一個任務分發框架,相比 Hadoop 更專注於「把任務派給合適的 worker」,而且門檻極低,用腳本就能跑起來。

$ sudo apt-get install gearman-job-server
$ gearmand -V
gearmand 1.0.6 - https://bugs.launchpad.net/gearmand

$ sudo apt-get install gearman-tools
$ gearman -H

# 啟動 job server 作為後台服務
$ sudo gearmand -d

二、同步測試的例子

以測試微信傳訊為例:A 發訊給 B,兩支 feature 分別在兩台手機上跑,靠 Gearman 交換「對方說了什麼」。

AA-send-message-to-BB–role-AA.feature:

Feature: 微信測試發送消息給好友-角色A

  Scenario: 微信測試發送消息給好友
    ...打開微信軟件,作為帳號A登錄,進入與好友B的聊天窗口
    When I send weixin message "A說,你好!"  # 微信聊天窗口中發送消息
    And  I send sync message "A說,你好!" to role "BB"
    Then I see "A說,你好!"  #我能看到自己說的話

    When I wait sync message $AA_sync_1 as role "AA"
    Then I see $AA_sync_1 #我能看到對方說的話
    ...

AA-send-message-to-BB–role-BB.feature:

Feature: 微信測試發送消息給好友-角色B

  Scenario: 微信測試發送消息給好友
    ...打開微信軟件,作為帳號B登錄,進入與好友A的聊天窗口
    When I wait sync message $BB_sync_1 as role "BB"
    Then I see $BB_sync_1  #我能看到對方說的話

    When I send weixin message "B說,你好!"  # 微信聊天窗口中發送消息
    And  I send sync message "B說,你好!" to role "AA"
    Then I see "B說,你好!"  #我能看到自己說的話
    ...

兩個終端各自綁定不同裝置執行:

# 終端 1
$ export ADB_DEVICE_ARG=HTC-G9
$ export GEARMAN_JOB_SERVER=localhost
$ calabash-android run weixin.apk -r features/ features/AA-send-message-to-BB--role-AA.feature

# 終端 2
$ export ADB_DEVICE_ARG=HWAWEI-P7
$ export GEARMAN_JOB_SERVER=localhost
$ calabash-android run weixin.apk -r features/ features/AA-send-message-to-BB--role-BB.feature

三、把 Gearman 封裝成 Calabash 步驟

sync_step.rb:

# encoding: utf-8
require 'calabash-android/calabash_steps'

When /^I wait sync message \$([^\$]*) as role "([^\"]*)"$/ do |msg_ev, role|
  gearman_job_server=ENV["GEARMAN_JOB_SERVER"]
  fail "環境變量::GEARMAN_JOB_SERVER::未定義! 設置方法: export GEARMAN_JOB_SERVER=localhost" if ( gearman_job_server == nil)
  uuid=`uuidgen`.strip 
  cmd="gearman -h #{gearman_job_server} -t 30000 -w -c 1 -f receiver_#{role} -- tee /tmp/#{role}-#{uuid}; cat /tmp/#{role}-#{uuid}"
  puts "角色#{role}准備執行命令:#{cmd}"

  message=`#{cmd}`.strip
  fail "未收到同步消息" if ( message == "" )
  ENV[msg_ev]=message
  puts "角色#{role}接收到同步消息: #{ENV[msg_ev]}"
end

When /^I send sync message "([^\"]*)" to role "([^\"]*)"$/ do |msg, role|
  gearman_job_server=ENV["GEARMAN_JOB_SERVER"]
  fail "環境變量::GEARMAN_JOB_SERVER::未定義! 設置方法: export GEARMAN_JOB_SERVER=localhost" if ( gearman_job_server == nil)
  fail "sync message 為空" if ( msg == "" )
  cmd="echo '#{msg}' | gearman -h #{gearman_job_server} -t 30000 -f receiver_#{role}"
  puts "角色#{role}准備執行命令:#{cmd}"

  response=`#{cmd}`.strip
  fail "發送同步消息失敗" if ( response != msg )
  puts "發送同步消息給角色#{role}: #{msg}"
end

When /^I send sync message \$([^\$]*) to role "([^\"]*)"$/ do |msg_ev, role|
  gearman_job_server=ENV["GEARMAN_JOB_SERVER"]
  fail "環境變量::GEARMAN_JOB_SERVER::未定義! 設置方法: export GEARMAN_JOB_SERVER=localhost" if ( gearman_job_server == nil)
  msg=ENV[msg_ev]
  response=`echo "${msg}" | gearman -h #{gearman_job_server} -f receiver_#{role}`
  fail "發送同步消息失敗" if ( response != msg )
  puts "發送同步消息給角色#{role}: #{msg}"
end

calabash_steps.rb:用環境變數間接傳值,讓「看到某段文字」這個步驟可以接收變數:

# encoding: utf-8
require 'calabash-android/calabash_steps'

Then /^I see \$([^\$]*)$/ do |text_ev|
  text = ENV[text_ev]
  steps %{
  Then I see "#{text}"
  }
end

關鍵設計是:等待方用 gearman -w 化身 worker 阻塞收訊,發送方用 -f receiver_角色 把訊息投給指定角色,兩邊因此能在不同手機、不同行程之間對齊時序。實務上要留意 -t 設定的逾時值必須大於真實等待時間,否則容易誤判為失敗。

Copyright © Linux教程網 All Rights Reserved