appium常用方法

来源:互联网 发布:基于unity3d的格斗类 编辑:程序博客网 时间:2024/05/16 05:33

1.相关链接:http://blog.csdn.net/liusuihui/article/details/64542082

                       https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=appium+contains%E6%96%B9%E6%B3%95

  

Text属性的方法

 

1
2
3
4
driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View")').click()         #text
driver.find_element_by_android_uiautomator('new UiSelector().textContains("View")').click()        #查找某个字符串包含View
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("Custom")').click()    #textStartsWith 查找某个串以Custom打头
driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^Custom.*")').click()    #textMatches

 

class属性的方法

 

1
2
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("Custom View")').click()     #className
driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches(".*TextView$").text("Custom View")').click()          #classNameMatches

 

 伪xpath方法定位

 这个方法还是比较有用,用来查找同级节点关系以及父节点关系

1
2
driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View").fromParent(new UiSelector().text("Accessibility Service"))').click()            #通过同级元素定位同级元素
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().text("Custom View"))').click()  #通过父级元素定位子集元素

 

 resourceId属性的方法

 

1
2
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")')    #resourceId
driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".*id/text1$")')  #resourceIdMatches

 

 


description方法

 

1
2
3
driver.find_element_by_android_uiautomator('new UiSelector().description("Custom View")').click()      #description
driver.find_element_by_android_uiautomator('new UiSelector().descriptionStartsWith("Custom")').click() #descriptionStartsWith
driver.find_element_by_android_uiautomator('new UiSelector().descriptionMatches("^Custom.*")').click() #descriptionMatches

 

 元素的其他属性

除了以上比较常用的方法外,UIAutomator还支持其他一些方法,比如根据控件属性是否可点击可聚焦可长按等来缩小要定位的控件的范围,具体使用方法不一一列举(checked,clickable,focesed.......)

 

1
driver.find_element_by_android_uiautomator('new UiSelector().clickable(true).text("Custom View")').click()

获取控件属性值:

father=self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.pakeage:id/novel_name")')print father.get_attribute("text")

登入操作:

Texts = self.driver.find_elements_by_xpath("//android.widget.EditText")username = Texts[0]username.send_keys("lius")pwd = Texts[1]pwd.send_keys("83698")self.driver.find_element_by_name("登录").click()

向上滑动:

self.driver.swipe(width / 2, heigh / 2, width / 2, heigh / 4)

向下滑动

self.driver.swipe(width / 2, heigh / 4, width / 2, heigh / 2)

点击:

self.driver.tap([(width*0.74,heigh*0.778)])

方法先写这么多,不懂的可以留言。看到了会及时回复


使用appium自带键盘,很好的处理文本框输入问题和中文问题

desired_caps["unicodeKeyboard"] = "True"desired_caps["resetKeyboard"] = "True"

new command 最大时间间隔

Appium:出现info: [debug] Didn't get a new command in 60 secs, shutting down...无法继续执行


desired_caps["newCommandTimeout"] =240


 获取信息类API

[java] view plain copy
  1. 1)获取默认系统语言对应的Strings.xml文件内的数据。  
  2.   
  3. get_app_string()  
  4.   
  5. 2)查找某一个语言环境对应的字符串文件Strings.xml内数据。  
  6.   
  7. get_app_string(String language)  
  8.   
  9. 3)获取当前activity,比如(.ApiDemos)  
  10.   
  11. current_activity()  
  12.   
  13.    

App安装与卸载类API

[java] view plain copy
  1. 1)根据bundleId来判断该应用是否已经安装  
  2.   
  3. is_app_installed(String bundleId)  
  4.   
  5. 2)安装app,appPath为应用的本地路径  
  6.   
  7. install_app(String appPath)  
  8.   
  9. 3)卸载app.bundleId在android中代表的是包名,而在ios中有专门的bundleId号。  
  10.   
  11. remove_app(String bundleId)  
  12.   
  13. 4)关闭应用,其实就是按home键把应用置于后台  
  14.   
  15. close_app()  
  16.   
  17. 5)启动应用  
  18.   
  19. launch_app()  
  20.   
  21. 6)先closeApp然后在launchAPP  
  22.   
  23. reset_app()  


文件操作类API

[java] view plain copy
  1. 1)将字符数组用64位格式写到远程目录的某个文件中。也可以理解为把本地文件push到设备上。  
  2.   
  3. push_file(String remotePath, byte[] base64Data)  
  4.   
  5. 2)将设备上的文件pull到本地硬盘上  
  6.   
  7. pull_file(String remotePath)  
  8.   
  9. 3)将设备上的文件夹pull到本地硬盘上,一般远程文件为/data/local/tmp下的文件。  
  10.   
  11. pull_folder(String remotePath)  


设置相关类API

[java] view plain copy
  1. 1)设置手机的网络连接状态,可以开关蓝牙、wifi、数据流量。通过NetworkConnectionSetting中的属性来设置各个网络连接的状态。  
  2.   
  3. set_network_connect(NetworkConnectionSetting connection)  
  4.   
  5. 2)得到当前网络的状态  
  6.   
  7. get_network_connection()  


用户操作类API

