Skip to content

Commit

Permalink
Set a timeout for all requests calls and warn if any cloud provider c…
Browse files Browse the repository at this point in the history
…alls fail
  • Loading branch information
davehunt committed Dec 17, 2015
1 parent c2deeb6 commit db3f61a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
6 changes: 4 additions & 2 deletions pytest_selenium/cloud/browserstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def start_driver(self, item, capabilities):
def url(self, config, session):
response = requests.get(self._session_url.format(id=session),
auth=(self._username(config),
self._access_key(config)))
self._access_key(config)),
timeout=10)
return response.json()['automation_session']['browser_url']

def additional_html(self, session):
Expand All @@ -51,4 +52,5 @@ def update_status(self, config, session, passed):
requests.put(self._session_url.format(id=session),
headers={'Content-Type': 'application/json'},
params=status,
auth=(self._username(config), self._access_key(config)))
auth=(self._username(config), self._access_key(config)),
timeout=10)
3 changes: 2 additions & 1 deletion pytest_selenium/cloud/saucelabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def update_status(self, config, session, passed):
status = {'passed': passed}
requests.put('http://saucelabs.com/rest/v1/{0}/jobs/{1}'.format(
username, session),
data=json.dumps(status), auth=(username, api_key))
data=json.dumps(status), auth=(username, api_key),
timeout=10)

def _video_html(self, session):
flash_vars = 'config={{\
Expand Down
3 changes: 2 additions & 1 deletion pytest_selenium/cloud/testingbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def additional_html(self, session):
def update_status(self, config, session, passed):
requests.put('https://api.testingbot.com/v1/tests/{0}'.format(session),
data={'test[success]': '1' if passed else '0'},
auth=(self._key(config), self._secret(config)))
auth=(self._key(config), self._secret(config)),
timeout=10)

def _video_html(self, session):
flash_vars = 'config={{\
Expand Down
62 changes: 43 additions & 19 deletions pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from . import cloud

REQUESTS_TIMEOUT = 10
SUPPORTED_DRIVERS = [
'BrowserStack',
'Chrome',
Expand Down Expand Up @@ -51,7 +50,7 @@ def _verify_url(request, base_url):
"""Verifies the base URL"""
verify = request.config.option.verify_base_url
if base_url and verify:
response = requests.get(base_url, timeout=REQUESTS_TIMEOUT)
response = requests.get(base_url, timeout=10)
if not response.status_code == requests.codes.ok:
raise pytest.UsageError(
'Base URL failed verification!'
Expand Down Expand Up @@ -93,7 +92,6 @@ def pytest_configure(config):
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
pytest_html = item.config.pluginmanager.getplugin('html')
report = outcome.get_result()
summary = []
extra = getattr(report, 'extra', [])
Expand All @@ -116,22 +114,10 @@ def pytest_runtest_makereport(item, call):
driver_name = item.config.option.driver
if hasattr(cloud, driver_name.lower()) and driver.session_id:
provider = getattr(cloud, driver_name.lower()).Provider()
# add cloud job identifier to the console output
job_url = provider.url(item.config, driver.session_id)
summary.append('{0} Job: {1}'.format(
provider.name, job_url))
if pytest_html is not None:
# always add cloud job url to the html report
extra.append(pytest_html.extras.url(
job_url, '{0} Job'.format(provider.name)))
if capture_debug:
# conditionally add cloud extras to html report
extra.append(pytest_html.extras.html(
provider.additional_html(driver.session_id)))
xfail = hasattr(report, 'wasxfail')
passed = report.passed or (report.failed and xfail)
# update job status with cloud provider
provider.update_status(item.config, driver.session_id, passed)
_gather_cloud_url(provider, item, report, driver, summary, extra)
if capture_debug:
_gather_cloud_extras(provider, item, report, driver, extra)
_update_cloud_status(provider, item, report, driver, summary)
if summary:
report.sections.append(('pytest-selenium', '\n'.join(summary)))
report.extra = extra
Expand Down Expand Up @@ -194,6 +180,44 @@ def _gather_logs(item, report, driver, summary, extra):
format_log(log), '%s Log' % name.title()))


def _gather_cloud_url(provider, item, report, driver, summary, extra):
try:
url = provider.url(item.config, driver.session_id)
except Exception as e:
summary.append('WARNING: Failed to gather {0} job URL: {1}'.format(
provider.name, e))
return
summary.append('{0} Job: {1}'.format(
provider.name, url))
pytest_html = item.config.pluginmanager.getplugin('html')
if pytest_html is not None:
# always add cloud job url to the html report
extra.append(pytest_html.extras.url(
url, '{0} Job'.format(provider.name)))


def _gather_cloud_extras(provider, item, report, driver, summary, extra):
try:
extras = provider.additional_html(driver.session_id)
except Exception as e:
summary.append('WARNING: Failed to gather {0} extras: {1}'.format(
provider.name, e))
return
pytest_html = item.config.pluginmanager.getplugin('html')
if pytest_html is not None:
extra.append(pytest_html.extras.html(extras))


def _update_cloud_status(provider, item, report, driver, summary):
xfail = hasattr(report, 'wasxfail')
passed = report.passed or (report.failed and xfail)
try:
provider.update_status(item.config, driver.session_id, passed)
except Exception as e:
summary.append('WARNING: Failed to update {0} status: {0}'.format(
provider.name, e))


def format_log(log):
timestamp_format = '%Y-%m-%d %H:%M:%S.%f'
entries = [u'{0} {1[level]} - {1[message]}'.format(
Expand Down
2 changes: 1 addition & 1 deletion pytest_selenium/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sensitive_url(request, base_url):

urls = [base_url]
try:
response = requests.get(base_url)
response = requests.get(base_url, timeout=10)
urls.append(response.url)
urls.extend([history.url for history in response.history])
except requests.exceptions.RequestException:
Expand Down

0 comments on commit db3f61a

Please sign in to comment.