ruby API

Ruby API 文档

1.Calabash-android 启动

Apk 的重签名: calabash-android resign ***.apk 作用:为了保证在虚拟机或真机上的 apk 正常运行。 Apk 的运行: calabash-android run ***.apk 作用:启用 apk 完成 ruby 测试。第一次运行的时候会在当前目录下生成 test-server 文件夹。 P:经过一些简单的测试,需要 apk 具有 internet 的权限,即:

2.calabash-android 查询

Apk 的测试: calabash-android console ***.apk 若当前目录下也没有 test-server 文件夹,也会生成 test-server 文件夹。 查询 apk 界面的一些控件及组件,需要输入一下命令 start_test_server_in_background 作用是启动虚拟机运行当下的***.apk..如果不输入此命令,只需在虚拟机打开你当前要测的 apk 。

Query

基本形式:query(uiquery,*args) 查询的结果是以数组的形式的。我们以 button 为例: query(“button”) 返回当前界面上所有的 button 以 args 的键值对形式,最前面的为数组的下标,从 0 开始。 query(“button index:1”) 返回数组下标为‘1’的 button 的所有信息。 query(“button index:1”).first.keys 返 回 数 组 下 标 为 '1' 的 button 的 所 有 的 key 。 包 括 ( id ,enable, contentDescription, class ,text ,rect ,description) query(“button”,”key”) 对应了 3 查询出来的所有的 key,经测试有严格区分大小写,"rect"和"description"查询不出. query(“button”,”text”,”length”) 返回 button 上 text 的长度 也是以 args 形式 测试只有"id" "text"可以返回长度. 查询”id”时,如果某个 button 没有 ID,将返回错误结果。 query("button""text""toLowerCase") 将 button 的 text 以小写的单词返回。 P:同样适用于"button""textview" “checkbox” “edittext””checkedtextview””radiobutton”

radiogrounp”等等。 P:query(“button id:button”) 错误的查询方式。 P:对空格也严格把关 query(“button “)错误的查询。

Waiting

基本形式 Wait_for(options,&block) 我们在 ruby 中的使用: 1.wait_for_elements_exist(elements_arr, options={}) Eg:wait_for_elements_exist( ["TextView text:'#{arg1}'"], :timeout => 10) 在最大的等待时间为 10 秒的情况下 查询["TextView text:'#{arg1}'"]是否存在。存在返回真。 2.wait_for_elements_do_not_exist(elements_arr, options={}) Eg:wait_for_elements_do_not_exist( ["button marked:'Save'" "* marked:'Please sign in'"], :timeout => 2) 在最大的等待时间为 2 秒的情况下 marked 对应了 button 的”text”的值 “*”可以表示缺省 的”button”。不存在返回真。 3wait_for Eg:wait_for(:timeout => 5) { query("button marked:'Save'").size > 0 } 这会检查是否存在一个匹配“button”,标明:”save”。它将等待最多 5 秒(如果超过 5 秒传 球失败) 。它会发出查询,直到它被发现或 5 秒

Assertions

1.check_element_exists(query) Eg:check_element_exists("view marked:'#{expected_mark}'") 2.check_element_does_not_exist(query) 3.Check_view_with_mark_exists(expected_mark)

Touch

基本形式 touch(uiquery,option={}) Eg:touch("* marked:'Save'") *的缺省值可以是 id 或 text 。点击。 一些等价的表达式: touch("button index:0") touch("button") touch(query("button index:0")) touch(query("button").first) touch(query("button"))

Screenshot

基本形式: screenshot(options={:prefix=>nil, :name=>nil}) Eg:screenshot({:prefix => "/tmp":name=>"my.png"}) screenshot_embed(options={:prefix=>nil, :name=>nil, :label => nil}) Eg:screenshot_embed({:prefix => "/tmp" :name=>"my.png" :label => "Mine"}) 如果前缀和名称是零,它会使用默认值。 注意到应用程序的截图和嵌入到 cucumber reporters。

Pull and push files and folders from and to the device

