Module: AppiumHelper

Defined in:
lib/common/appium_helper.rb

Instance Method Summary collapse

Instance Method Details

#init_deviceObject

功能:

初始化当前设备信息,结果会存储在@desired_caps 这个Hash中

参数解释:

Example:

Example #1:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/common/appium_helper.rb', line 166

def init_device
    @caps = ConfigHelper::CONF["appium_driver"] || Hash.new

    if @caps['caps']['platformName'].downcase === 'android'
        # res = `adb devices`
        # => "List of devices attached\n9e6d6de3\tdevice\nc8e86f6e\tdevice\n\n"
        system 'adb devices > /dev/null 2>&1'
        device_name = `adb devices`.split(' ')[4]
        fail 'android 设备未连接' if device_name.nil?
        platform_version = `adb -s #{device_name} shell getprop ro.build.version.release`.split(' ')[0]
        @desired_caps = {
            'platformVersion' => "#{platform_version}",
            'deviceName' => "#{device_name}"
        }
    else
        # ideviceinfo是一个ios手机基于ideviceinstaller的命令,在安装ifuse的时候回要求用户安装
        # IDEviceinfo会返回当前ios设备的信息

        # `ideviceinfo`.split("\n").each do |info|
        #     $device_name = (info.split(' ')[1] if info.to_s.include?('DeviceName:')) || nil
        #     p '------' unless $device_name.nil?
        #     $platform_version = (info.split(' ')[1] if info.to_s.include?('ProductVersio')) || nil
        #     $udid = (info.split(' ')[1] if info.to_s.include?('UniqueDeviceID')) || nil
        # end

        for info in `ideviceinfo`.split("\n")
            if info.to_s.include?('DeviceName:')
                @device_name = info.split(' ')[1]
            end
            if info.to_s.include?('ProductVersion')
                @platform_version = info.split(' ')[1]
            end
            if info.to_s.include?('UniqueDeviceID')
                @udid = info.split(' ')[1]
            end
        end
        fail 'ios设备未连接' if (@device_name.nil? || @platform_version.nil? || @udid.nil?)
        @desired_caps = {
            'platformVersion' => "#{@platform_version}",
            'deviceName' => "#{@device_name}",
            'udid' => "#{@udid}"
        }
    end
    @caps['caps'] = @caps['caps'].merge(@desired_caps) unless @desired_caps.nil?

end

#install(path) ⇒ Object



213
214
215
216
# File 'lib/common/appium_helper.rb', line 213

def install path
    p 'app 正在现在安装中,请稍后...' if path.match(/^http/)
    install_app path
end

#press(opts = {}) ⇒ Object

功能:

长按操作
支持坐标与元素两种方式

参数解释:

  • opts:

Example:

Example #1: opts = href="0">element=>find_elements_stable(:id,‘com.taobao.trip.usercenter:id/usercenter_orderlist_title’), :duration=2000 press opts

Parameters:

  • element (Hash)

    a customizable set of options

  • x (Hash)

    a customizable set of options

  • y (Hash)

    a customizable set of options

  • duration (Hash)

    a customizable set of options



151
152
153
154
155
# File 'lib/common/appium_helper.rb', line 151

def press(opts={})
    # 默认2000ms,即2s
    opts[:duration] = 2000 unless opts.has_key? :duration
    Appium::TouchAction.new.long_press(opts).perform
end

#start_appiumObject

功能:

启动appium,初始化appium driver,
在supertest使用appium功能之前,你需要先调用它

参数解释:

Example:

Example #1:



22
23
24
25
26
27
28
29
# File 'lib/common/appium_helper.rb', line 22

def start_appium
    init_device
    p 'app path为网路地址,正在下载安装请稍后...' if @caps['caps']['app'] && @caps['caps']['app'].match(/^http/)
    $appium_driver.driver_quit unless $appium_driver.nil?
    $appium_driver = Appium::Driver.new(@caps)
    $appium_driver.start_driver
    Appium.promote_appium_methods self.class
end

#stop_appiumObject

功能:

与start_appium对应,您需要在用例退出之前调用stop_appium以完成appium的session清理

参数解释:

Example:

Example #1:



39
40
41
# File 'lib/common/appium_helper.rb', line 39

def stop_appium
    $appium_driver.driver_quit unless $appium_driver.nil?
end

#swipe_down(duration = 500) ⇒ Object

功能:

下滑操作
坐标(start_x: width/2, start_y: height*0.3) -> 坐标(end_x: width/2, end_y: height*0.8)

参数解释:

  • duration: 滑动所需时长,可以不加

Example:

Example #1:



127
128
129
130
131
132
133
# File 'lib/common/appium_helper.rb', line 127

def swipe_down(duration = 500)
    actions = Appium::TouchAction.new
    width = window_width
    height = window_height
    swipe start_x: width/2, start_y: height*0.3, end_x: width/2, end_y: height*0.8, duration: duration
    actions.perform
end

#swipe_left(duration = 500) ⇒ Object

功能:

向左滑动操作
坐标(start_x: width-1, start_y: height/2) -> 坐标(end_x: width/4, end_y: height/2)

参数解释:

  • duration: 滑动所需时长,可以不加

Example:

Example #1:



76
77
78
79
80
81
82
# File 'lib/common/appium_helper.rb', line 76

def swipe_left(duration = 500)
    actions = Appium::TouchAction.new
    width = window_width
    height = window_height
    swipe start_x: width-1, start_y: height/2, end_x: width/4, end_y: height/2, duration: duration
    actions.perform
end

#swipe_right(duration = 500) ⇒ Object

功能:

向右滑动操作
坐标(start_x: width/4, start_y: height/2) -> 坐标(end_x: width-1, end_y: height/2)

参数解释:

  • duration: 滑动所需时长,可以不加

Example:

Example #1:



93
94
95
96
97
98
99
# File 'lib/common/appium_helper.rb', line 93

def swipe_right(duration = 500)
    actions = Appium::TouchAction.new
    width = window_width
    height = window_height
    swipe start_x: width/4, start_y: height/2, end_x: width-1, end_y: height/2, duration: duration
    actions.perform
end

#swipe_up(duration = 500) ⇒ Object

功能:

上滑操作
坐标(start_x: width/2, start_y: height*0.8) -> 坐标(end_x: width/2, end_y: height*0.3)

参数解释:

  • duration: 滑动所需时长,可以不加

Example:

Example #1:



110
111
112
113
114
115
116
# File 'lib/common/appium_helper.rb', line 110

def swipe_up(duration = 500)
    actions = Appium::TouchAction.new
    width = window_width
    height = window_height
    swipe start_x: width/2, start_y: height*0.8, end_x: width/2, end_y: height*0.3, duration: duration
    actions.perform
end

#window_heightObject

功能:

获取屏幕高度

参数解释:

Example:

Example #1:



63
64
65
# File 'lib/common/appium_helper.rb', line 63

def window_height
    window_size.height
end

#window_widthObject

功能:

获取屏幕宽度

参数解释:

Example:

Example #1:



51
52
53
# File 'lib/common/appium_helper.rb', line 51

def window_width
    window_size.width
end