Automated test execution report for Selenium + Python 2.x
Asked Answered
R

6

9

I am doing some R&D on Selenium and Python 2.7.x.

I wrote some test cases in Python using Selenium WebDriver and unittest module.

I want to know how can I create report of the test cases. Is there inbuilt solution available in selenium or I need to code to generate file?

Or is there any other web testing framework with JavaScript support available in Python, which have reporting functionality?

I am basically new to Python as well as Selenium. Just trying to explore.

Recruit answered 18/4, 2012 at 21:59 Comment(0)
M
9

To start building test reports on top of Selenium+Python, I would leverage the python unittest module. You will get a basic sample in Selenium documentation here.

Then HTMLTestRunner module combined with unittest provides basic but robust HTML reports.

Mitman answered 18/4, 2012 at 22:13 Comment(1)
Note: HTMLTestRunner code will work only on Python 2.x version.Organism
M
5

Use HTMLTestRunner

Go to below URL :

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

  • Click on HTMLTestRunner.py
  • Copy all code
  • Create a file in your project with name HTMLTestRunner.py and dump the code
  • Now import that file in your script using import keyword
  • In main method call HTMLTestRunner

Example code:

from selenium import webdriver
import unittest
import HTMLTestRunner

class LoginTest(unittest.TestCase):

def setUp(self):

    print driverpath
    self.driver = webdriver.Chrome(driverpath + "chromedriver.exe")
    self.driver.get("http://google.com/")

def testPythonScript(self):
    driver=self.driver
    driver.maximize_window()
    driver.implicitly_wait(60)
    driver.get_screenshot_as_file(screenshotpath + "testPngFunction.png")
    driver.find_element_by_xpath("(//a[contains(@href,'contact-us')])[1]").click()
    driver.find_element_by_name("name").send_keys("shubham")
    driver.find_element_by_id("contactemail").send_keys("[email protected]")
    driver.find_element_by_css_selector("#contact_form > div:nth-child(3) > div:nth-child(3) > input").send_keys(
        "389198318312")
    driver.find_element_by_name("company").send_keys("myname")
    driver.get_screenshot_as_file(screenshotpath + "ConatctUs.png")
    print driver.title
    assert "Hello" in driver.title
    print "execution ends"

def testPythonFailScript(self):
    driver=self.driver
    driver.find_element_by_name("notExist").send_keys("done")

    def tearDown(self):
        driver = self.driver
        driver.quit();

if __name__ == "__main__":
    HTMLTestRunner.main()

Now open terminal and fire below command

python scriptFileName.py > TestReport.HTML

Note: scriptFileName is a python file name and TestReport is html report name. you can name it as you want

Mccullough answered 29/1, 2018 at 9:14 Comment(1)
Note: HTMLTestRunner code will work only on Python 2.x version.Organism
T
1

My experience has been that any sufficiently useful test framework will end up needing a customized logging solution. You are going to end up wanting domain specific and context relevant information, and the pre-baked solutions never really fit the bill by virtue of being specifically designed to be generic and broadly applicable. If you are already using Python, I'd suggest looking in to the logging module and learning how to write Handlers and Formatters. It's actually pretty straight forward, and you will end up getting better results than trying to shoehorn the logging you need in to some selenium-centric module.

Tell answered 18/4, 2012 at 22:8 Comment(0)
J
1

Consider using the robot framework. It has a plugin for selenium, and robot produces very nice logs and reports. With robot you don't directly write your tests in python (though, I suppose you can). Instead, robot is a keyword-based testing system built on top of python.

Jointly answered 18/4, 2012 at 22:20 Comment(0)
B
1

Robot Framework is a functional test framework that makes it possible for its users to:

  • Write tests in easily understood language
  • Organize what tests are run for specific purposes
  • Generate both high level and detailed test results
    • RF results file
    • RF log file
    • HTML page

enter image description here

Bayreuth answered 18/5, 2015 at 11:15 Comment(0)
A
0

In my specific application I use the Nose unittest extension for writing and running test suites.

On top of that I use an html-reporting plugin that produces nice reports from my test cycles.

Acrefoot answered 13/1, 2016 at 19:26 Comment(2)
can you please share sample code and configuration steps which requiredMccullough
especially please provoide the steps to genrate reportsMccullough

© 2022 - 2024 — McMap. All rights reserved.