<commit_before>import datetime
import logging
import os
import shutil
import sys
import time

from django.utils.unittest import TestCase
from selenium import webdriver
from testconfig import config

from tests.selenium.utils.constants import wait_time
from tests.selenium.utils.element import find_visible_element_by_css_selector


def quiesce_api(driver, timeout):
    for i in xrange(timeout):
        busy = driver.execute_script('return ($.active != 0);')
        if not busy:
            return
        else:
            time.sleep(1)
    raise RuntimeError('Timeout')


def wait_for_transition(driver, timeout):
    """Wait for a job to complete.  NB the busy icon must be visible initially
    (call quiesce after state change operations to ensure that if the busy icon
    is going to appear, it will have appeared)"""

    for timer in xrange(timeout):
        if find_visible_element_by_css_selector(driver, "img#notification_icon_jobs"):
            time.sleep(1)
        else:
            # We have to quiesce here because the icon is hidden on command completion
            # but updates to changed objects are run asynchronously.
            quiesce_api(driver, timeout)
            return

    raise RuntimeError('Timeout after %s seconds waiting for transition to complete' % timeout)


log = logging.getLogger(__name__)
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)

# The location of the chrome profiles depending on OS
CHROME_USER_DATA_DIR_LOC_LINUX = os.path.join(os.path.expanduser('~'), ".config/")
CHROME_USER_DATA_DIR_LOC_OSX = os.path.join(os.path.expanduser('~'), "Library/Application Support/Google/")

# The Chrome profile to use/create for running selenium tests.
# We don't want to use the Default profile as then devs can't
# have their regular browser open at the same time as running
# selenium tests.
CHROME_TEST_PROFILE = "ChromeChromaTest"


class SeleniumBaseTestCase(TestCase):
    """This is the base class for the test classes.
    The setUp() method is called during the
    initialization process. The tearDown() method is called
    irrespective of the status of the application.
    """
    def __init__(self, *args, **kwargs):
        super(SeleniumBaseTestCase, self).__init__(*args, **kwargs)
        self.log = log

        self.driver = None
        self.standard_wait = wait_time['standard']
        self.medium_wait = wait_time['medium']
        self.long_wait = wait_time['long']

    def setUp(self):
        if not config['chroma_managers'][0]['server_http_url']:
            raise RuntimeError("Please set server_http_url in config file")

        if config['headless']:
            from pyvirtualdisplay import Display
            display = Display(visible = 0, size = (1280, 1024))
            display.start()

        if not self.driver:
            browser = config['chroma_managers'][0]['browser']
            if browser == 'Chrome':
                options = webdriver.ChromeOptions()
                options.add_argument('no-proxy-server')

                options.add_argument('enable-logging=stderr')
                options.add_argument('log-level=""')  # log-level interferes with v=1
                options.add_argument('v=1')

                if os.path.exists(CHROME_USER_DATA_DIR_LOC_LINUX):
                    options.add_argument('user-data-dir=%s' % os.path.join(CHROME_USER_DATA_DIR_LOC_LINUX, CHROME_TEST_PROFILE))
                elif os.path.exists(CHROME_USER_DATA_DIR_LOC_OSX):
                    options.add_argument('user-data-dir=%s' % os.path.join(CHROME_USER_DATA_DIR_LOC_OSX, CHROME_TEST_PROFILE))
                else:
                    raise RuntimeError(
                        "Did not find the expected location for Chrome profiles at either '%s' or '%s'" % (
                            CHROME_USER_DATA_DIR_LOC_LINUX,
                            CHROME_USER_DATA_DIR_LOC_OSX
                        )
                    )
                options.add_argument('incognito')  # To clear state between test runs since sharing a profile now
<commit_msg>TT-1561 - Enable Chrome crash reporting and increase the log verbosity.<commit_after>import datetime
import logging
import os
import shutil
import sys
import time

from django.utils.unittest import TestCase
from selenium import webdriver
from testconfig import config

