<commit_before>"""
Tests browser instantiation, selection, etc
"""

import tempfile
import shutil
import os
import socket
from mock import patch
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from unittest import TestCase

import bok_choy.browser
from bok_choy.promise import BrokenPromise
from .pages import ButtonPage, JavaScriptPage


class TestBrowser(TestCase):
    """
    Tests browser functionality (starting browser, choosing browser, etc)
    """

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'firefox'})
    def test_local_browser(self):
        browser = bok_choy.browser.browser()
        self.addCleanup(browser.quit)
        self.assertIsInstance(browser, webdriver.Firefox)

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'firefox'})
    def test_firefox_preferences(self):
        browser = bok_choy.browser.browser()
        self.addCleanup(browser.quit)
        # In-spite of the name, 'default_preferences' represents the preferences
        # that are in place on the browser. (The underlying preferences start
        # with default_preferences and are updated in-place.)
        preferences = browser.profile.default_preferences
        self.assertEqual(preferences['browser.startup.homepage'], 'about:blank')
        self.assertEqual(preferences['startup.homepage_welcome_url'], 'about:blank')
        self.assertEqual(preferences['startup.homepage_welcome_url.additional'], 'about:blank')

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'phantomjs'})
    def test_phantom_browser(self):
        browser = bok_choy.browser.browser()
        self.addCleanup(browser.quit)
        self.assertIsInstance(browser, webdriver.PhantomJS)

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'invalid'})
    def test_invalid_browser_name(self):
        with self.assertRaises(bok_choy.browser.BrowserConfigError):
            bok_choy.browser.browser()

    @patch.dict(os.environ, {'SELENIUM_FIREFOX_PATH': '/foo/path'})
    def test_custom_firefox_path(self):
        browser_kwargs_tuple = bok_choy.browser._local_browser_class('firefox')
        self.assertTrue('firefox_binary' in browser_kwargs_tuple[2])

    @patch.dict(os.environ, {'SELENIUM_FIREFOX_PATH': ''})
    def test_no_custom_firefox_path(self):
        browser_kwargs_tuple = bok_choy.browser._local_browser_class('firefox')
        self.assertFalse('firefox_binary' in browser_kwargs_tuple[2])

    def test_profile_error(self):
        """
        If there is a WebDriverException when instantiating the driver,
         it should be tried again.
        """
        patcher = patch('bok_choy.browser._local_browser_class')
        patch_object = patcher.start()
        patch_object.side_effect = WebDriverException(msg='oops', screen=None, stacktrace=None)
        self.addCleanup(patch.stopall)
        with self.assertRaises(BrokenPromise):
            bok_choy.browser.browser()
        self.assertEqual(patch_object.call_count, 3)

    def test_socket_error(self):
        """
        If there is a socket error when instantiating the driver,
         it should be tried again.
        """
        patcher = patch('bok_choy.browser._local_browser_class')
        patch_object = patcher.start()
        patch_object.side_effect = socket.error(61, 'socket error message')
        self.addCleanup(patch.stopall)
        with self.assertRaises(BrokenPromise):
            bok_choy.browser.browser()
        self.assertEqual(patch_object.call_count, 3)


class TestSaveFiles(TestCase):
<commit_msg>Added tests for the new ff profiles env var option<commit_after>"""
Tests browser instantiation, selection, etc
"""

import tempfile
import shutil
import os
import socket
from mock import patch
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from unittest import TestCase

import bok_choy.browser
from bok_choy.promise import BrokenPromise
from .pages import ButtonPage, JavaScriptPage


class TestBrowser(TestCase):
    """
    Tests browser functionality (starting browser, choosing browser, etc)
    """

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'firefox'})
    def test_local_browser(self):
        browser = bok_choy.browser.browser()
        self.addCleanup(browser.quit)
        self.assertIsInstance(browser, webdriver.Firefox)

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'firefox'})
    def test_firefox_preferences(self):
        browser = bok_choy.browser.browser()
        self.addCleanup(browser.quit)
        # In-spite of the name, 'default_preferences' represents the preferences
        # that are in place on the browser. (The underlying preferences start
        # with default_preferences and are updated in-place.)
        preferences = browser.profile.default_preferences
        self.assertEqual(preferences['browser.startup.homepage'], 'about:blank')
        self.assertEqual(preferences['startup.homepage_welcome_url'], 'about:blank')
        self.assertEqual(preferences['startup.homepage_welcome_url.additional'], 'about:blank')

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'phantomjs'})
    def test_phantom_browser(self):
        browser = bok_choy.browser.browser()
        self.addCleanup(browser.quit)
        self.assertIsInstance(browser, webdriver.PhantomJS)

    @patch.dict(os.environ, {'SELENIUM_BROWSER': 'invalid'})
    def test_invalid_browser_name(self):
        with self.assertRaises(bok_choy.browser.BrowserConfigError):
            bok_choy.browser.browser()

    @patch.dict(os.environ, {'SELENIUM_FIREFOX_PATH': '/foo/path'})
    def test_custom_firefox_path(self):
        browser_kwargs_tuple = bok_choy.browser._local_browser_class('firefox')
        self.assertTrue('firefox_binary' in browser_kwargs_tuple[2])

    @patch.dict(os.environ, {'SELENIUM_FIREFOX_PATH': ''})
    def test_no_custom_firefox_path(self):
        browser_kwargs_tuple = bok_choy.browser._local_browser_class('firefox')
        self.assertFalse('firefox_binary' in browser_kwargs_tuple[2])

    def test_profile_error(self):
        """
        If there is a WebDriverException when instantiating the driver,
         it should be tried again.
        """
        patcher = patch('bok_choy.browser._local_browser_class')
        patch_object = patcher.start()
        patch_object.side_effect = WebDriverException(msg='oops', screen=None, stacktrace=None)
        self.addCleanup(patch.stopall)
        with self.assertRaises(BrokenPromise):
            bok_choy.browser.browser()
        self.assertEqual(patch_object.call_count, 3)

    @patch.dict(os.environ, {'FIREFOX_PROFILE_PATH': '/foo/path'})
    def test_custom_firefox_profile(self):
        patch_object = patch.object(webdriver, 'FirefoxProfile').start()
        self.addCleanup(patch.stopall)
        browser_kwargs_tuple = bok_choy.browser._local_browser_class('firefox')
        self.assertTrue('firefox_profile' in browser_kwargs_tuple[2])
        patch_object.assert_called_with('/foo/path')

    @patch.dict(os.environ, {'FIREFOX_PROFILE_PATH': ''})
    def test_no_custom_firefox_profile(self):
        patch_object = patch.object(webdriver, 'FirefoxProfile').start()
        self.addCleanup(patch.stopall)
        browser_kwargs_tuple = bok_choy.browser._local_browser_class('firefox')
        self.assertTrue('firefox_profile' in browser_kwargs_tuple[2])
        patch_object.assert_called_with()

    def test_socket_error(self):
        """
        If there is a socket error when instantiating the driver,
         it should be tried again.
        """
        patcher = patch('bok_choy.browser._local_browser_class')
        patch_object = patcher.start()
        patch_object.side_effect = socket.error(61, 'socket error message')
        self.addCleanup(patch.stopall)
        with self.assertRaises(BrokenPromise):
            bok_choy.browser.browser()
        self.assertEqual(patch_object.call_count, 3)


class TestSaveFiles(TestCase):

{'fraction_non_alphanumeric': 0.10229276895943562, 'fraction_numerical': 0.001763668430335097, 'mean_word_length': 3.7595923261390887, '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': '6921542', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2383, 'n_words': 444}