自动化测试常用断言的使用方法(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反着用就可以了。

最新文章

  1. windbg学习---!thread和.thread
  2. Spring学习总结(四)——表达式语言 Spring Expression Language
  3. java实例--海盗的最优方案
  4. WEB系统开发
  5. 关于Warn:name or service not known的解决办法
  6. 关于UPdate用法的
  7. BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
  8. Javascript高级程序设计学习笔记一
  9. WinForm 使用皮肤,且单击按更换皮肤。
  10. Redmine插件及使用
  11. Nginx HTTP模块指令
  12. UML作业第三次:分析《书店图书销售管理系统,绘制类图
  13. hive小文件合并设置参数
  14. python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
  15. 同时使用Union和Order by问题(ORA-00933错误)解决
  16. 教师信息管理系统(方式一:数据库为oracle数据库;方式二:存储在文件中)
  17. 【转】BASE64编码简介
  18. ABAP-HTML浏览器
  19. Spring3.0.5jar包用法详解 [转载]
  20. leetcode two pointer

热门文章

  1. Windows下配置Visualsvn Server时需要注意的几点事项
  2. CSRF漏洞详细说明
  3. URLRewrite 实现方法详解
  4. WPF程序开机速度策略
  5. 【01】循序渐进学 docker:到底是啥
  6. Java_Ant详解(转载)
  7. win7关闭关机时的自动终止的功能
  8. 校园网络安全CTF 第一题 和 你真了解我吗?
  9. 老男孩python作业5-开发一个简单的python计算器
  10. LeetCode记录之28——Implement strStr()