appium使用教程(三)-------------用例编写

1. 驱动

import os, time, unittest
from appium import webdriver
PATH = lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p))

desired_caps = {}

  1. desired_caps['platformName'] = 'Android'  # 设备系统
  2. desired_caps['platformVersion'] = '4.2.2'  # 设备系统版本
  3. desired_caps['deviceName'] = 'H30-U10'  #  设备名称
  4. desired_caps["unicodeKeyboard"] = True  #设置输入法
  5. desired_caps["resetKeyBoard"] = True    #设置输入法
  6.  
  7.  
  8. # desired_caps['app'] = PATH(r"D:\zhihu.apk")
  9. desired_caps['appPackage'] = 'com.zhihu.android'
  10. desired_caps['appActivity'] = 'com.zhihu.android.app.ui.activity.MainActivity'
  11. def new_driver():
  12.     #设置driver
  13.     driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)       

    return driver

注意:其中第9行,如果使用appium安装应用的话则需要,手动安装则注销掉

10,11行有几种方法得到,建议使用解析apk直接得到,方法如下:

[cmd]

aapt dump badging D:\zhihu.apk

 

 

 

 

此处有个坑,开发者选项里面需要勾选一个使用自动化的一个选项,记不清到底是什么了,换了个手机竟然连接不上了…╮(╯▽╰)╭好气呀

1. 编写用例.

使用D:\Android\android-sdk\tools下面的一个工具进行定位

 

 

 

 

点击它

 

 

一般用test定位

 

2. 简单代码

driver = self.driver

try:
    time.sleep(10)
    driver.find_element_by_name("搜索").send_keys("xxx")
    driver.find_element_by_name("xxxxx").click()
    str = "xxxx"
    driver.find_element_by_name(str).click()
    time.sleep(5)
     #实例化
    driver.get_screenshot_as_file("new.png")
    print("页面打开成功")

涉及到一些隐私内容,已经替换成xxx

 

3. 其他

Appium是由selenium继承来的,所以使用appium的话需要有一些selenium的经验,毕竟selenium的环境比appium好搭太太太多了,至于再深入的东西,可以使用unittes框架,或者pytest.

 

posted @ 2017-04-07 15:11  两只老虎111  阅读(2342)  评论(0编辑  收藏  举报