Appium ruby客户端介绍
ruby_lib是一个支持Appium服务端的ruby客户端
支持Appium服务端的客户端列表,可在这里查看
Appium_lib是为Appium用ruby写跨平台的测试的客户端,它为用户提供了driver对象和很多有用的方法,来使用Appium
需要
Ruby: 2.2+
更新和安装
更新
gem update --system
安装
gem uninstall -aIx appium_lib
gem install --no-rdoc --no-ri appium_lib
ios端的使用
元素查看工具:
Appium桌面客户端
常用元素定位方法
注:所有查询定位都在当前App显示视图中处理
1、使用button查找按钮
first_button // 查找第一个button
button(value) // 查找第一个包含value的button,返回[UIAButton|XCUIElementTypeButton]对象
buttons(value) // 查找所有包含value的所有buttons,返回[Array<UIAButton|XCUIElementTypeButton>]对象
eg:
button("登录") // 查找登录按钮
2、使用textfield查找输入框
first_textfield // 查找第一个textfield
textfield(value) // 查找第一个包含value的textfield,返回[TextField]
eg:
textfield("用户名") // 查找
3、使用accessibility_id查找
id(value) // 返回id等于value的元素
eg:
id("登录") // 返回登录按钮
id("登录页面") // 返回登录页面
注意:iOS设置accessibility_id的方法
self.view.accessibilityLabel = "登录页面"
,ios的button和textfield通过设置text就能指定accessibility_id,所以无需设置accessibilityLabel,但view需要手动设置accessibilityLabel
4、通过find查找
find(value) // 返回包含value的元素
eg:
find("登录页面")
更多用法请看这里
常用事件
1、点击事件
// 通过坐标点击
tap(x: 68, y: 171)
// 通过按钮元素点击
button("登录").click
2、手势事件
// 滑动手势
swipe(direction:, element: nil) // direction - Either 'up', 'down', 'left' or 'right'.
eg: 上滑手势
swipe(direction: "up", element: nil)
更多用法请看这里
延时
// wait
wait { find("登录页面") } // 等待登录页面加载完成
// sleep
sleep(2) // 延时2秒
rspec断言
1、相等
expect(actual).to eq(expected) # passes if actual == expected
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).not_to eql(not_expected) # passes if not(actual.eql?(expected))
2、比较
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be < expected
expect(actual).to be_within(delta).of(expected)
3、类型判断
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be < expected
expect(actual).to be_within(delta).of(expected)
4、Bool值比较
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsy # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to_not be_nil # passes if actual is not nil
5、错误
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
6、异常
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
更多使用,请参考这里
小技巧
可通过methods方法,查看元素所有可用的属性和方法
eg:
并且(/^点击返回$/) do
puts driver.methods
end
methods:
[:network_connection_type, :network_connection_type=, :location, :location=, :set_location, :touch, :lock, :unlock, :reset, :window_size, :shake, :launch_app, :close_app, :device_locked?, :device_time, :current_context, :open_notifications, :toggle_airplane_mode, :current_activity, :current_package, :get_system_bars, :get_display_density, :is_keyboard_shown, :get_network_connection, :get_performance_data_types, :available_contexts, :set_context, :app_strings, :install_app, :remove_app, :app_installed?, :background_app, :hide_keyboard, :press_keycode, :long_press_keycode, :set_immediate_value, :push_file, :pull_file, :pull_folder, :get_settings, :update_settings, :touch_actions, :multi_touch, :touch_id, :toggle_touch_id_enrollment, :ime_deactivate, :ime_activate, :ime_available_engines, :ime_active_engine, :ime_activated, :find_element, :find_elements, :local_storage, :session_storage, :remote_status, :rotate, :rotation=, :orientation, :session_id, :save_screenshot, :screenshot_as, :file_detector=, :[], :inspect, :first, :close, :all, :action, :quit, :get, :ref, :title, :script, :window_handle, :window_handles, :mouse, :keyboard, :browser, :navigate, :switch_to, :manage, :current_url, :page_source, :execute_script, :execute_async_script, :capabilities, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :to_yaml, :to_yaml_properties, :psych_to_yaml, :cucumber_instance_exec, :to_json, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :kind_of?, :instance_variables, :tap, :method, :public_method, :singleton_method, :awesome_print, :is_a?, :extend, :define_singleton_method, :awesome_inspect, :to_enum, :enum_for, :ai, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :object_id, :display, :send, :gem, :to_s, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :frozen?, :!, :==, :!=, :send, :equal?, :instance_eval, :instance_exec, :id, :should, :should_not]
attribute:
An unknown server-side error occurred while processing the command. Original error: The attribute 'row' is unknown. Valid attribute names are: (
UID,
accessibilityContainer,
accessible,
enabled,
frame,
label,
name,
rect,
type,
value,
visible,
wdAccessibilityContainer,
wdAccessible,
wdEnabled,
wdFrame,
wdLabel,
wdName,
wdRect,
wdType,
wdUID,
wdValue,
wdVisible
)
Array methods
[:values_at, :delete_at, :delete_if, :reject!, :transpose, :fill, :assoc, :rassoc, :to_h, :uniq, :uniq!, :compact, :include?, :flatten!, :shuffle!, :flatten, :sample, :compact!, :shuffle, :repeated_permutation, :repeated_combination, :combination, :product, :permutation, :bsearch_index, :bsearch, :rotate!, :&, :*, :+, :-, :sort, :count, :find_index, :select, :reject, :collect, :map, :first, :any?, :pack, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :insert, :|, :index, :rindex, :replace, :clear, :<=>, :<<, :==, :[], :[]=, :reverse, :empty?, :eql?, :concat, :reverse!, :shelljoin, :inspect, :delete, :length, :size, :each, :slice, :slice!, :to_ary, :to_a, :to_s, :dig, :hash, :at, :fetch, :frozen?, :last, :push, :pop, :shift, :unshift, :join, :rotate, :each_index, :sort!, :sort_by!, :collect!, :map!, :select!, :keep_if, :to_json, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :lazy, :to_set, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :to_yaml, :to_yaml_properties, :psych_to_yaml, :cucumber_instance_exec, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :kind_of?, :instance_variables, :tap, :method, :public_method, :singleton_method, :awesome_print, :is_a?, :extend, :define_singleton_method, :awesome_inspect, :to_enum, :enum_for, :ai, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :display, :send, :gem, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :!, :!=, :send, :equal?, :instance_eval, :instance_exec, :id, :should, :should_not]