我正在使用bok_choy,它使用硒来编写验收测试。该测试在本地计算机上使用chromedriver / geckodriver可以正常运行,但不适用于docker容器中的Selenium Images。出现以下错误:
======================================================================
ERROR: test_result_page (__main__.TestPolls)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/bok_choy/page_object.py", line 329, in visit
self.browser.get(self.url)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=nssFailure2&u=https%3A//demo.app%3A8000/polls/&c=UTF-8&f=regular&d=%20
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app/tests/test_polls.py", line 35, in test_result_page
self.polls_index_page.visit().click_question('question1')
File "/usr/local/lib/python3.8/site-packages/bok_choy/page_object.py", line 332, in visit
raise PageLoadError(u"Could not load page '{!r}' at URL '{}'".format(
bok_choy.page_object.PageLoadError: Message: Could not load page '<pages.PollsIndexPage object at 0x7f19190eed00>' at URL 'http://demo.app:8000/polls/'
======================================================================
ERROR: test_vote_again (__main__.TestPolls)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/bok_choy/page_object.py", line 329, in visit
self.browser.get(self.url)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=nssFailure2&u=https%3A//demo.app%3A8000/polls/&c=UTF-8&f=regular&d=%20
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app/tests/test_polls.py", line 42, in test_vote_again
self.polls_index_page.visit().click_question('question1')
File "/usr/local/lib/python3.8/site-packages/bok_choy/page_object.py", line 332, in visit
raise PageLoadError(u"Could not load page '{!r}' at URL '{}'".format(
bok_choy.page_object.PageLoadError: Message: Could not load page '<pages.PollsIndexPage object at 0x7f1918f24100>' at URL 'http://demo.app:8000/polls/'
我使用python app/tests/test_polls.py
在demo.app
容器中运行测试。
我的dockerfile:
FROM python:latest
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /app
COPY ./app /app
ENV SELENIUM_BROWSER=firefox
ENV SELENIUM_HOST=selenium-hub
ENV SELENIUM_PORT=4444
我的docker-compose.yml文件:
version: "3"
services:
app:
container_name: demo.app
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
- /etc/hosts:/etc/hosts
command: >
sh -c "python app/manage.py runserver 0.0.0.0:8000
&& python test_polls.py"
selenium-hub:
image: selenium/hub:3.141.59-yttrium
container_name: selenium-hub
ports:
- "4444:4444"
chrome:
container_name: demo.chrome
image: selenium/node-chrome:3.141.59-yttrium
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
firefox:
container_name: demo.firefox
image: selenium/node-firefox:3.141.59-yttrium
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
Pages.py:
from bok_choy.page_object import PageObject
class PollsIndexPage(PageObject):
url = 'http://demo.app:8000/polls/'
def is_browser_on_page(self):
return self.q(css='.questions').is_present()
def click_question(self, question):
self.q(css='#{} a'.format(question)).click()
PollsDetailPage(self.browser).wait_for_page()
class PollsDetailPage(PageObject):
url = None
def is_browser_on_page(self):
return self.q(css='input#vote').is_present()
def select_choice(self, choice):
self.q(css='#{}'.format(choice)).click()
def vote(self):
self.q(css='input#vote').click()
PollsResultPage(self.browser).wait_for_page()
def vote_a_question(self, choice):
self.select_choice(choice)
self.vote()
@property
def title(self):
return self.q(css='h1').text[0]
@property
def vote_error(self):
return self.q(css='#error strong').text[0]
class PollsResultPage(PageObject):
url = None
def is_browser_on_page(self):
return self.q(css='#vote_again').is_present()
def vote_again(self):
self.q(css='#vote_again').click()
PollsDetailPage(self.browser).wait_for_page()
test_polls.py:
import unittest
from bok_choy.web_app_test import WebAppTest
from pages import PollsIndexPage, PollsDetailPage, PollsResultPage
class TestPolls(WebAppTest):
def setUp(self):
"""
Instantiate the page objects.
"""
super(TestPolls, self).setUp()
self.polls_index_page = PollsIndexPage(self.browser)
self.polls_detail_page = PollsDetailPage(self.browser)
self.polls_result_page = PollsResultPage(self.browser)
def test_page_existence(self):
"""
Make sure that the page is accessible.
"""
self.polls_index_page.visit()
def test_detail_page(self):
"""
Check if the detail page opens after clicking a question
"""
self.polls_index_page.visit().click_question('question1')
assert "What's up?" == self.polls_detail_page.title
def test_result_page(self):
"""
Check if the result page opens after voting a question
"""
self.polls_index_page.visit().click_question('question1')
self.polls_detail_page.vote_a_question('choice1')
def test_vote_again(self):
"""
Check vote_again option in results page
"""
self.polls_index_page.visit().click_question('question1')
self.polls_detail_page.vote_a_question('choice1')
self.polls_result_page.vote_again()
self.polls_detail_page.vote_a_question('choice2')
def test_invalid_vote(self):
"""
make sure that an error message is shown when no choice is selecteddadad
"""
self.polls_index_page.visit().click_question('question1')
self.polls_detail_page.q(css='input#vote').click()
assert "You didn't select a choice." == self.polls_detail_page.vote_error
if __name__ == '__main__':
unittest.main()