基本形式 pull(remote, local) Eg:pull("/sdcard/file.jpg" "file.jpg") push(local, remote) Eg:push("file.jpg""/sdcard/file.jpg") 使用 adb 的规则同样适用于: 将无法从受限制的文件夹,如 /data/data 如果目标路径已经存在,它无预警覆盖 对于文件,必须提供完整的目标路径,即 将无法正常工作。

Read, write and clear SharedPreferences

get_preferences(name) Eg:preferences = get_preferences("my_preferences") 可用于给定的名称,返回 hash。 set_preferences(name, hash) Eg:set_preferences("my_preferences" {:name => "wadus" :email => "wadus@wadus.com" :id => 8, :active => true}) 作为给定名称的首选项设置给定的 hash。 clear_preferences(name) Eg:clear_preferences("my_preferences") 清除为给定的名称的 preferences。 从一个 activity 到另一个 activity Eg: When(/^用户进入到下一个界面 "(.*?)"$/) do |arg1| wait_for_activity arg1

end 必须有 wait_for_activity activity 否则无法从另一个 activity 上获得焦点,即无法对另一个 activity 上的控件或组件进行操作。

1.Query

Query(“*”) 查询界面上所有的控件 Query(“button”) 查询界面上所有的 button, 进过本人测试在 android 上 xml 文件上的所有控件都可以查到, 我们由此也可以猜测到查询就是通过读取 xml 上的信息来进行筛选匹配的。 Query(“* marked:‟text‟”) 查询界面文本信息为’text‘的所有控件,因为我们点击界面大都都已’text‘来来标明控 件,所以这个很有必要。

2.Touch

(1) Button

通过 id 来点击 button Touch(“button id:‟ „ ”) 通过文本标记来点击 button Touch(“button marked:‟ „ ”) performAction('press_button_with_text', buttonText) 通过 buttonNumber 来点击图片按钮 performAction('press_image_button_number', buttonNumber) 通过 text 来点击文本 performAction('click_on_text',text) 点击屏幕的位置 performAction('click_on_screen',x, y) Eg: Then /^I click on screen (\d+)% from the left and (\d+)%

from the top$/ do |x, y| performAction('click_on_screen',x, y) end Then /^I touch the "([^\"]*)" text$/ do |text| performAction('click_on_text',text) End Given /^I press the "([^\"]*)" button$/ do |buttonText| performAction('press_button_with_text', buttonText) 或者 Touch(“button marked:‟ buttonText „ ”)

End

(2)spinner

下拉框的选取及点击 Then /^I select "([^\"]*)" from "([^\"]*)"$/ do |item_text, spinner_content_description| performAction('select_item_from_named_spinner', spinner_content_description, item_text) End

(3)checkbox

选择框的选取及点击 Then /^I toggle checkbox number (\d+)$/ do |checkboxNumber| performAction('toggle_numbered_checkbox', checkboxNumber) End

(4)系统控件

返回键 Then /^I go back$/ do performAction('go_back') end 菜单键 Then /^I press the menu key$/ do performAction('press_menu') end 发送键(确认键) Then /^I press the enter button$/ do performAction('send_key_enter') end 左划 Then /^I swipe left$/ do performAction('swipe', 'left') end 右划 Then /^I swipe right$/ do performAction('swipe', 'right') End

向下滚动 Then /^I scroll down$/ do performAction('scroll_down') end 向上滚动 Then /^I scroll up$/ do performAction('scroll_up') end

3.Wait

等待时间 Then /^I wait for (\d+) seconds$/ do |seconds| performAction('wait', seconds) End 等待文本出现 Then /^I wait for "([^\"]*)" to appear$/ do |text| performAction('wait_for_text', text) End 在一定时间内等待文本出现 Then /^I wait up to (\d+) seconds for "([^\"]*)" to appear$/ do |timeout, text| performAction('wait_for_text', text, timeout) End 等待 table 的出现 Then /^I wait for the "([^\"]*)" tab to appear$/ do | tab | performAction('wait_for_tab', tab) End

4.Assert

断言 text Then /^I see the text "([^\"]*)"$/ do |text| performAction('assert_text',text, true) En

断言 activity Then /^the "([^\"]*)" activity should be open$/ do | expected_activity | actual_activity = performAction('get_activity_name')['message'] raise "The current activity is #{actual_activity}" unless( actual_activity == expected_activity || actual_activity == expected_activity + 'Activity' ) End 断言 list Then /^I should see following list:$/ do | expected_table | result = performAction('get_list_item_text') response_table = result['bonusInformation'] response_table.each_with_index do | row_data, index | row_data = JSON.parse( row_data ) response_table[index] = row_data end expected_table.diff!(response_table) End 断言 list 的某一条记录 Then /^The "([^\"]*)" for row (\d+) should be "([^\"]*)"$/ do | view_id, row, value | response = performAction( 'get_list_item_properties', '1' , row )['bonusInformation'] response = JSON.parse( response[0] ) if( response['children'] ) found_id = false response['children'].each do | view | if( view['id'] == view_id ) raise "Text is #{view['text']}, expected #{value}" unless( view['text'] == value ) found_id = true end end raise "Could not find view with ID: #{view_id}" unless( f

ound_id ) else raise "ID is #{response['id']}, expected #{view_id}" unless( response['id'] == view_id ) raise "Text is #{response['text']}, expected #{view_id}" unless( response['text'] == value ) end End 断言 TimePicker 时间 Given /^I set the time to "(\d\d:\d\d)" on TimePicker with index "([^\"]*)"$/ do |time, index| performAction('set_time_with_index', time, index) end 断言时间

Given /^I set the "([^\"]*)" time to "(\d\d:\d\d)"$/ do |content_description, time| performAction('set_time_with_description', content_description, time) end

5 enter_text

搜索框输入 Then /^I enter "([^\"]*)" into search field$/ do |text| performAction('enter_query_into_numbered_field', text, 1) end

Then /^I enter "([^\"]*)" into search field number (\d+)$/ do |text, number| performAction('enter_query_into_numbered_field', text, number) End Text 的输入 Then /^I enter "([^\"]*)" into "([^\"]*)"$/ do |text, name| performAction('enter_text_into_named_field',text, name) end 清除 Then /^I clear "([^\"]*)"$/ do |name| performAction('clear_named_field',name) End 在 DatePicker 上输入时间 Given /^I set the date to "(\d\d-\d\d-\d\d\d\d)" on DatePicker with index "([^\"]*)"$/ do |date, index| performAction('set_date_with_index', date, index) end 自定义的输入时间 Given /^I set the "([^\"]*)" date to "(\d\d-\d\d-\d\d\d\d)"$/ do |content_description, date| performAction('set_date_with_description', content_description, date) End

6.Long_press

长按并选择编号 Then /^I long press "([^\"]*)" and select item number "([^\"]*)"$/ do |text_to_press, index| performAction('press_long_on_text_and_select_with_index', text_to_press, index) end 长按并选择内容 Then /^I long press "([^\"]*)" and select "([^\"]*)"$/ do |text_to_press, context_text| performAction('press_long_on_text_and_select_with_text', text_to_press, context_text) end 长按 Then /^I long press "([^\"]*)"$/ do |text_to_press| performAction('press_long_on_text', text_to_press) end

新增: 点击页面上的图片按钮 performAction("press_image_button_number""2") 从 1 开始 点击 listview 中的某项 performAction('press_list_item',10,0) 第二个参数最多只能点击屏幕上显示的个数 第三个参数表示屏幕上的第几个 listitem 对于继承于 listview 的 gridview 以及自己写的继承类同样适用 获得当前 activity 的名字 puts(performAction("get_activity_name")) 样例输出: {"bonusInformation"=>[], "message"=>"LoginActivity""success"=>true} Puts(performAction("get_activity_name")[“message”])

将应用置为横屏 performAction("set_activity_orientation""landscape") 竖屏 performAction("set_activity_orientation""portrait")

performAction("finish_opened_activities")关闭所有 activity


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值