<commit_before>"""
Browser set up for acceptance tests.
"""

#pylint: disable=E1101
#pylint: disable=W0613
#pylint: disable=W0611

from lettuce import before, after, world
from splinter.browser import Browser
from logging import getLogger
from django.core.management import call_command
from django.conf import settings
from selenium.common.exceptions import WebDriverException

# Let the LMS and CMS do their one-time setup
# For example, setting up mongo caches
from lms import one_time_startup
from cms import one_time_startup

# There is an import issue when using django-staticfiles with lettuce
# Lettuce assumes that we are using django.contrib.staticfiles,
# but the rest of the app assumes we are using django-staticfiles
# (in particular, django-pipeline and our mako implementation)
# To resolve this, we check whether staticfiles is installed,
# then redirect imports for django.contrib.staticfiles
# to use staticfiles.
try:
    import staticfiles
except ImportError:
    pass
else:
    import sys
    sys.modules['django.contrib.staticfiles'] = staticfiles

LOGGER = getLogger(__name__)
LOGGER.info("Loading the lettuce acceptance testing terrain file...")

MAX_VALID_BROWSER_ATTEMPTS = 20


@before.harvest
def initial_setup(server):
    """
    Launch the browser once before executing the tests.
    """
    browser_driver = getattr(settings, 'LETTUCE_BROWSER', 'chrome')

    # There is an issue with ChromeDriver2 r195627 on Ubuntu
    # in which we sometimes get an invalid browser session.
    # This is a work-around to ensure that we get a valid session.
    success = False
    num_attempts = 0
    while (not success) and num_attempts < MAX_VALID_BROWSER_ATTEMPTS:

        # Get a browser session
        world.browser = Browser(browser_driver)

        # Try to visit the main page
        # If the browser session is invalid, this will
        # raise a WebDriverException
        try:
            world.visit('/')

        except WebDriverException:
            world.browser.quit()
            num_attempts += 1

        else:
            success = True

    # If we were unable to get a valid session within the limit of attempts,
    # then we cannot run the tests.
    if not success:
        raise IOError("Could not acquire valid ChromeDriver browser session.")

    # Set the browser size to 1280x1024
    world.browser.driver.set_window_size(1280, 1024)


@before.each_scenario
def reset_data(scenario):
    """
    Clean out the django test database defined in the
    envs/acceptance.py file: mitx_all/db/test_mitx.db
    """
    LOGGER.debug("Flushing the test database...")
    call_command('flush', interactive=False)


@after.each_scenario
def screenshot_on_error(scenario):
    """
    Save a screenshot to help with debugging.
    """
    if scenario.failed:
        world.browser.driver.save_screenshot('/tmp/last_failed_scenario.png')


@after.all
def teardown_browser(total):
    """
    Quit the browser after executing the tests.
    """
    world.browser.quit()
<commit_msg>Turn off automatic snapshot to tmp dir on lettuce failure<commit_after>"""
Browser set up for acceptance tests.
"""

#pylint: disable=E1101
#pylint: disable=W0613
#pylint: disable=W0611

from lettuce import before, after, world
from splinter.browser import Browser
from logging import getLogger
from django.core.management import call_command
from django.conf import settings
from selenium.common.exceptions import WebDriverException

# Let the LMS and CMS do their one-time setup
# For example, setting up mongo caches
from lms import one_time_startup
from cms import one_time_startup

# There is an import issue when using django-staticfiles with lettuce
# Lettuce assumes that we are using django.contrib.staticfiles,
# but the rest of the app assumes we are using django-staticfiles
# (in particular, django-pipeline and our mako implementation)
# To resolve this, we check whether staticfiles is installed,
# then redirect imports for django.contrib.staticfiles
# to use staticfiles.
try:
    import staticfiles
except ImportError:
    pass
else:
    import sys
    sys.modules['django.contrib.staticfiles'] = staticfiles

LOGGER = getLogger(__name__)
LOGGER.info("Loading the lettuce acceptance testing terrain file...")

MAX_VALID_BROWSER_ATTEMPTS = 20


@before.harvest
def initial_setup(server):
    """
    Launch the browser once before executing the tests.
    """
    browser_driver = getattr(settings, 'LETTUCE_BROWSER', 'chrome')

    # There is an issue with ChromeDriver2 r195627 on Ubuntu
    # in which we sometimes get an invalid browser session.
    # This is a work-around to ensure that we get a valid session.
    success = False
    num_attempts = 0
    while (not success) and num_attempts < MAX_VALID_BROWSER_ATTEMPTS:

        # Get a browser session
        world.browser = Browser(browser_driver)

        # Try to visit the main page
        # If the browser session is invalid, this will
        # raise a WebDriverException
        try:
            world.visit('/')

        except WebDriverException:
            world.browser.quit()
            num_attempts += 1

        else:
            success = True

    # If we were unable to get a valid session within the limit of attempts,
    # then we cannot run the tests.
    if not success:
        raise IOError("Could not acquire valid ChromeDriver browser session.")

    # Set the browser size to 1280x1024
    world.browser.driver.set_window_size(1280, 1024)


@before.each_scenario
def reset_data(scenario):
    """
    Clean out the django test database defined in the
    envs/acceptance.py file: mitx_all/db/test_mitx.db
    """
    LOGGER.debug("Flushing the test database...")
    call_command('flush', interactive=False)

# Uncomment below to trigger a screenshot on error
# @after.each_scenario
def screenshot_on_error(scenario):
    """
    Save a screenshot to help with debugging.
    """
    if scenario.failed:
        world.browser.driver.save_screenshot('/tmp/last_failed_scenario.png')


@after.all
def teardown_browser(total):
    """
    Quit the browser after executing the tests.
    """
    world.browser.quit()

{'fraction_non_alphanumeric': 0.07224210868857793, 'fraction_numerical': 0.01269118125610153, 'mean_word_length': 3.4064516129032256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7041433', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1701, 'n_words': 635}