Skip to content

Instantly share code, notes, and snippets.

@gabrielfalcao
Created May 17, 2011 17:46
Show Gist options
  • Save gabrielfalcao/976959 to your computer and use it in GitHub Desktop.
Save gabrielfalcao/976959 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import urlparse
from lettuce import before, after, world
from splinter.browser import Browser
from django.conf import settings
@before.each_scenario
def setup_browser(scenario):
world.browser = Browser(settings.SELENIUM_DRIVER)
drv = world.browser.driver
world._old_visit = world.browser.visit
port = os.getenv('DJANGO_PROJECT_PORT', '8000')
_base = 'http://localhost:%s/' % port
def navigate(path):
for times in range(len(drv.get_window_handles()) - 1):
# closing all tabs, so that chrome doesn't get crazy with
# many window handlers whenever we use ".visit(url)"
# and yeah, I hate webdriver
drv.close()
world._old_visit(_base + path.lstrip('/'))
# sometimes the django app that lettuce starts in background
# can't support so many requests and nginx gets returning 502
# Bad Gateway
# the work around is to refresh the browser
while world.browser.html is None or '502 Bad Gateway' in world.browser.html:
world.browser.driver.refresh()
drv.switch_to_default_content()
world.browser.visit = navigate
world._old_visit(_base)
world.current_path = lambda: urlparse.urlsplit(world.browser.url).path
@after.each_scenario
def teardown_browser(scenario):
world.browser.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment