Skip to content

make_screenshot_on_failure: handle "URLError: Connection refused" #16

@blueyed

Description

@blueyed

When quitting the browser yourself in the (overridden) browser fixture,
make_screenshot_on_failure causes a "Connection refused" exception.

While this indicates a problem with your test setup, I think it should only
log a warning instead, or something similar.

_______ ERROR at teardown of test_submitter_history ______

    def make_screenshot_on_failure():
        if splinter_make_screenshot_on_failure and request.node.splinter_failure:
            slaveoutput = getattr(request.config, 'slaveoutput', None)
            names = junitxml.mangle_testnames(request.node.nodeid.split("::"))
            classname = '.'.join(names[:-1])
            screenshot_dir = os.path.join(splinter_screenshot_dir, classname)
            screenshot_file_name = '{0}-{1}.png'.format(
                names[-1][:128 - len(parent.__name__) - 5], parent.__name__)
            if not slaveoutput:
                if not os.path.exists(screenshot_dir):
                    os.makedirs(screenshot_dir)
            else:
                screenshot_dir = tmpdir.mkdir('screenshots').strpath
            screenshot_path = os.path.join(screenshot_dir, screenshot_file_name)
>           browser.driver.save_screenshot(screenshot_path)

venv/lib/python2.7/site-packages/pytest_splinter/plugin.py:308:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py:696: in get_screenshot_as_file
    png = self.get_screenshot_as_png()
venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py:715: in get_screenshot_as_png
    return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))
venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py:725: in get_screenshot_as_base64
    return self.execute(Command.SCREENSHOT)['value']
venv/lib/python2.7/site-packages/pytest_splinter/webdriver_patches.py:76: in execute
    result = self._base_execute(driver_command, params)
venv/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py:171: in execute
    response = self.command_executor.execute(driver_command, params)
venv/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py:349: in execute
    return self._request(command_info[0], url, body=data)
venv/lib/python2.7/site-packages/pytest_splinter/webdriver_patches.py:61: in _request
    return old_request(*args, **kwargs)
venv/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py:417: in _request
    resp = opener.open(request)
..…/pyenv/2.7.9/lib/python2.7/urllib2.py:431: in open
    response = self._open(req, data)
..…/pyenv/2.7.9/lib/python2.7/urllib2.py:449: in _open
    '_open', req)
..…/pyenv/2.7.9/lib/python2.7/urllib2.py:409: in _call_chain
    result = func(*args)
..…/pyenv/2.7.9/lib/python2.7/urllib2.py:1227: in http_open
    return self.do_open(httplib.HTTPConnection, req)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <urllib2.HTTPHandler instance at 0x7f86aac10c20>, http_class = <class httplib.HTTPConnection at 0x7f86b2577a10>
req = <selenium.webdriver.remote.remote_connection.Request instance at 0x7f86aad84e60>, http_conn_args = {}, host = '127.0.0.1:52595'
h = <httplib.HTTPConnection instance at 0x7f86aaca5248>, err = error(111, 'Connection refused')

    def do_open(self, http_class, req, **http_conn_args):
        """Return an addinfourl object for the request, using http_class.

            http_class must implement the HTTPConnection API from httplib.
            The addinfourl return value is a file-like object.  It also
            has methods and attributes including:
                - info(): return a mimetools.Message object for the headers
                - geturl(): return the original request URL
                - code: HTTP status code
            """
        host = req.get_host()
        if not host:
            raise URLError('no host given')

        # will parse host:port
        h = http_class(host, timeout=req.timeout, **http_conn_args)
        h.set_debuglevel(self._debuglevel)

        headers = dict(req.unredirected_hdrs)
        headers.update(dict((k, v) for k, v in req.headers.items()
                            if k not in headers))

        # We want to make an HTTP/1.1 request, but the addinfourl
        # class isn't prepared to deal with a persistent connection.
        # It will try to read all remaining data from the socket,
        # which will block while the server waits for the next request.
        # So make sure the connection gets closed after the (only)
        # request.
        headers["Connection"] = "close"
        headers = dict(
            (name.title(), val) for name, val in headers.items())

        if req._tunnel_host:
            tunnel_headers = {}
            proxy_auth_hdr = "Proxy-Authorization"
            if proxy_auth_hdr in headers:
                tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr]
                # Proxy-Authorization should not be sent to origin
                # server.
                del headers[proxy_auth_hdr]
            h.set_tunnel(req._tunnel_host, headers=tunnel_headers)

        try:
            h.request(req.get_method(), req.get_selector(), req.data, headers)
        except socket.error, err: # XXX what error?
            h.close()
>           raise URLError(err)
E           URLError: <urlopen error [Errno 111] Connection refused>

..…/pyenv/2.7.9/lib/python2.7/urllib2.py:1197: URLError
-

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions