执行过程中没有报错,也生成了HTML文件,但文件里没有内容。
代码如下:
TestCase 文件,login.py
__author__ = 'Amy Liu' from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium import webdriver from selenium.common.exceptions import NoSuchElementException import unittest import time
class login(unittest.TestCase): def setUp(self): self.verificationErrors = [] self.accept_next_alert = True self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.verificationErrors = [] self.accept_next_alert = True self.base_url = 'http://www.haosou.com' def test_signin(self): driver = self.driver try: driver.get(self.base_url + '/') time.sleep(2) driver.find_element_by_id("input").send_keys("su") driver.find_element_by_id("search-button").click() result = driver.find_element_by_id("keyword").get_attribute('value') if result == "su": print("Test Passed! Test Result is: " + result) flag = True except Exception as e: falg = False print("Test Failed") print(e) driver.get_screenshot_as_file("D:\\Program Files\\NM.bmp") def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors)
执行用例文件,run_case.py import HTMLTestRunner import sys import time
# should import this if use method (1)
# from Demo import Login import unittest from Demo1.Login import login
# Get current time
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) # Define the report path
filename = "d:\\result " + now + '.html' result_file = open(filename, "wb") runner = HTMLTestRunner.HTMLTestRunner( stream=result_file,
title="Test Result",
description="Test cases execute result") if __name__ == "__main__": suite = unittest.TestSuite() # (1) add all test cases from Login.py
# suite.addTest(unittest.makeSuite(Login.login))
# (2) add test case one by one
suite.addTest(login("test_signin")) runner = unittest.TextTestRunner() runner.run(suite) result_file.close() time.sleep(6)
log 如下: C:\Python34\python.exe I:/QA/Python/workspace/Demo1/run_case.py
Test Passed! Test Result is: su
.
----------------------------------------------------------------------
Ran 1 test in 6.632s