在开发环境中本地运行时,导致Travis CI错误失败的Selenium测试。我收到一个错误消息,说Firefox无法在本地主机上建立与服务器的连接。我正在尝试使测试在本地运行并失败,以便我可以调试它而不必使用Travis CI版本。我正在开发一个Python-Flask网络应用程序,该应用程序将docker容器用于该网络应用程序和postgres数据库。
这是测试:
def test_name_capitalization(mockdata, browser):
browser.get("http://127.0.0.1:5000/auth/login")
wait_for_page_load(browser)
test_pairs = [("mcDonald", "McDonald"), ("Mei-Ying", "Mei-Ying"),
("G", "G"), ("oh", "Oh")]
for test_pair in test_pairs:
test_input = test_pair[0]
test_output = test_pair[1]
# enter a name to test
browser.get("http://127.0.0.1:5000/officer/new")
wait_for_element(browser, By.ID, "name")
elem = browser.find_element_by_id("name")
elem.clear()
elem.send_keys(test_input)
browser.find_element_by_id("submit").click()
# get past the "Submit images" page
images_button = browser.find_element_by_class_name("btn btn-primary")
images_button.click()
# check result
wait_for_element(browser, By.TAG_NAME, "tbody")
rendered_field = browser.find_element_by_tag_name("h1").text
rendered_name = rendered_field.split(":")[1].strip()
assert rendered_name == test_output
在Travis CI中失败,出现以下错误:
# get past the "Submit images" page
> images_button = browser.find_element_by_class_name("btn btn-primary")
tests/test_functional.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../../virtualenv/python3.6.3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:563: in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
../../../../virtualenv/python3.6.3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:966: in find_element
'value': value})['value']
../../../../virtualenv/python3.6.3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:320: in execute
self.error_handler.check_response(response)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7ff1c16f0c50>
response = {'status': 404, 'value': '{"error":"no such element","message":"Unable to locate element: .btn btn-primary"}'}
当我在本地运行测试时,输出是不同的。
我运行以下
name=test_name_capitalization make test
并在本地获取这些错误
tests/test_functional.py::test_name_capitalization
[gw0] [100%] FAILED tests/test_functional.py::test_name_capitalization
[gw0] [100%] ERROR tests/test_functional.py::test_name_capitalization
_______________________________________________________________________________ ERROR at teardown of test_name_capitalization ________________________________________________________________________________
[gw0] linux -- Python 3.7.2 /usr/local/bin/python
tp = <class 'selenium.common.exceptions.WebDriverException'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
> raise value
/usr/local/lib/python3.7/site-packages/six.py:693:
`
self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7f4333fb0908>
response = {'status': 500, 'value': '{"value":{"error":"unknown error","message":"Reached error page: about:neterror?e=connection...tate@chrome://marionette/content/listener.js:277:21\\nhandleEvent@chrome://marionette/content/listener.js:245:9\\n"}}'}
`
再往下看
E selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=connectionFailure&u=http%3A//127.0.0.1%3A5000/shutdown&c=UTF-8&f=regular&d=Firefox%20can%E2%80%99t%20establish%20a%20connection%20to%20the%20server%20at%20127.0.0.1%3A5000.
/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py:242: WebDriverException
我读了Selenium WebDriverException: Reached error page这个问题,并遵循将localhost更改为127.0.0.1的建议,但是我没有看到测试行为的任何变化。我也将端口从5000更改为3000,因为我的本地Web应用程序在3000上,但这也没有任何改变。
有人能看到为什么在本地运行时此测试与Travis CI版本中的行为不同吗?谢谢。
答案 0 :(得分:0)
Travis CI 中的错误非常清楚。错误在以下行:
images_button = browser.find_element_by_class_name("btn btn-primary")
find_element_by_class_name(name)
在传递两个类名btn
和btn-primary
的同时,接受一个类名作为参数。
解决方案是使用以下Locator Strategies之一:
css_selector
:
images_button = browser.find_element_by_css_selector(".btn.btn-primary")
xpath
:
images_button = browser.find_element_by_xpath("//*[@class='btn btn-primary']")
localhost 中的错误有点主观,如下所示:
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=connectionFailure&u=http%3A//127.0.0.1%3A5000/shutdown&c=UTF-8&f=regular&d=Firefox%20can%E2%80%99t%20establish%20a%20connection%20to%20the%20server%20at%20127.0.0.1%3A5000.
表示在初始化 WebDriver / Web浏览会话时由于网络错误而导致的500 Internal Server Error。您可以在Reached error page: about:neterror when trying to navigate to other tabs if there is a form submit under that tab
中找到详细的讨论