20

I am using a very complex setup to test various non-public webpages. I use jenkins to run the python-selenium tests within a dockerimage. That way, I am completly independent of the jenkins environment and can create my own environment. In this environment I have the following software installed:

  • Firefox: 57.0.1
  • geckodriver: 0.18.0
  • nosetests: 1.3.7
  • selenium: 3.8.0

The selenium tests create the WebDriver the following way:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir", self.downloadpath)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.useDownloadDir", True)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
"application/force-download, image/png, text/html, text/plain, "
"image/tiff, text/csv, application/zip, application/octet-stream")
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.closeWhenDone", True)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting",
False)
self.driver = webdriver.Firefox(profile, log_path = logfile)

where logfile and self.downloadpath are two valid paths in the docker setup.

The whole test suite consists of 6 independant test cases, each with the same setup as above. They normally run fine and complete without problems.

But without any change to the tests or the general setup, a test sometimes fails with the following error message:

  File "/root/tests/bsp_usecase_tests/tools/basicsuite.py", line 210, in set_driver_firefox
    self.driver = webdriver.Firefox(profile, log_path = logfile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__
    keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: connection refused

I have two questions:

  1. What connection is refused here? What is the meaning of the error message
  2. How can I possibly fix this error?

Addendum:

  • When I used a time.sleep(10) just before the webdriver.Firefox line, the error did not show up anymore. Shoud I put that line in a while-try-except loop?
Alex
  • 34,021
  • 64
  • 178
  • 371
  • do you use a virtual display for running the selenium tests? Afaik jenkins runs in headless environment, so you need `Xvfb` to instantiate a firefox driver. Newer versions of firefox also can work in a headless environment - to use it, you have to pass the flag in options: `opts = selenium.webdriver.firefox.options.Options(); opts.add_argument('-headless');driver = webdriver.Firefox(firefox_profile=profile, firefox_options=opts)`. – hoefling Jan 03 '18 at 21:07
  • This error/stack-trace indicates that the driver failed to start its internal server to communicate with the python client. It could be a race condition on the generated port number or maybe the docker image has a lack of memory which is not recycle on time when the next test starts. In any case your issue requires a reproductible example and an analysis of the logs from the OS, browser and driver which are not present in your post. – Florent B. Jan 04 '18 at 19:57

4 Answers4

7

The error you are seeing is :

WebDriverException: Message: connection refused

As per the documentation WebDriverException is the Base webdriver exception which is as follows :

exception selenium.common.exceptions.WebDriverException(msg=None, screen=None, stacktrace=None)

So connection is refused here means that Selenium is unable to establish the connecting which you wanted to establish through :

self.driver = webdriver.Firefox(profile, log_path = logfile)

A possible solution would be to provide the complete name of the logfile along with the logical location of the logfile (from Project Level) as follows :

self.driver = webdriver.Firefox(firefox_profile=profile, log_path='./Log/geckodriver.log')

Here you can find a similar Discussion

Again, as you mentioned When I used a time.sleep(10) just before the webdriver.Firefox line, the error did not show up anymore, so I assume there was an instance of GeckoDriver and Firefox Browser client active previously. Hence, similarly as @Florent B. mentioned you have to shield your script against facing Race Around Condition which can stem out from either of the following :

  • Accessing the same logfile by the new session which previous session have't released yet.
  • Accessing the same port number by GeckoDriver or Marionette by the new session which previous session have't released yet.
  • Lack of access to CPU
  • Lack of Physical Memory
  • Lack of Swap Memory
  • Lack of Disc Cache
  • Lack of Network Bandwidth
  • Docker Image ran out of memory

Here you can find a similar Discussion.

As per the above mentioned causes, you need to follow a few steps as follows :

  • Always use the latest released version of Selenium-Python client, WebDriver variant (GeckoDriver) and Web Browser (Firefox Browser)
  • Always use quit() in the tearDown() method so that the webdriver and the webclient both are properly destroyed.
  • Clean the Project Workspace from your IDE before and after executing your Test Suite.
  • Clear the Browser Cache before and after the execution of your Tests
  • Use CCleaner tool regularly to wipe away the OS chores including the stale rust_mozprofile directories.
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    I can change that. Your answer does not epxplain, however, what a `WebDriverException` is, nor does your answer discuss possible fitting reasons why I get this error just only occassional... – Alex Dec 21 '17 at 10:31
5

What connection is refused here? What is the meaning of the error message

The connection between the Python webdriver API and your Firefox web browser. Well, not the connection itself, but a single request that the webdriver sent was "rejected" by the browser. Note that this works through the JSON Wire protocol - JSON over HTTP.

How can I possibly fix this error?

Usually, in case of error like this, the most common reason is a compatibility issue. In other words, I suspect that your geckodriver version 0.18.0 is too old for the Firefox 57. Upgrade geckodriver to the latest stable version (currently 0.19.1).

alecxe
  • 414,977
  • 106
  • 935
  • 1,083
3

A quick fix to try for anyone else out there struggling with this or similar errors -- I found that deleting my geckodriver.log file got rid of this error.

This is implied in the "Clean the Project Workspace" part of DebanjanB's answer, but I just wanted to share the concrete action that fixed it for me. Note that I was not using a test suite like the original poster.

I imagine what happened for me was that I had previous webdriver.Firefox instances that never got to driver.close() in my code due to an error in another part of my code, which I was still debugging, and they had not released the log file geckodriver.log.

Thus I imagine this issue might also be solved by renaming the log file or writing to a different log file.

grooveplex
  • 2,131
  • 4
  • 24
  • 28
Alta Fang
  • 41
  • 2
0

I had the same problem, and found that it was a permissions problem. I am running Selenium within apache, and the apache folder did not have the correct permissions. Either selenium, geckodriver, or firefox tries creating files and folders in the /var/www/ directory, and it does not have permissions to do so.

You can fix this with the following command:

chmod a+rwx /var/www/

The above command will work, but is potentially insecure as it gives everyone access to the folder. If you want to make it a bit more secure, try:

chown www-data /var/www/

This are both quick hacks, the better way would be to figure out why geckodriver is creating files in the folder, and change that folder setting (I haven't done this).

speedplane
  • 14,130
  • 14
  • 78
  • 128