Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium+python Reporting

I am doing some R&D on selenium+python. 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.

like image 831
Aniruddha Avatar asked Apr 18 '12 21:04

Aniruddha


2 Answers

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.

like image 194
Zeugma Avatar answered Nov 19 '22 17:11

Zeugma


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

like image 4
Shubham Jain Avatar answered Nov 19 '22 16:11

Shubham Jain