Python unittest + PhantomJS

时间:2014-10-14 11:36:56

标签: python

基于python + PhantoJS的测试我遇到了一些问题。我有这样的测试(里面大约有90个测试)。

import unittest
import os
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

class ceskyflorbal_error_message(unittest.TestCase):
# Test suit for search error messages by css selectors 

def setUp(self):
    phantomjs_path = "C:\Phantom\phantomjs.exe"
    self.driver = webdriver.PhantomJS(executable_path=phantomjs_path, service_log_path=os.path.devnull)
    self.driver.set_window_size(1400, 1000)
    self.driver.implicitly_wait(1)

def test_error_msg_home(self):
    driver = self.driver
    driver.get('https://xxxxxxx')

    with self.assertRaises(NoSuchElementException):
         driver.find_element_by_css_selector(".no-params-msg")

    with self.assertRaises(NoSuchElementException):
         driver.find_element_by_css_selector(".error-render-component")

def test_error_msg_muzi_2(self):
    driver = self.driver
    driver.get('https://xxxxxx')

    with self.assertRaises(NoSuchElementException):
         driver.find_element_by_css_selector(".no-params-msg")

    with self.assertRaises(NoSuchElementException):
         driver.find_element_by_css_selector(".error-render-component")

def test_error_msg_muzi_3(self):
    driver = self.driver
    driver.get('https://xxxxxxx')

    with self.assertRaises(NoSuchElementException):
         driver.find_element_by_css_selector(".no-params-msg")

    with self.assertRaises(NoSuchElementException):
         driver.find_element_by_css_selector(".error-render-component")

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

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

我需要所有测试都在一个浏览器会话中执行。现在为每个测试创建一个新的浏览器会话,它会占用系统性能和时间。你能举个例子怎么做吗?我正在寻找,但没有任何对我有效。

1 个答案:

答案 0 :(得分:1)

使用setUpClass而不是setUp来设置会话。