zoukankan      html  css  js  c++  java
  • 自动化测试常用断言的使用方法(python+selenium)

    自动化测试常用断言的使用方法(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
    • 2
    • 3

    (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
    • 2
    • 3
    • 4
    • 5

    (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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (1)这边是通过寻找id(com.boohee.secret:id/tv_edit_profile)的元素是否存在,如存在则pass;不存在则fail。
    (2)后面的“无编辑资料按钮,登录失败,Fail”是fail时需要打印的信息,可写可不写。
    断言assertIsNone反着用就可以了。

    总结:这边的断言虽然不能像人工判断预期结果那样准确,但合理灵活地运用,对于重要节点加上断言也是具有一定判断预期的效果的。

    原文转至:https://blog.csdn.net/zhuquan0814/article/details/51049498

  • 相关阅读:
    十分钟内学会:将HTML格式化为合法的XML
    十分钟内学会:根据数据库生成站点导航
    Adobe Apollo vs Joyeur Slingshot
    英语阅读推荐:你真的懂UPDATE语句吗 & 当有layout之时
    欲练 CSS ,必先宫 IE
    英语阅读推荐:海明威写作技巧 & UpdatePanel为何失灵
    英语阅读推荐:在AJAX中制作自定义验证服务 & 优秀网站的5个因素
    Code is Configuration
    十分钟内学会:自动识别GB2312与UTF8编码的文件
    学习 Ruby on Rails 真的很爽!
  • 原文地址:https://www.cnblogs.com/lelexiong/p/8989746.html
Copyright © 2011-2022 走看看