from tests.selenium.utils.constants import wait_time
from tests.selenium.utils.element import find_visible_element_by_css_selector


def quiesce_api(driver, timeout):
    for i in xrange(timeout):
        busy = driver.execute_script('return ($.active != 0);')
        if not busy:
            return
        else:
            time.sleep(1)
    raise RuntimeError('Timeout')


def wait_for_transition(driver, timeout):
    """Wait for a job to complete.  NB the busy icon must be visible initially
    (call quiesce after state change operations to ensure that if the busy icon
    is going to appear, it will have appeared)"""

    for timer in xrange(timeout):
        if find_visible_element_by_css_selector(driver, "img#notification_icon_jobs"):
            time.sleep(1)
        else:
            # We have to quiesce here because the icon is hidden on command completion
            # but updates to changed objects are run asynchronously.
            quiesce_api(driver, timeout)
            return

    raise RuntimeError('Timeout after %s seconds waiting for transition to complete' % timeout)


log = logging.getLogger(__name__)
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)

# The location of the chrome profiles depending on OS
CHROME_USER_DATA_DIR_LOC_LINUX = os.path.join(os.path.expanduser('~'), ".config/")
CHROME_USER_DATA_DIR_LOC_OSX = os.path.join(os.path.expanduser('~'), "Library/Application Support/Google/")

# The Chrome profile to use/create for running selenium tests.
# We don't want to use the Default profile as then devs can't
# have their regular browser open at the same time as running
# selenium tests.
CHROME_TEST_PROFILE = "ChromeChromaTest"


class SeleniumBaseTestCase(TestCase):
    """This is the base class for the test classes.
    The setUp() method is called during the
    initialization process. The tearDown() method is called
    irrespective of the status of the application.
    """
    def __init__(self, *args, **kwargs):
        super(SeleniumBaseTestCase, self).__init__(*args, **kwargs)
        self.log = log

        self.driver = None
        self.standard_wait = wait_time['standard']
        self.medium_wait = wait_time['medium']
        self.long_wait = wait_time['long']

    def setUp(self):
        if not config['chroma_managers'][0]['server_http_url']:
            raise RuntimeError("Please set server_http_url in config file")

        if config['headless']:
            from pyvirtualdisplay import Display
            display = Display(visible = 0, size = (1280, 1024))
            display.start()

        if not self.driver:
            browser = config['chroma_managers'][0]['browser']
            if browser == 'Chrome':
                options = webdriver.ChromeOptions()
                options.add_argument('no-proxy-server')

                options.add_argument('enable-crash-reporter')
                options.add_argument('full-memory-crash-report')
                options.add_argument('enable-logging=stderr')
                options.add_argument('log-level=""')  # log-level interferes with v=1
                options.add_argument('v=1000')  # Get all levels of vlogs

                if os.path.exists(CHROME_USER_DATA_DIR_LOC_LINUX):
                    options.add_argument('user-data-dir=%s' % os.path.join(CHROME_USER_DATA_DIR_LOC_LINUX, CHROME_TEST_PROFILE))
                elif os.path.exists(CHROME_USER_DATA_DIR_LOC_OSX):
                    options.add_argument('user-data-dir=%s' % os.path.join(CHROME_USER_DATA_DIR_LOC_OSX, CHROME_TEST_PROFILE))
                else:
                    raise RuntimeError(
                        "Did not find the expected location for Chrome profiles at either '%s' or '%s'" % (
                            CHROME_USER_DATA_DIR_LOC_LINUX,
                            CHROME_USER_DATA_DIR_LOC_OSX
                        )
                    )
                options.add_argument('incognito')  # To clear state between test runs since sharing a profile now

{'fraction_non_alphanumeric': 0.08557749850388989, 'fraction_numerical': 0.00466786355475763, 'mean_word_length': 2.6875551632833186, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2872043', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2281, 'n_words': 660}