我正尝试执行一些带有unittest的Selenium脚本,但是我遇到了以下错误。
Starting at: "Sat Dec 07 14:43:17 2013"
E
======================================================================
ERROR: test_template (__main__.ManageTemplates)
----------------------------------------------------------------------
Traceback (most recent call last):
File "template.py", line 70, in tearDown
self.driver.quit()
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 66, in quit
RemoteWebDriver.quit(self)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 454, in quit
self.execute(Command.QUIT)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 162, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 350, in execute
return self._request(url, method=command_info[0], data=data)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 381, in _request
self._conn.request(method, parsed_url.path, data, headers)
File "C:\Program Files (x86)\Python27\lib\httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "C:\Program Files (x86)\Python27\lib\httplib.py", line 1001, in _send_request
self.putrequest(method, url, **skips)
File "C:\Program Files (x86)\Python27\lib\httplib.py", line 871, in putrequest
raise CannotSendRequest()
CannotSendRequest
----------------------------------------------------------------------
Ran 1 test in 766.686s
FAILED (errors=1)
错误发生在template.py文件中tearDown()函数的第70行,这是由Selenium-IDE自动生成的默认unittest函数
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
编辑:
这个问题在我升级到Firefox 26时发生,即使我更新到selenium 2.39.0,问题仍然存在。
来源:
import unittest
from os import path
from config import config
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from PyWebBotClass import PyWebBot
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from os import listdir, environ
from os.path import isfile, join
import time
class ManageReceivers(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
self.base_url = config['baseurl']
self.verificationErrors = []
self.accept_next_alert = True
def setup_bot(self, f):
self.bot = PyWebBot(self.driver, path.join(config['configs'],f))
self.bot.set_LogPath(config['LogPath'])
self.bot.set_ScreenshotPath(config['screenshots'])
self.bot.set_ConfigBaseURL(config['baseurl'])
def test_receiver_profile(self):
self.imported = False
for f in listdir(config['configs']):
if isfile(join(config['configs'],f)):
self.setup_bot(f)
if not self.imported:
self.bot.gotourl('csv2db/import_db_1')
self.imported = True
self.bot.goto('login')
self.bot.JS__fillform('login')
self.bot.goto('receiver_profile')
self.bot.JS__fillform('receiver_profile')
try:
self.bot._driver.execute_script("var e = $('.icon-zoom-in'); e[e.length-1].click()")
except:
print "unable to access selector id = view"
pass
try:
self.bot._driver.execute_script("var e = $('.icon-pencil'); e[e.length-1].click()")
except:
print "unable to access selector id = edit"
pass
self.bot.JS__fillform('receiver_profile')
self.bot.goto('logout')
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert.text
finally: self.accept_next_alert = True
@classmethod
def setUpClass(cls):
environ['NO_PROXY'] = '127.0.0.1' # IP-address of Jenkins server
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == '__main__':
print('Starting at: "%s"' % time.asctime())
unittest.main()
print('Finished at: "%s"' % time.asctime())
django-dash
(https://github.com/barseghyanartur/django-dash/blob/master/src/dash/tests.py)的Selenium测试运行良好,我可以填写表单、提交它们、访问链接,也能考虑JavaScript。我使用Python的`selenium`库(https://pypi.python.org/pypi/selenium/2.37.0)。Firefox版本为26(Ubuntu 12.04 LTS)。 - Artur Barseghyan