如何一次在测试用例中运行所有这些方法并使用HTMLTestRunner生成报告?

时间:2013-06-03 12:25:08

标签: python unit-testing selenium python-unittest

我有这样的Python Unittest:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import HTMLTestRunner, time

class AddEmployer(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.openURL()

    def openURL(self):
        driver = self.driver
        driver.get("http://172.16.0.6:8080/rmt/")
        elem = driver.find_element_by_xpath("//input")
        elem.send_keys("admin")
        elem = driver.find_element_by_xpath("//div[2]/input")
        elem.send_keys("admin123")
        elem.send_keys(Keys.RETURN)
        self.driver.implicitly_wait(1000)
        #Manage Employer
        elem = driver.find_element_by_xpath("//div[3]/a/div/p").click()
        #Add Employee
        elem = driver.find_element_by_xpath("//div[2]/div/div/button").click()
        self.selectTrust()

    def selectTrust(self):
    #Selecting Trust
        driver = self.driver
        driver.find_element_by_xpath("//i/i").click()
        driver.find_element_by_xpath("//body/div[2]/div/div/div/table/tbody/tr/td/div/span").click()
        driver.find_element_by_xpath("//body/div[2]/div/div/div/table/tbody/tr[2]/td/div/span").click()
        driver.find_element_by_xpath("//body/div[2]/div/div/div/table/tbody/tr[3]/td/div/span").click()
        driver.find_element_by_xpath("//body/div[2]/div/div/div/table/tbody/tr[4]/td/div/span").click()
        driver.find_element_by_xpath("//body/div[2]/div/div/div/table/tbody/tr[5]/td/div/span").click()
        self.driver.implicitly_wait(1000)
        self.InsertID()
    # Inserting Employer ID

    def InsertID(self):
        driver = self.driver
        elem = driver.find_element_by_xpath("//div/input")
        elem.send_keys('4343')
        self.uploadLogo()

    def uploadLogo(self):     
        #Uploading Logo
        driver = self.driver
        elem = driver.find_element_by_xpath("//input")
        #self.driver.implicitly_wait(10)
        elem.send_keys("/home/santosh/Pictures/Downloaded/logo.jpg")
        self.driver.implicitly_wait(10)
        self.InsertName()

    def InsertName(self):
    #Inserting Employer Name
        driver = self.driver
        elem = driver.find_element_by_xpath("//tr[3]/td[2]/div/input")
        elem.send_keys('Universal Solution co.')
        # Inserting Employer website
        elem = driver.find_element_by_xpath("//tr[4]/td[2]/div/input")
        elem.send_keys('www.universal.com')
        # Inserting Employer e-mail
        elem = driver.find_element_by_xpath("//tr[5]/td[2]/div/input")
        elem.send_keys('contact@universal.com')
        # Inserting Employer Acronym
        elem = driver.find_element_by_xpath("//tr[6]/td[2]/div/input")
        elem.send_keys('USC')
        #Inserting Employer description
        elem = driver.find_element_by_xpath("//textarea")
        elem.send_keys('Universal soultion co. is the employer for all the trusts.')
        #Inserting Employer comment
        elem = driver.find_element_by_xpath("//tr[8]/td[2]/div/textarea")
        elem.send_keys('Trust of the year 2012')
        #Inserting Employer Street address
        elem = driver.find_element_by_xpath("//tr[10]/td[2]/div/input")
        elem.send_keys('East Boulevard. Wall-E street')
        #Inserting Employer city
        elem = driver.find_element_by_xpath("//tr[11]/td[2]/div/input")
        elem.send_keys('San Fransisco')
        #Selecting Employer state
        elem = driver.find_element_by_xpath("//td[2]/div/i/i").click()
        elem = driver.find_element_by_xpath("//div[2]/table/tbody/tr[5]/td[2]").click()
        #Inserting Zip codes
        elem = driver.find_element_by_xpath("//div/div/input")
        elem.send_keys('42351')
        elem = driver.find_element_by_xpath("//div[2]/input")
        elem.send_keys('4562')
        #Contact First Name
        elem = driver.find_element_by_xpath("//tr[14]/td[2]/div/input")
        elem.send_keys('Stephanie')
        #Contact Last Name 
        elem = driver.find_element_by_xpath("//tr[15]/td[2]/div/input")
        elem.send_keys('Winslet')
        #Contact E-mail
        elem = driver.find_element_by_xpath("//tr[16]/td[2]/div/input")
        elem.send_keys('stephanie_winslet@gmail.com')
        #Conatct Job Ttile
        elem = driver.find_element_by_xpath("//tr[17]/td[2]/div/input")
        elem.send_keys('Human Resource Manager')
        #Contact Comment
        elem = driver.find_element_by_xpath("//tr[18]/td[2]/div/input")
        elem.send_keys('No comments!!!')
        self.Submit()
        #Saving the employer

    def Submit(self):
        try:
            driver = self.driver
            elem = driver.find_element_by_xpath("//div/button").click()
            self.driver.implicitly_wait(10)
            elem = driver.find_element_by_xpath("//td/table/tbody/tr/td/button").click()
            self.driver.implicitly_wait(1000)
        except:
            driver = self.driver
            elem = driver.find_element_by_xpath("//div/input")
            elem.clear()
            elem.send_keys('436')
            elem.send_keys(Keys.TAB)



    def tearDown(self):
        self.driver.close()


if __name__ == "__main__":  
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(AddEmployer, 'openURL'))
    dateTimeStamp = time.strftime('%Y%m%d_%H_%M_%S')

    buf = file("TestReport" + "_" + dateTimeStamp + ".html", 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(
            stream=buf,
            title='Test the Report',
            description='Result of tests'
            )
    runner.run(suite)

我正在使用HTMLTestRunner生成报告。除了我想要报告类AddEmployer中的每个方法之外,所有代码都正常工作。我只能打开openURL方法的报告:

suite.addTest(unittest.makeSuite(AddEmployer, 'openURL'))

我希望类AddEmployer的每个方法在同一个驱动程序上同时执行。我会尝试为每个方法调用suite.addTest,但它不起作用。我怎样才能做到这一点?

0 个答案:

没有答案