Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
Merge branch 'wharf-urlerror'
Browse files Browse the repository at this point in the history
  • Loading branch information
seandst committed Jan 9, 2015
2 parents caa9405 + aa53ce4 commit b72b940
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
34 changes: 34 additions & 0 deletions fixtures/pytest_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from utils import property_or_none
from utils import conf
from utils.randomness import generate_random_string


class FlexibleTerminalReporter(TerminalReporter):
Expand Down Expand Up @@ -132,3 +133,36 @@ def pytest_configure(config):

def pytest_sessionstart(session):
store.session = session


def write_line(line, **kwargs):
"""A write-line helper that should *always* write a line to the terminal
It knows all of py.tests dirty tricks, including ones that we made, and works around them.
Args:
**kwargs: Normal kwargs for pytest line formatting, stripped from slave messages
"""
if store.slave_manager:
# We're a pytest slave! Write out the vnc info through the slave manager
store.slave_manager.message(line)
else:
# If py.test is supressing stdout/err, turn that off for a moment
if store.capturemanager:
store.capturemanager.suspendcapture()

# terminal reporter knows whether or not to write a newline based on currentfspath
# so stash it, then use rewrite to blow away the line that printed the current
# test name, then clear currentfspath so the test name is reprinted with the
# write_ensure_prefix call. shenanigans!
cfp = store.terminalreporter.currentfspath
# carriage return, write spaces for the whole line, carriage return, write the new line
store.terminalreporter.line('\r' + ' ' * store.terminalreporter._tw.fullwidth + '\r' + line,
**kwargs)
store.terminalreporter.currentfspath = generate_random_string()
store.terminalreporter.write_ensure_prefix(cfp)

# resume capturing
if store.capturemanager:
store.capturemanager.resumecapture()
49 changes: 23 additions & 26 deletions utils/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import threading
import urllib2
from contextlib import contextmanager
from shutil import rmtree
from string import Template
Expand All @@ -14,7 +15,7 @@
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

from fixtures.pytest_store import store
from fixtures.pytest_store import store, write_line
from utils import conf
from utils.log import logger
from utils.path import data_path
Expand Down Expand Up @@ -133,32 +134,28 @@ def start(webdriver_name=None, base_url=None, **kwargs):
view_msg = 'tests can be viewed via vnc on display %s' % wharf_config['vnc_display']
logger.info('webdriver command executor set to %s' % wharf_config['webdriver_url'])
logger.info(view_msg)
write_line(view_msg, cyan=True)

if store.slave_manager:
# We're a pytest slave! Write out the vnc info through the slave manager
store.slave_manager.message(view_msg)
elif store.in_pytest_session:
# if we're running pytest, write out the vnc info through the terminal reporter
if store.capturemanager:
# sneak the msg past the stdout capture if it's enabled
store.capturemanager.suspendcapture()

# terminal reporter knows whether or not to write a newline based on currentfspath
# so stash it, then use rewrite to blow away the line that printed the current
# test name, then clear currentfspath so the test name is reprinted with the
# write_ensure_prefix call. shenanigans!
cfp = store.terminalreporter.currentfspath
store.terminalreporter.line('\r' + view_msg, cyan=True)
store.terminalreporter.currentfspath = None
store.terminalreporter.write_ensure_prefix(cfp)

if store.capturemanager:
store.capturemanager.resumecapture()

browser = webdriver_class(**browser_kwargs)
browser.maximize_window()
browser.get(base_url)
thread_locals.browser = browser
try:
browser = webdriver_class(**browser_kwargs)
browser.maximize_window()
browser.get(base_url)
thread_locals.browser = browser
except urllib2.URLError as ex:
# connection to selenium was refused for unknown reasons
if thread_locals.wharf:
# If we're running wharf, try again with a new container
logger.error('URLError connecting to selenium; recycling container. URLError:')
# Plus, since this is a really weird thing that we need to figure out,
# throw a message out to the terminal for visibility
write_line('URLError caused container recycle, see log for details', red=True)
logger.exception(ex)
thread_locals.wharf.checkin()
thread_locals.wharf = None
start(webdriver_name, base_url, **kwargs)
else:
# If we aren't running wharf, raise it
raise

return thread_locals.browser

Expand Down

0 comments on commit b72b940

Please sign in to comment.