环境
1、appium下载:npm install –g appium
2、Appium-Python-Client下载:sudo pip install Appium-Python-Client
3、编译器:pycharm
4、依赖java环境、Android环境、python3环境
项目结构
屏幕快照 2019-03-07 下午3.29.06.pngcommon:基类的封装
封装通过元素id隐式等待10s查找元素的方法
# 通过id查找元素
def find_element_id(self, id, timeout=10):
logging.info("通过id查找元素:" + id)
try:
element = WebDriverWait(self.driver, timeout, 1, NoSuchElementException).until(
lambda driver: driver.find_element_by_id(id))
return element
except selenium.common.exceptions.TimeoutException:
logging.error("超时找不到元素")
except selenium.common.exceptions.NoSuchElementException:
logging.error("不存在这个元素")
method:测试用例的一些基本方法
获取指定长度的字符串作为评论的内容
#获取固定长度的随机字符串
def get_random_str(self, randomlength):
"""
生成一个指定长度的随机字符串
"""
random_str = ''
base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'
length = len(base_str) - 1
for i in range(randomlength):
random_str += base_str[random.randint(0, length)]
return random_str
testcase:具体的测试用例
添加长度为10的字符作为评论
def test_add_comment(self):
comment = Comment(self.driver)
comment.check_ui()
context=comment.get_random_str(10)
comment.add_comment(context)
comment.back()
self.assertTrue(comment.exist_element(context),"评论失败")
基础API
通过id查找元素
driver.find_element_by_id()
通过name查找元素
driver.find_element_by_name()
通过classname查找元素
driver.find_element_by_class_name()
通过xpath查找元素
driver.find_element_by_xpath()
android特有的方法,通过uiautomator查找元素
driver.find_element_by_android_uiautomator()
#比如通过uiautomator的text查找部门
driver.find_element_by_android_uiautomator('new UiSelector().text(" ")')
#比如通过uiautomator的index查找部门
driver.find_element_by_android_uiautomator('new UiSelector().index(" ")')
点击元素
ele.click()
输入框输入
ele.send_keys()
重启app,不清除数据
driver.launch_app()
重置app,清除数据
driver.reset()
更多API参考:http://appium.io/docs/en/about-appium/api/
unittest框架
执行测试用例前与appium服务建立连接的方法,每个测试用例前都会执行
def setUp(self):
加上@classmethod,每个类只执行一次setUp()方法
@classmethod
def setUp(self):
每个测试用例必须以test_开头,才会被识别为测试用例
def test_check(self):
comment = Comment(self.driver)
comment.check_ui()
time.sleep(3)
self.assertTrue(comment.exist_element("还没有人评论哦~"), "UI显示不正确")
用例执行完的善后工作,加上@classmethod每个类执行一次
def tearDown(self):
self.driver.quit()
unittest的断言,判断测试用例是否执行成功
#当前页面存在“还没有人评论哦~”则用例执行成功,否则提示“UI显示不正确”
self.assertTrue(comment.exist_element("还没有人评论哦~"), "UI显示不正确")
#当前页面不存在“还没有人评论哦~”则用例执行成功,否则提示“超过300字符不能发送成功”
self.assertFalse(comment.exist_element("还没有人评论哦~"), "超过300字符不能发送成功")
执行测试用例:
if __name__=='__main__':
unittest.main()
测试用例
评论功能.pngpython3使用HTMLTestRunner生成测试报告
下载:http://tungwaiyip.info/software/HTMLTestRunner.html
修改:
第94行,将import StringIO修改成import io
第539行,将self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()
第642行,将if not rmap.has_key(cls):修改成if not cls in rmap:
第766行,将uo = o.decode('latin-1')修改成uo = o
第775行,将ue = e.decode('latin-1')修改成ue = e
第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
遇到的问题:HTMLTestRunner不能显示用例中的print字符串
if isinstance(o,str):
*******
else:
*******
删掉if else 直接改成 uo=o
uo=o
导入python3的库,路径为:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
应用:
runner = HTMLTestRunner.HTMLTestRunner(stream=fw, title='Testing', description=u'用例执行情况')
runner.run(suite)
网友评论