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

來源:互聯網
上載者:User

標籤:ttext   false   nbsp   密碼   ber   AC   輸入密碼   情況下   啟用   

自動化測試常用斷言的使用方法(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()#切到超模25tabsleep(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反著用就可以了。

總結:這邊的斷言雖然不能像人工判斷預期結果那樣準確,但合理靈活地運用,對於重要節點加上斷言也是具有一定判斷預期的效果的。

 

 

原文轉至:51049498

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.