【发布时间】:2017-12-26 12:16:50
【问题描述】:
我正在使用 selenium webdriver 创建一个用 python 编写的测试套件。但是,当我运行测试时,我收到以下错误消息:'PythonOrgSearch' object has no attribute 'driver'
我很确定这是因为测试没有按顺序运行,所以驱动程序在测试完成之前就关闭了。我以前也收到错误:“尝试在未建立连接的情况下运行命令”,我认为这也表明测试没有按顺序运行,所以驱动程序没有启动?我不确定这是否准确,只是我的最佳猜测。我的代码如下所示:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')
class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
self.driver.start()
def test_opens(self):
driver.get("url.com")
driver.find_element_by_id('username').send_keys('user')
driver.find_element_by_id('password').send_keys('pass')
driver.find_elements_by_css_selector("button[type='submit']")[0].click()
time.sleep(2);
self.assertIn("title", driver.title)
def ztearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
编辑:我在每个函数的开头添加了 driver=self.driver
【问题讨论】:
-
你不想在课堂上到处使用 self.driver 吗?
-
如果您以某种方式并行运行测试,它们可能会出现乱序。你?此外,驱动程序在
setUp在每个测试之前启动;无论它们以什么顺序运行,当驱动程序未启动时,不应运行任何测试。由于多个测试并行运行,打开/关闭可能会干扰,因为您可能只运行一个浏览器。
标签: python unit-testing selenium selenium-webdriver python-unittest