Appium 不同手机分辨率影响滑动操作的问题和解决
问题:不同手机因分辨率不同,进行滑动动作时,滑动的位置也不同,进而影响自动化成功率和维护成本。
比如场景:iOS(or Android)不同型号的3部手机,手机1,手机2,手机3。如何执行同一段代码,确保3部手机滑动操作的位置是基本一样的呢?
1.iOS
基本思路:iOS机型,基于Robot Framework,
1)调用关键字Get Window Width/Get Window Height,获取iOS不同手机屏幕的Width和Height,
2)通过Width和Height乘以一个合适的值(如0.25),生成坐标点${iOS_X1},${iOS_Y1}, ${iOS_X2},${iOS_Y2}
3)通过Appium开发包函数Swipe(x1,y1,x2,y2)方法进行Down/Up操作。
Mobile_iOS_Scroll[Arguments] ${direction} ${times}${width_wd} Get Window Width${height_wd} Get Window Height${iOS_X1} Evaluate ${width_wd}*0.5${iOS_Y1} Evaluate ${height_wd}*0.25${iOS_X2} Evaluate ${width_wd}*0.5${iOS_Y2} Evaluate ${height_wd}*0.6FOR ${i} IN RANGE ${times}Run Keyword If '${direction}'=='down' AppiumLibrary.Swipe ${iOS_X2} ${iOS_Y2} ${iOS_X1} ${iOS_Y1} 1000Run Keyword If '${direction}'=='up' AppiumLibrary.Swipe ${iOS_X1} ${iOS_Y1} ${iOS_X2} ${iOS_Y2} 1000END
2. Android
基本思路:Android机型,不同于iOS,Android不能通过Robot Framework的关键字来获取Android手机屏幕真实的Width和Height。
所以考虑通过Robot Framework调研Python,Python中用Android开发包支持的Shell命令来处理。
1)通过Android开发包支持的Adb Shell “adb shell wm size” 获取命令Width和Height,
2)经过返回结果的解析和调整,获取真实的Android手机的Width和Height,然后用Width和Height乘以一个合适的值(如0.25),生成坐标点 android_X1,android_Y1, android_X2,android_Y2
3)通过Adb Shell Input swipe方法进行Down/Up操作。(划动次数用参数times控制)
import subprocessdef android_adb_swipe(direction, times):#获得实际 width, height#此例子中'adb shell wm size'返回了width、height和其他字符。所以用到了str.rfind()等方法,对多余字符进行了字符处理,可根据你手机实际返回值酌情处理adb_cmd = 'adb shell wm size'pi = subprocess.Popen(adb_cmd, shell=True, stdout=subprocess.PIPE)str_width_height = str(pi.stdout.read())width_start_index = str_width_height.rfind(' ')width_end_index = str_width_height.rfind('x')height_start_index = str_width_height.rfind('x')height_end_index = str_width_height.rfind('\\')android_width = str_width_height[width_start_index+1:width_end_index]android_height = str_width_height[height_start_index+1:height_end_index]#scroll down or up via timesandroid_X1 = float(android_width) * 0.5android_Y1 = float(android_height) * 0.25android_X2 = float(android_width) * 0.5android_Y2 = float(android_height) * 0.6adb_cmd_down='adb shell input swipe '+str(android_X2)+' '+str(android_Y2)+' '+str(android_X1)+' '+str(android_Y1)adb_cmd_up='adb shell input swipe '+str(android_X1)+' '+str(android_Y1)+' '+str(android_X2)+' '+str(android_Y2)for i in range(int(times)):if(direction=='down'):subprocess.Popen(adb_cmd_down, shell=True, stdout=subprocess.PIPE)if(direction=='up'):subprocess.Popen(adb_cmd_up, shell=True, stdout=subprocess.PIPE)else:passtime.sleep(1)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
