1. 程式人生 > >自動化測試常用斷言的使用方法(python+selenium)

自動化測試常用斷言的使用方法(python+selenium)

打印 lin text 重要 string ID pre python idg

自動化測試常用斷言的使用方法(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

自動化測試常用斷言的使用方法(python+selenium)