<commit_before># pylint: disable=missing-docstring
import os
import sys
import logging
import warnings
import percy

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import (
    WebDriverException,
    TimeoutException,
    MoveTargetOutOfBoundsException,
)

from dash.testing.wait import (
    text_to_equal,
    style_to_equal,
    contains_text,
    until,
)
from dash.testing.dash_page import DashPageMixin
from dash.testing.errors import DashAppLoadingError, BrowserError, TestingTimeoutError
from dash.testing.consts import SELENIUM_GRID_DEFAULT


logger = logging.getLogger(__name__)


class Browser(DashPageMixin):
    def __init__(
        self,
        browser,
        remote=False,
        remote_url=None,
        headless=False,
        options=None,
        download_path="",
        percy_finalize=True,
        percy_assets_root="",
        wait_timeout=10,
    ):
        self._browser = browser.lower()
        self._remote_url = remote_url
        self._remote = (
            True
            if remote_url and remote_url != SELENIUM_GRID_DEFAULT
            else remote
        )
        self._headless = headless
        self._options = options
        self._download_path = download_path
        self._wait_timeout = wait_timeout
        self._percy_finalize = percy_finalize

        self._driver = until(self.get_webdriver, timeout=1)
        self._driver.implicitly_wait(2)

        self._wd_wait = WebDriverWait(self.driver, wait_timeout)
        self._last_ts = 0
        self._url = None

        self._window_idx = 0  # switch browser tabs

        self.percy_runner = percy.Runner(
            loader=percy.ResourceLoader(
                webdriver=self.driver,
                base_url="/assets",
                root_dir=percy_assets_root,
            )
        )
        self.percy_runner.initialize_build()

        logger.info("initialize browser with arguments")
        logger.info("  headless => %s", self._headless)
        logger.info("  download_path => %s", self._download_path)
        logger.info(
            "  percy asset root => %s", os.path.abspath(percy_assets_root)
        )

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, traceback):
        try:
            self.driver.quit()
            if self._percy_finalize:
                logger.info("percy runner finalize build now")
                self.percy_runner.finalize_build()
            else:
                logger.info("percy finalize relies on CI job")
        except WebDriverException:
            logger.exception("webdriver quit was not successful")
        except percy.errors.Error:
            logger.exception("percy runner failed to finalize properly")

    def percy_snapshot(self, name="", wait_for_callbacks=False):
        """percy_snapshot - visual test api shortcut to `percy_runner.snapshot`.
        It also combines the snapshot `name` with the python version.
        """
        snapshot_name = "{} - py{}.{}".format(
            name, sys.version_info.major, sys.version_info.minor
        )
        logger.info("taking snapshot name => %s", snapshot_name)
        try:
            if wait_for_callbacks:
                until(self._wait_for_callbacks, timeout=10, poll=0.5)
        except TestingTimeoutError:
            for _ in range(2):
                self.driver.refresh()
                # for snapshot intensive case, it might happens that
                # the selenium get stuck, give it one more chance after refresh
                until(self._wait_for_callbacks, timeout=20, poll=1)

        self.percy_runner.snapshot(name=snapshot_name)

    def take_snapshot(self, name):
        """Hook method to take snapshot when a selenium test fails. The
        snapshot is placed under.

            - `/tmp/dash_artifacts` in linux
            - `%TEMP` in windows
        with a filename combining test case name and the
        running selenium session id
        """
        target = (
            "/tmp/dash_artifacts"
            if not self._is_windows()
            else os.getenv("TEMP")
        )
        if not os.path.exists(target):
            try:
                os.mkdir(target)
            except OSError:<commit_msg>extend timeout<commit_after># pylint: disable=missing-docstring
import os
import sys
import logging
import warnings
import percy

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import (
    WebDriverException,
    TimeoutException,
    MoveTargetOutOfBoundsException,
)

from dash.testing.wait import (
    text_to_equal,
    style_to_equal,
    contains_text,
    until,
)
from dash.testing.dash_page import DashPageMixin
from dash.testing.errors import DashAppLoadingError, BrowserError, TestingTimeoutError
from dash.testing.consts import SELENIUM_GRID_DEFAULT


logger = logging.getLogger(__name__)


class Browser(DashPageMixin):
    def __init__(
        self,
        browser,
        remote=False,
        remote_url=None,
        headless=False,
        options=None,
        download_path="",
        percy_finalize=True,
        percy_assets_root="",
        wait_timeout=10,
    ):
        self._browser = browser.lower()
        self._remote_url = remote_url
        self._remote = (
            True
            if remote_url and remote_url != SELENIUM_GRID_DEFAULT
            else remote
        )
        self._headless = headless
        self._options = options
        self._download_path = download_path
        self._wait_timeout = wait_timeout
        self._percy_finalize = percy_finalize

        self._driver = until(self.get_webdriver, timeout=1)
        self._driver.implicitly_wait(2)

        self._wd_wait = WebDriverWait(self.driver, wait_timeout)
        self._last_ts = 0
        self._url = None

        self._window_idx = 0  # switch browser tabs

        self.percy_runner = percy.Runner(
            loader=percy.ResourceLoader(
                webdriver=self.driver,
                base_url="/assets",
                root_dir=percy_assets_root,
            )
        )
        self.percy_runner.initialize_build()

        logger.info("initialize browser with arguments")
        logger.info("  headless => %s", self._headless)
        logger.info("  download_path => %s", self._download_path)
        logger.info(
            "  percy asset root => %s", os.path.abspath(percy_assets_root)
        )

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, traceback):
        try:
            self.driver.quit()
            if self._percy_finalize:
                logger.info("percy runner finalize build now")
                self.percy_runner.finalize_build()
            else:
                logger.info("percy finalize relies on CI job")
        except WebDriverException:
            logger.exception("webdriver quit was not successful")
        except percy.errors.Error:
            logger.exception("percy runner failed to finalize properly")

    def percy_snapshot(self, name="", wait_for_callbacks=False):
        """percy_snapshot - visual test api shortcut to `percy_runner.snapshot`.
        It also combines the snapshot `name` with the python version.
        """
        snapshot_name = "{} - py{}.{}".format(
            name, sys.version_info.major, sys.version_info.minor
        )
        logger.info("taking snapshot name => %s", snapshot_name)
        try:
            if wait_for_callbacks:
                until(self._wait_for_callbacks, timeout=10, poll=0.5)
        except TestingTimeoutError:
            for _ in range(2):
                self.driver.refresh()
                # for snapshot intensive case, it might happens that
                # the selenium get stuck, give it one more chance after refresh
                until(self._wait_for_callbacks, timeout=50, poll=1)

        self.percy_runner.snapshot(name=snapshot_name)

    def take_snapshot(self, name):
        """Hook method to take snapshot when a selenium test fails. The
        snapshot is placed under.

            - `/tmp/dash_artifacts` in linux
            - `%TEMP` in windows
        with a filename combining test case name and the
        running selenium session id
        """
        target = (
            "/tmp/dash_artifacts"
            if not self._is_windows()
            else os.getenv("TEMP")
        )
        if not os.path.exists(target):
            try:
                os.mkdir(target)
            except OSError:
{'fraction_non_alphanumeric': 0.07925966249319542, 'fraction_numerical': 0.0030484485574305935, 'mean_word_length': 2.2574468085106383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '625373', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2497, 'n_words': 616}