1

A Selenium test that fails in Travis CI errors out when run locally in the development environment. I’m getting an error that says Firefox can’t establish a connection to the server at localhost. I’m trying to get the test to run and fail locally so that I can debug it without having to push to the Travis CI build. I’m working on a Python-Flask web app that uses docker containers for the web app and the postgres database.

Here’s the test:

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

It fails in Travis CI with the following error:

            # 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"}'}

Output is different when I run the test locally. I run the below name=test_name_capitalization make test and get these errors locally

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"}}'}
`

Further down I also see

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

I read this question Selenium WebDriverException: Reached error page and followed the advice of changing localhost to 127.0.0.1, but I didn’t see any change in the test behavior. I also changed the port from 5000 to 3000, because I have the web app locally on 3000, but that also didn’t change anything.

Can anyone see why this test behaves differently when run locally than it does in the Travis CI build? Thanks.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
ByteByByte
  • 303
  • 1
  • 2
  • 11

1 Answers1

1

The error from Travis CI was clear and concise. The error is at the line:

images_button = browser.find_element_by_class_name("btn btn-primary")

find_element_by_class_name(name) accepts a single class name as an argument whilest you have passed two class names btn and btn-primary.

The solution would be to use either of the following 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']")
    

The error from your localhost is a bit subjective as it reads:

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.

which indicates a 500 Internal Server Error due to a Network Error while initializing the WebDriver / Web Browsing session. You can find a detailed discussion in Reached error page: about:neterror when trying to navigate to other tabs if there is a form submit under that tab

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi, it seems that I need to clarify what I was asking. I am trying to figure out why the test threw an error when it ran locally. I was not asking about the failure in Travis CI, as the cause of that is clearer to me. – ByteByByte Feb 28 '19 at 15:36
  • @ByteByByte Probably I have covered both the Jenkins and localhost failure case in my answer. – undetected Selenium Feb 28 '19 at 19:26