Python 断言的使用方法
自动化测试常用断言的使用方法(python)
自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断。
这里介绍几个常用断言的使用方法,可以一定程度上帮助大家对预期结果进行判断。
这里介绍以下几个断言方法:
assertEqual
assertNotEqual
assertTrue
assertFalse
assertIsNone
assertIsNotNone
(一)assertEqual 和 assertNotEqual
assertEqual:如两个值相等,则pass
assertNotEqual:如两个值不相等,则pass
下面看下具体使用方法
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.support.v7.app.ActionBar.e[2]").click()#切到超模25tab
sleep(3)
self.assertEqual(self.driver.find_element_by_id('com.boohee.secret:id/tv_title').text,u'超模25','切到超模25tab失败')
(1)这边是通过id(com.boohee.secret:id/tv_title)获取它的text值,与预期“超模25”对比,如相等则pass;不相等则fail。
(2)后面的“切到超模25tab失败”是fail时需要打印的信息,可写可不写。
断言assertNotEqual反着用就可以了。
(二)assertTrue和assertFalse
assertTrue:判断bool值为True,则pass
assertFalse:判断bool值为False,则Pass
下面看下具体使用方法
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.TextView[1]").click()#点击登录入口
sleep(2)
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.EditText[1]").send_keys("testq1")#输入用户名
sleep(2)
self.assertTrue(self.find_element_by_id('com.boohee.secret:id/btn_login').is_enabled(),'未输密码登录按钮为不可点状态,Fail')
(1)这边是通过id(com.boohee.secret:id/btn_login)获取它的激活状态,如为True则pass;反之则fail。
(2)后面的“未输密码登录按钮为不可点状态”是fail时需要打印的信息,可写可不写。
断言assertFalse反着用就可以了。
(三)assertIsNone和assertIsNotNone
assertIsNone:不存在,则pass
assertIsNotNone:存在,则pass
下面看下具体使用方法
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.TextView[1]").click()#点击登录入口
sleep(2)
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.EditText[1]").send_keys("testq1")#输入用户名
sleep(2)
self.driver.find_element_by_xpath("//android.widget.LinearLayout[2]/android.widget.EditText[1]").send_keys("boohee")#输入密码
sleep(2)
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.Button[1]").click()#点击登录按钮
sleep(10)
self.assertIsNotNone(self.driver.find_element_by_id('com.boohee.secret:id/tv_edit_profile'),'无编辑资料按钮,登录失败,Fail')
(1)这边是通过寻找id(com.boohee.secret:id/tv_edit_profile)的元素是否存在,如存在则pass;不存在则fail。
(2)后面的“无编辑资料按钮,登录失败,Fail”是fail时需要打印的信息,可写可不写。
断言assertIsNone反着用就可以了。
最新文章
- windbg学习---!thread和.thread
- Spring学习总结(四)——表达式语言 Spring Expression Language
- java实例--海盗的最优方案
- WEB系统开发
- 关于Warn:name or service not known的解决办法
- 关于UPdate用法的
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- Javascript高级程序设计学习笔记一
- WinForm 使用皮肤,且单击按更换皮肤。
- Redmine插件及使用
- Nginx HTTP模块指令
- UML作业第三次:分析《书店图书销售管理系统,绘制类图
- hive小文件合并设置参数
- python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
- 同时使用Union和Order by问题(ORA-00933错误)解决
- 教师信息管理系统(方式一:数据库为oracle数据库;方式二:存储在文件中)
- 【转】BASE64编码简介
- ABAP-HTML浏览器
- Spring3.0.5jar包用法详解 [转载]
- leetcode two pointer