"Selenium2" "Htmltestrunner"

Source: Internet
Author: User

When replying to the selenium2+python2.7 of the god of the worm, it was found that the Htmltestrunner module used to generate Htmltestrunner's test report was Python2 syntax. And I am more accustomed to Python3. And I also use the Python3.4 environment, in the Internet to find a lot of information, modified the next htmltestrunner.py

Reference: http://bbs.chinaunix.net/thread-4154743-1-1.html

: http://tungwaiyip.info/software/HTMLTestRunner.html

After modification: Http://pan.baidu.com/s/1tp3Ts ?

To modify a rollup:

Line 94th, change import Stringio to import IO

Line No. 539, change self.outputbuffer = Stringio.stringio () to Self.outputbuffer = Io. Stringio ()

Line No. 642, if not Rmap.has_key (CLS): modified to If not CLS in Rmap:

Line No. 766, change uo = O.decode (' latin-1 ') to UO = E

Line No. 775, change UE = E.decode (' latin-1 ') to UE = E

Line No. 631, change print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime) to print (Sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime))

Use Htmltestrunner under Python3.4, start, introduce Htmltestrunner module error.

In the Htmltestrunner 94 line, is the use of the Stringio, but Python3, there is no stringio. Instead, it's IO. Stringio. So change this line to import IO

In 539 rows of htmltestrunner, Self.outputbuffer = Stringio.stringio () is modified to Self.outputbuffer = IO. Stringio ()

After the modification, the module was successfully introduced.

Execute script code:

#-*-Coding:utf-8-*-#引入webdriver和unittest所需要的包from Selenium import webdriverfrom selenium.webdriver.common.by Import Byfrom Selenium.webdriver.common.keys Import keysfrom selenium.webdriver.support.ui import Selectfrom Selenium.common.exceptions Import nosuchelementexceptionfrom selenium.common.exceptions Import Noalertpresentexceptionimport UnitTest, Time, re# introduced Htmltestrunner package import Htmltestrunner class Baidu (unittest. TestCase): #初始化设置 def setUp (self): Self.driver = Webdriver. Firefox () self.driver.implicitly_wait () Self.base_url = "http://www.baidu.com/" Self.verificationer        Rors = [] Self.accept_next_alert = True #百度搜索用例 def test_baidu (self): Driver = Self.driver        Driver.get (Self.base_url) driver.find_element_by_id ("kw"). Click () driver.find_element_by_id ("kw"). Clear ()        driver.find_element_by_id ("kw"). Send_keys ("Selenium webdriver") driver.find_element_by_id ("su"). Click () Time.sleep (2) driver.close () def tearDown (self): Self.driver.quit () self.assertequal ([], Self.verificationerr ORS) If __name__ = = "__main__": #定义一个测试容器 test = unittest. TestSuite () #将测试用例, added to the Test Container test.addtest (Baidu ("Test_baidu")) #定义个报告存放的路径, support relative path File_path = "f:\\robottest\\r Esult.html "file_result= Open (File_path, ' WB ') #定义测试报告 runner = Htmltestrunner.htmltestrunner (stream = File_resul T, title = u "Baidu Search test Report", Description = u "use Case execution") #运行测试用例 runner.run (test) File_result.close ()

After running the test script, the error is found:

File "C:\Python34\lib\HTMLTestRunner.py", line 642, in Sortresult

If not Rmap.has_key (CLS):

So go to line 642 to modify the code:

Continue to error after running:

Attributeerror: ' str ' object has no attribute ' decode '

Go to 766, 772 lines continue to modify (note: 766 lines is UO and 772 lines is the UE, then blind, did not notice these, thought is the same, leading to some inexplicable errors, tossing half a day):

Modified to run, found and error:

File "C:\Python34\lib\HTMLTestRunner.py", line 631, in run

Print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime)

typeerror:unsupported operand type (s) for >>: ' Builtin_function_or_method ' and ' _io. Textiowrapper '

Go to 631 to view and discover the only print in the entire program:

Print >> sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime

This is the 2.x notation, we modify the print to 3.x, the following changes:

Print (Sys.stderr, ' \ntime Elapsed:%s '% (self.stoptime-self.starttime))

Continue running scripts, OK runs successfully

Viewing the specified directory generates the result.html

Click to open the report:

Faq

1. Do not generate test reports

Workaround:

Using Python, Htmltestrunner generated test reports, encountered a very strange problem, clearly run the results, no error, that is, do not generate test reports, tangled for a long time. Google+baidu search results are not satisfied, finally resolved, the first summary.

code Example
login.py

"" "Os:w7 64-bit ide:pycharmpy:python2.7.11" ""#-*-Coding:utf-8-*-__author__ ="XIEWM"Import timeFrom seleniumImport WebdriverImport HtmltestrunnerImport UnitTestFrom Po_loginImport LoginPageClassLogin(UnitTest. TestCase):DefSetUp(self): Self.driver = Webdriver. Firefox () Self.username =' xxxxx ' Self.password =' xxxxx 'DefTest_user_login(self): Driver = self.driver username = self.username Password = Self.password login_page = LoginPage (driver) login_page.o Pen () Login_page.type_username (username) login_page.type_password (password) login_page.submit () Time.sleep (3) def teardown (self): Self.driver.quit () if __name__ =  ' __main__ ': Suite = unittest. TestSuite () suite.addtest (Login ( ' e:\\testresult.html ' with open (filename, as Fp:runner = Htmltestrunner.htmltestrunner (STREAM=FP, title= u ' Test Report ', Description=u ' use case execution details: ') Runner.run (suite)   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

Workaround

1:filename = ' E:\testresult.html ', if it is in a Windows environment, the file name is used in the following formats.

‘E:\\testresult.html’  ②filename = r‘E:\testresult.html‘   ③filename = ‘E:/testresult.html‘ 
    • 1
    • 2
    • 3

2: If you do not use with the context management of the file, remember to fp.close() close the open file before you can save.

‘wb‘)fp.close()
    • 1
    • 2

3: The third kind of wonderful, see (for the same Code) (Pycharm IDE)
Figure A

Figure II

If it is run in figure one, there will be no report generation, as for the reason, possibly because if name = = 'main'. The cause

November 25, 2016 09:01:08, probably know what reason, because Pycharm comes with a test frame, in the upper right corner, click Edit Configurations→python tests→ Select the item you want to delete, This will not automatically execute the unittest.

4: Another situation, according to the above 3 methods are not, use shortcut key: Ctrl+shift+f10 still can't generate report, finally in the upper right corner of Pycharm, found a button (Shift + F9)

So you can run it, ⊙﹏⊙ b Khan. (only if you have to configure it in edit configurations, you need to run the file path of the. Py Script Path)

such as configuration.

5: Again encountered can not generate test report problems, according to the previous summary of the various methods or not, and finally, the original just modify a line of code on it, in this record.

#原 if __name__ == ‘__main__‘:if __name__ == ‘interface_demo‘:# 把main修改成自己的文件夹名就可以了至于if__name__ == ‘__main__‘ 的作用,google下。
    • 1
    • 2
    • 3
    • 4
    • 5

6: If it doesn't work, change the IDE (e.g. Atom Eclipse) or run directly in cmd

Python login.py

summed up so much. The above methods should be able to solve most of the problems, if you encounter other situations, will continue to summarize

"Selenium2" "Htmltestrunner"

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.