How to use common assertions for automated testing (Python+selenium)

Source: Internet
Author: User
Tags assert

How to use common assertions for automated testing (python)

In the automated test to find elements and operations, if the elements are easy to look at, I believe you can be more proficient in writing use case script, but light operation may not be enough, and sometimes need to judge the expected results.

The use of several common assertions is described here to help you judge the expected results to some extent.

Here are a few of the assertion methods:
Assertequal
Assertnotequal
Asserttrue
Assertfalse
Assertisnone
Assertisnotnone

(i) assertequal and assertnotequal
Assertequal: If two values are equal, pass
Assertnotequal: If two values are not equal, pass
Take a look at the specific use of the method

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) This side is obtained by the ID (com.boohee.secret:id/tv_title) its text value, in comparison with the expected "supermodel 25", such as the equivalent of the pass;
(2) The back of the "cut to supermodel 25tab failure" is a fail to print the information, can write not write.
Assert that assertnotequal can be used in reverse.

(ii) asserttrue and Assertfalse
Asserttrue: If the bool value is true, pass
Assertfalse: To determine the bool value is false, the pass
Take a look at the specific use of the method

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) This side is the ID (com.boohee.secret:id/btn_login) to get its activation status, such as true pass;
(2) The following "No password login button is not a point status" is a fail when the need to print information, can write not write.
Assert that Assertfalse can be used in reverse.

(iii) Assertisnone and Assertisnotnone
Assertisnone: Does not exist, then pass
Assertisnotnone: exists, then pass
Take a look at the specific use of the method

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 '),              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

(1) This is done by looking for an element of ID (com.boohee.secret:id/tv_edit_profile) if it exists, pass if present;
(2) The following "No edit Data button, Login failed, fail" is a fail when the need to print information, can write not write.
Assert that Assertisnone can be used in reverse.

Summary: The assertion of this side is not as accurate as the expected result of artificial judgment, but reasonable and flexible to use, for the important node plus the assertion also has a certain expectation of the effect of judgment.

The original text goes to: 51049498

How to use common assertions for automated testing (Python+selenium)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.