Selenium2+python Automation 30-Introduction of the UnitTest framework "reprint"

Source: Internet
Author: User

From the blog: Shanghai-leisurely

Original address: http://www.cnblogs.com/yoyoketang/tag/unittest/

From selenium import Webdriver
From selenium.webdriver.common.by Import by
From Selenium.webdriver.common.keys import keys
From Selenium.webdriver.support.ui import Select
From selenium.common.exceptions import nosuchelementexception
From selenium.common.exceptions import noalertpresentexception
Import UnitTest, time, re
Class Baidu (UnitTest. TestCase):
def setUp (self):
Self.driver = Webdriver. Firefox ()
Self.driver.implicitly_wait (30)
Self.base_url = "http://www.baidu.com/"
Self.verificationerrors = []
Self.accept_next_alert = True
def test_baidu (self):
Driver = Self.driver
Driver.get (Self.base_url + "/")
driver.find_element_by_id ("kw"). Send_keys ("Selenium Webdriver")
driver.find_element_by_id ("su"). Click ()
Driver.close ()
def is_element_present (self, how, what):
Try:self.driver.find_element (By=how, Value=what)
Except Nosuchelementexception, E:return False
Return True
def is_alert_present (self):
Try:self.driver.switch_to_alert ()
Except Noalertpresentexception, E:return False
Return True
def close_alert_and_get_its_text (self):
Try
Alert = Self.driver.switch_to_alert ()
Alert_text = Alert.text
If Self.accept_next_alert:
Alert.accept ()
Else
Alert.dismiss ()
Return Alert_text
Finally:self.accept_next_alert = True
def tearDown (self):
Self.driver.quit ()
Self.assertequal ([], self.verificationerrors)
if __name__ = = "__main__":
Unittest.main ()

Popping up is a bunch of code, is not a bit ignorant!, the following small part to explain:
Class Baidu (UnitTest. TestCase):
Baidu class inherits UnitTest. The TestCase class, which inherits from the TestCase class, is the way to tell the UnitTest module, which is a test case.

def setUp (self):
Self.driver = Webdriver. Firefox ()
Self.base_url = "http://www.baidu.com/"
Setup is used to set the initialization part, and the function in this method is called first before the test case executes. This will call the browser
And URL access is placed in the initialization section.

Self.verificationerrors = []
When the script runs, the wrong information will be printed to this list

Self.accept_next_alert = True
Whether to continue accepting the next warning.

def test_baidu (self):
Driver = Self.driver
Driver.get (Self.base_url + "/")
driver.find_element_by_id ("kw"). Send_keys ("Selenium Webdriver")
driver.find_element_by_id ("su"). Click ()
Our test script is placed in the Test_baidu, which we are not unfamiliar with, because the script we execute is here

def is_element_present (self, how, what):
Try:self.driver.find_element (By=how, value= what)
Except Nosuchelementexception, E:return False
Return True
The Is_element_present function is used to find out whether a page element exists, try...except .... Exception capture for the Python language.
The Is_element_present function is not very useful here, usually deleted, because it is common to judge whether a page element exists in TestCase.

def is_alert_present (self):
Try:self.driver.switch_to_alert ()
Except Noalertpresentexception, E:return False
Return True
Handling of the exception of the pop-up window

def close_alert_and_get_its_text (self):
Try
Alert = Self.driver.switch_to_alert ()
Alert_text = Alert.text
If Self.accept_next_alert:
Alert.accept ()
Else
Alert.dismiss ()
Return Alert_text
Finally:self.accept_next_alert = True
Close the warning and to get the processing of the text box, if Judgment statement has been used many times, is not unfamiliar; try....finally To Python
Exception handling.

def tearDown (self):
Self.driver.quit ()
Self.assertequal ([], self.verificationerrors)
The TearDown method is called after each test method executes, and this place does all the cleanup work done by the test cases, such as exiting
Browser and so on.

Self.assertequal ([], self.verificationerrors)
This is a difficult point to compare the list obtained by the previous Verificationerrors method, such as a list of verificationerrors
Not empty, the error message in the output list

if __name__ = = "__main__":
Unittest.main ()
The Unitest.main () function is used to test test cases in a class that start with test

In this analysis, we have a preliminary understanding of the UnitTest framework. Run the script because the UnitTest framework was introduced,

So the console outputs information such as the number of executions, the time of the use cases, and whether or not OK.

This article is only a preliminary analysis of the UnitTest framework, and the details will be discussed in the following chapters, such as Python's
exception handling mechanisms, and so on.

Selenium2+python Automation 30-Introduction of the UnitTest framework "reprint"

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.