The second UI script--python+selenium Unittest+htmltestrunner and Python inheritance

Source: Internet
Author: User

There is a Python automation script for the identification and manipulation of common elements, which goes on to talk about Python class inheritance, its third unittest framework, and the application of the report collection package Htmltestrunner.

Let's go straight to the code.

Applications of UnitTest and Htmltestrunner:

#coding = Utf-8Import TimeImportUnitTestImportHtmltestrunnerImportOSImportSYS fromSeleniumImportWebdriver
# Inherit the UnitTest classclassTestClass (unittest. TestCase):
# UnitTest provides the initialization method, Setup, where you can do some initialization workdefsetUp (self):Print "Setup"Self.driver=Webdriver. Firefox () Self.driver.get ("http://www.baidu.com") Time.sleep (3)
# Here is the test methoddefTestsearch2 (self): input=SELF.DRIVER.FIND_ELEMENT_BY_ID ('kw') Search=SELF.DRIVER.FIND_ELEMENT_BY_ID ('su') Input.send_keys ("Byebye") Search.Click ()
defTestsearch (self): input=SELF.DRIVER.FIND_ELEMENT_BY_ID ('kw') Search=SELF.DRIVER.FIND_ELEMENT_BY_ID ('su') Input.send_keys ("Hello") Search.Click ()Print "Assertion"self.asserttrue (search.is_displayed (),"Baidu Yixia should display")
# Processing action After the end of the rundefTearDown (self):Print 'Test down ...' #driver.quit ()self.driver.close ()if __name__=='__main__': #Unittest.main (), here to illustrate, if the test method starts with test, then unittest can be identified, here you can directly invoke its main method to execute all the test methods, the order of operation is by the name of the test method to sort #UnitTest. Testcase.asserttrue ()

#这块是获取项目路径, the back will be placed in the directory under change when generating the report current_path=OS.GETCWD ()Print 'Current path:', Current_path Project_path=os.path.dirname (Current_path)Print "Project Path:", Project_path
#这块是通过unittest的testsuite方式来组织测试 Testsuite=UnitTest. TestSuite ()#testsuite.addtest (TestClass ("Testsearch2"))Testsuite.addtest (TestClass ("Testsearch")) Temp=Str (time.time ()) Filedir=project_path+"//report//"+temp os.makedirs (filedir) filename="//pyresult.html"filepath=filedir+filename FP=file (filepath,'WB')

# call Htmltestrunner to execute scripts and generate test reports, HTML-formatted runner=htmltestrunner.htmltestrunner (stream=fp,title=' Report', description='Demo') Runner.run (testsuite)

Note: The above with the # is a comment, due to direct editing reasons, so he did not show green, if you want to copy out, remember to pay attention to adjust.

Inheritance of Python classes:

#Coding=utf-8 fromPyseImportPyse fromTimeImportSleepclassPage (object):#login_url = ' http://www.126.com '    def __init__(Self, selenium_driver, Base_url, parent=None): Self.base_url=Base_url Self.driver=Selenium_driver self.timeout= 30self.parent=Parentdef_iopen (self,url):#url = self.base_url + URLSelf.base_url=url self.driver.open (URL)assertSelf.on_page (),'Did not land on %s'%URLdefIopen (self): Self._iopen (Self.url)defon_page (self):Print 'Get_url is:', Self.driver.get_url ()Print 'Base_url is:', Self.base_urlreturnSelf.driver.get_url () = =Self.base_urlclassLogin (Page):defsystemout (self):Print 'Pass'deftestlogin (): Driver=pyse ('FF')    Print 'get in'URL='http://www.baidu.com/'LG=login (Driver,"') lg._iopen (URL)if __name__=='__main__': Testlogin ()

This is the Python class inheritance, page inherits the root object class, then login inherits the page class, and login can use the method of the page class.

The point here is that the page class has 2 methods, one is _iopen () and the other is Iopen (), and their difference is that when called with the Login object:

Calling Iopen does not require a URL parameter, but requires the login class to have a URL member because the Self.url in its parent page is actually invoking login's own URL member.

When calling _iopen, you can pass a URL parameter directly.

The second UI script--python+selenium Unittest+htmltestrunner and Python inheritance

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.