[java] view plain copy
  1. 1)ios隐藏键盘  
  2.   
  3. hide_keyboard()  
  4.   
  5. 2)隐藏键盘,只能用于ios上。  
  6.   
  7. hide_keyboard(String strategy, String keyName)  
  8.   
  9. 3)按下某个键,具体哪个键由key值决定,key值定义在AndroidKeyCode类中  
  10.   
  11. send_key_event(int key)  
  12.   
  13. 4)执行一个touch动作,该touch动作是由TouchAction封装的。  
  14.   
  15. perform_touchAction(TouchAction touchAction)  
  16.   
  17. 5)点击element控件中心点按下,duration*5毫秒秒后松开,如此重复fingers次。  
  18.   
  19. tap(int fingers, WebElement element, int duration)  
  20.   
  21. 6)点击(x,y)点按下,duration*5毫秒后松开,如此重复fingers次。  
  22.   
  23. tap(int fingers, int x, int y, int duration)  
  24.   
  25. 7)从(startx,starty)滑到(endx,endy),分duration步滑,每一步用时是5毫秒。  
  26.   
  27. swipe(int startx, int starty, int endx, int endy, int duration)  
  28.   
  29. 82个手指操作控件,从对角线向中心点滑动。  
  30.   
  31. pinch(WebElement el)  
  32.   
  33. 9)以(x,y)为基准,计算得出(x,y-100),(x,y+100)两个点,然后2个手指按住这两个点同时滑到(x,y)  
  34.   
  35. pinch(int x, int y)  
  36.   
  37. (10)与pinch(el)的动作刚好相反。两个手指由控件的中心点慢慢向控件的左顶点后右底点滑动。  
  38.   
  39. zoom(WebElement el)  
  40.   
  41. (11)和pinch(x,y)相反。两个手指从(x,y)点开始向(x,y-100)和(x,y+100)滑动。  
  42.   
  43. zoom(int x, int y)  
  44.   
  45. (12)锁屏多少秒后解锁  
  46.   
  47. lock_screen(int seconds)  
  48.   
  49. (13)模拟摇晃手机  
  50.   
  51. shake()  
  52.   
  53. (14)滚动到某个text属性为指定的字符串的控件  
  54.   
  55. scroll_to(String text)  
  56.   
  57. (15)滚动方向由element1到element2  
  58.   
  59. scroll(element1,element2)    
  60.   
  61. (16)滚动到某个text属性包含传入的字符串的控件  
  62.   
  63. scroll_to_exact(String text)  
  64.   
  65. (17)设置上下文  
  66.   
  67. context(String name)  
  68.   
  69. (18)可用上下文  
  70.   
  71. get_context_handles()  
  72.   
  73. (19)当前上下文  
  74.   
  75. get_context()  
  76.   
  77. (20)设置屏幕横屏或者竖屏  
  78.   
  79. rotate(ScreenOrientation orientation)  
  80.   
  81. (21)获取当前屏幕的方向  
  82.   
  83. get_orientation()  


获取控件类API

seleniumdriver

[java] view plain copy
  1. find_element_by_id  
  2. find_elements_by_id  
  3. find_element_by_name  
  4. find_elements_by_name  
  5. find_element_by_link_text  
  6. find_elements_by_link_text  
  7. find_element_by_partial_link_text  
  8. find_elements_by_partial_link_text  
  9. find_element_by_tag_name  
  10. find_elements_by_tag_name  
  11. find_element_by_xpath  
  12. find_elements_by_xpath  
  13. find_element_by_class_name  
  14. find_elements_by_class_name  
  15. find_element_by_css_selector  
  16. find_elements_by_css_selector  


appiumdriver

[java] view plain copy
  1. find_element_by_ios_uiautomation  
  2. find_elements_by_ios_uiautomation  
  3. find_element_by_android_uiautomator  
  4. find_elements_by_android_uiautomator  
  5. find_element_by_accessibility_id  
  6. find_elements_by_accessibility_id  


AppiumDriver的辅助类

主要针对手势操作,比如滑动、长按、拖动等。TouchAction的原理是讲一系列的动作放在一个链条中,然后将该链条传递给服务器。服务器接受到该链条后,解析各个动作,逐个执行。

[java] view plain copy
  1. TouchAction()  
  2.   
  3. 1)在控件上执行press操作  
  4.   
  5. press(WebElement el)  
  6.   
  7. 2)在坐标为(x,y)的点执行press操作  
  8.   
  9. press(int x, int y)  
  10.   
  11. 3)在控件el的左上角的x坐标偏移x单位,y左边偏移y单位的坐标上执行press操作。  
  12.   
  13. press(WebElement el, int x, int y)  
  14.   
  15. 4)释放操作,代表该系列动作的一个结束标志。  
  16.   
  17. release()   
  18.   
  19. 5)以el为目标,从另一个点移动到该目标上  
  20.   
  21. move_to(WebElement el)  
  22.   
  23. 6)以(x,y)点为目标,从另一个点移动到该目标上  
  24.   
  25. move_to( int x, int y)  
  26.   
  27. (7) 在控件的中心点上敲击一下  
  28.   
  29. tap(WebElement el)  
  30.   
  31. (8)在(x,y)点轻击一下  
  32.   
  33. tap(int x, int y)  
  34.   
  35. (9)以控件el的左上角为基准,x轴向右移动x单位,y轴向下移动y单位。在该点上轻击。  
  36.   
  37. tap(WebElement el, int x, int y)  
  38.   
  39. (10)代表一个空操作,等待一段时间  
  40.   
  41. wait_action()  
  42.   
  43. (11)等待ms秒  
  44.   
  45. wait_action(int ms)  
  46.   
  47. (12)控件长按  
  48.   
  49. long_press(WebElement el)  
  50.   
  51. (13)点长按  
  52.   
  53. long_press(int x, int y)  
  54.   
  55. 14)偏移点长按  
  56.   
  57. long_press(WebElement el, int x, int y)   
  58.   
  59. 15)取消执行该动作  
  60.   
  61. cancel() 


原创粉丝点击