<commit_before>from selenium.webdriver import Ie, Firefox, Chrome, Opera, Safari, PhantomJS
from selenium.common.exceptions import NoAlertPresentException

from .context import Context
from .exceptions import UnsupportedWebDriverTypeException
from .waits.waiter import wait_for
from .config import DEFAULT, web_driver_default_page_load_timeout, web_driver_default_script_timeout, web_driver_default_wait_interval, \
    web_driver_default_wait_timeout
from .decorator import SupportedBy

__author__ = 'karl.gong'


class WebDriverType:
    IE = "ie"
    FIREFOX = "firefox"
    CHROME = "chrome"
    OPERA = "opera"
    SAFARI = "safari"
    PHANTOMJS = "phantomjs"
    ANDROID = "android"
    IOS = "ios"

    _BROWSER = [IE, FIREFOX, CHROME, OPERA, SAFARI, PHANTOMJS]
    _MOBILE = [ANDROID, IOS]
    _ALL = _BROWSER + _MOBILE


class WebDriver(Context):
    def __init__(self, web_driver_type=WebDriverType.CHROME, page_load_timeout=DEFAULT, script_timeout=DEFAULT,
                 wait_interval=DEFAULT, wait_timeout=DEFAULT, **kwargs):
        Context.__init__(self)
        self.__web_driver_type = web_driver_type.lower()
        if self.__web_driver_type == WebDriverType.IE:
            self.__selenium_web_driver = Ie(**kwargs)
        elif self.__web_driver_type == WebDriverType.FIREFOX:
            self.__selenium_web_driver = Firefox(**kwargs)
        elif self.__web_driver_type == WebDriverType.CHROME:
            self.__selenium_web_driver = Chrome(**kwargs)
        elif self.__web_driver_type == WebDriverType.OPERA:
            self.__selenium_web_driver = Opera(**kwargs)
        elif self.__web_driver_type == WebDriverType.SAFARI:
            self.__selenium_web_driver = Safari(**kwargs)
        elif self.__web_driver_type == WebDriverType.PHANTOMJS:
            self.__selenium_web_driver = PhantomJS(**kwargs)
        elif self.__web_driver_type in WebDriverType._MOBILE:
            from appium.webdriver.webdriver import WebDriver as Mobile
            self.__selenium_web_driver = Mobile(**kwargs)
        else:
            raise UnsupportedWebDriverTypeException("The web driver type [%s] is not supported." % web_driver_type)
        self.set_page_load_timeout(web_driver_default_page_load_timeout if page_load_timeout == DEFAULT else page_load_timeout)
        self.set_script_timeout(web_driver_default_script_timeout if script_timeout == DEFAULT else script_timeout)
        self.__wait_interval = web_driver_default_wait_interval if wait_interval == DEFAULT else wait_interval
        self.__wait_timeout = web_driver_default_wait_timeout if wait_timeout == DEFAULT else wait_timeout

    def _selenium_web_driver(self):
        return self.__selenium_web_driver

    def _selenium_context(self):
        return self.__selenium_web_driver

    def get_web_driver(self):
        return self

    def get_web_driver_type(self):
        return self.__web_driver_type

    def get_wait_interval(self):
        return self.__wait_interval

    def get_wait_timeout(self):
        return self.__wait_timeout

    @SupportedBy(WebDriverType._BROWSER)
    def maximize_window(self):
        self.__selenium_web_driver.maximize_window()

    def set_page_load_timeout(self, timeout):
        self.__selenium_web_driver.set_page_load_timeout(timeout / 1000.0)

    def set_script_timeout(self, timeout):
        self.__selenium_web_driver.set_script_timeout(timeout / 1000.0)

    def execute_script(self, script, *args):
        return self.__selenium_web_driver.execute_script(script, *args)

    def execute_async_script(self, script, *args):
        return self.__selenium_web_driver.execute_async_script(script, *args)

    def open(self, url):
        self.__selenium_web_driver.get(url)

    def get_title(self):
        return self.__selenium_web_driver.title

    def refresh(self):
        self.__selenium_web_driver.refresh()

    def back(self):
        self.__selenium_web_driver.back()

    def forward(self):
        self.__selenium_web_driver.forward()

    def quit(self):
        self.__selenium_web_driver.quit()

    def get_current_url(self):
        return self.__selenium_web_driver.current_url

    def get_page_source(self):
        return self.__selenium_web_driver.page_source

    @SupportedBy(WebDriverType._MOBILE)
    def get_contexts(self):
        return self.__selenium_web_driver.contexts

    @SupportedBy(WebDriverType._MOBILE)
    def get_current_context(self):
        return self.__selenium_web_driver.current_context

    @SupportedBy(WebDriverType._MOBILE)
    def switch_to_context(self, context_name):
        self.__selenium_web_driver.switch_to.context(context_name)

    def switch_to_frame(self, frame_reference):
        self.__selenium_web_driver.switch_to.frame(frame_reference)

    def switch_to_parent_frame(self):
        self.__selenium_web_driver.switch_to.parent_frame()

    def switch_to_default_content(self):
        self.__selenium_web_driver.switch_to.default_content()

    def get_alert(self):
        return self.__selenium_web_driver.switch_to.alert

    def is_alert_present(self):
        try:
            alert = self.__selenium_web_driver.switch_to.alert
            return True
        except NoAlertPresentException:
            return False

    def wait_for_alert_present(self):
        wait_for(self.is_alert_present)

    def get_cookies(self):
        return self.__selenium_web_driver.get_cookies()

    def get_cookie(self, name):
        return self.__selenium_web_driver.get_cookie(name)

    def delete_cookie(self, name):
        self.__selenium_web_driver.delete_cookie(name)

    def delete_all_cookies(self):
        self.__selenium_web_driver.delete_all_cookies()

    def add_cookie(self, cookie_dict):
        self.__selenium_web_driver.add_cookie(cookie_dict)

    def get_desired_capabilities(self):
        return self.__selenium_web_driver.desired_capabilities

    def get_screenshot_as_file(self, filename):
        return self.__selenium_web_driver.get_screenshot_as_file(filename)

    def get_screenshot_as_png(self):
        return self.__selenium_web_driver.get_screenshot_as_png()

    def get_screenshot_as_base64(self):
        return self.__selenium_web_driver.get_screenshot_as_base64()

    save_screenshot = get_screenshot_as_file

    def get_current_window_handle(self):
        return self.__selenium_web_driver.current_window_handle

    def get_window_handles(self):
        return self.__selenium_web_driver.window_handles

    def switch_to_window(self, window_reference):
        self.__selenium_web_driver.switch_to.window(window_reference)

    def close_window(self, window_reference="current"):
        if window_reference == "current":
            self.__selenium_web_driver.close()
        else:
            current_window = self.get_current_window_handle()
            self.switch_to_window(window_reference)
            self.__selenium_web_driver.close()
            self.switch_to_window(current_window)

    def set_window_size(self, width, height, window_reference="current"):
        self.__selenium_web_driver.set_window_size(width, height, window_reference)

    def get_window_size(self, window_reference="current"):
        return self.__selenium_web_driver.get_window_size(window_reference)

    def set_window_position(self, x, y, window_reference="current"):
        self.__selenium_web_driver.set_window_position(x, y, window_reference)

    def get_window_position(self, window_reference="current"):
        return self.__selenium_web_driver.get_window_position(window_reference)

    def get_orientation(self):
        return self.__selenium_web_driver.orientation

    def set_orientation(self, value):
        self.__selenium_web_driver = value

    def get_application_cache(self):
        return self.__selenium_web_driver.application_cache

    def get_log_types(self):
        return self.__selenium_web_driver.log_types

    def get_log(self, log_type):
        return self.__selenium_web_driver.get_log(log_type)

    @SupportedBy(WebDriverType._MOBILE)
    def swipe(self, start_x, start_y, end_x, end_y, duration=None):
        self.__selenium_web_driver.swipe(start_x, start_y, end_x, end_y, duration)

    @SupportedBy(WebDriverType._MOBILE)
    def flick(self, start_x, start_y, end_x, end_y):
        self.__selenium_web_driver.flick(start_x, start_y, end_x, end_y)

    @SupportedBy(WebDriverType._MOBILE)
    def app_strings(self, language=None, string_file=None):
        return self.__selenium_web_driver.app_strings(language, string_file)

    @SupportedBy(WebDriverType._MOBILE)
    def reset_current_app(self):
        self.__selenium_web_driver.reset()

    @SupportedBy(WebDriverType._MOBILE)
    def hide_keyboard(self, key_name=None, key=None, strategy=None):
        self.__selenium_web_driver.hide_keyboard(key_name, key, strategy)

    @SupportedBy(WebDriverType._MOBILE)
    def key_event(self, key_code, meta_state=None):
        self.__selenium_web_driver.keyevent(key_code, meta_state)

    @SupportedBy(WebDriverType.ANDROID)
    def press_key(self, key_code, meta_state=None):
        self.__selenium_web_driver.press_keycode(key_code, meta_state)

    @SupportedBy(WebDriverType.ANDROID)
    def long_press_key(self, key_code, meta_state=None):
        self.__selenium_web_driver.long_press_keycode(key_code, meta_state)

    @SupportedBy(WebDriverType.ANDROID)
    def get_current_activity(self):
        return self.__selenium_web_driver.current_activity

    @SupportedBy(WebDriverType._MOBILE)
    def pull_file(self, path):
        return self.__selenium_web_driver.pull_file(path)

    @SupportedBy(WebDriverType._MOBILE)
    def pull_folder(self, path):
        return self.__selenium_web_driver.pull_folder(path)

    @SupportedBy(WebDriverType._MOBILE)
    def push_file(self, path, base64data):
        self.__selenium_web_driver.push_file(path, base64data)
<commit_msg>key_event is only for android<commit_after>from selenium.webdriver import Ie, Firefox, Chrome, Opera, Safari, PhantomJS
from selenium.common.exceptions import NoAlertPresentException

from .context import Context
from .exceptions import UnsupportedWebDriverTypeException
from .waits.waiter import wait_for
from .config import DEFAULT, web_driver_default_page_load_timeout, web_driver_default_script_timeout, web_driver_default_wait_interval, \
    web_driver_default_wait_timeout
from .decorator import SupportedBy

__author__ = 'karl.gong'


class WebDriverType:
    IE = "ie"
    FIREFOX = "firefox"
    CHROME = "chrome"
    OPERA = "opera"
    SAFARI = "safari"
    PHANTOMJS = "phantomjs"
    ANDROID = "android"
    IOS = "ios"

    _BROWSER = [IE, FIREFOX, CHROME, OPERA, SAFARI, PHANTOMJS]
    _MOBILE = [ANDROID, IOS]
    _ALL = _BROWSER + _MOBILE


class WebDriver(Context):
    def __init__(self, web_driver_type=WebDriverType.CHROME, page_load_timeout=DEFAULT, script_timeout=DEFAULT,
                 wait_interval=DEFAULT, wait_timeout=DEFAULT, **kwargs):
        Context.__init__(self)
        self.__web_driver_type = web_driver_type.lower()
        if self.__web_driver_type == WebDriverType.IE:
            self.__selenium_web_driver = Ie(**kwargs)
        elif self.__web_driver_type == WebDriverType.FIREFOX:
            self.__selenium_web_driver = Firefox(**kwargs)
        elif self.__web_driver_type == WebDriverType.CHROME:
            self.__selenium_web_driver = Chrome(**kwargs)
        elif self.__web_driver_type == WebDriverType.OPERA:
            self.__selenium_web_driver = Opera(**kwargs)
        elif self.__web_driver_type == WebDriverType.SAFARI:
            self.__selenium_web_driver = Safari(**kwargs)
        elif self.__web_driver_type == WebDriverType.PHANTOMJS:
            self.__selenium_web_driver = PhantomJS(**kwargs)
        elif self.__web_driver_type in WebDriverType._MOBILE:
            from appium.webdriver.webdriver import WebDriver as Mobile
            self.__selenium_web_driver = Mobile(**kwargs)
        else:
            raise UnsupportedWebDriverTypeException("The web driver type [%s] is not supported." % web_driver_type)
        self.set_page_load_timeout(web_driver_default_page_load_timeout if page_load_timeout == DEFAULT else page_load_timeout)
        self.set_script_timeout(web_driver_default_script_timeout if script_timeout == DEFAULT else script_timeout)
        self.__wait_interval = web_driver_default_wait_interval if wait_interval == DEFAULT else wait_interval
        self.__wait_timeout = web_driver_default_wait_timeout if wait_timeout == DEFAULT else wait_timeout

    def _selenium_web_driver(self):
        return self.__selenium_web_driver

    def _selenium_context(self):
        return self.__selenium_web_driver

    def get_web_driver(self):
        return self

    def get_web_driver_type(self):
        return self.__web_driver_type

    def get_wait_interval(self):
        return self.__wait_interval

    def get_wait_timeout(self):
        return self.__wait_timeout

    @SupportedBy(WebDriverType._BROWSER)
    def maximize_window(self):
        self.__selenium_web_driver.maximize_window()

    def set_page_load_timeout(self, timeout):
        self.__selenium_web_driver.set_page_load_timeout(timeout / 1000.0)

    def set_script_timeout(self, timeout):
        self.__selenium_web_driver.set_script_timeout(timeout / 1000.0)

    def execute_script(self, script, *args):
        return self.__selenium_web_driver.execute_script(script, *args)

    def execute_async_script(self, script, *args):
        return self.__selenium_web_driver.execute_async_script(script, *args)

    def open(self, url):
        self.__selenium_web_driver.get(url)

    def get_title(self):
        return self.__selenium_web_driver.title

    def refresh(self):
        self.__selenium_web_driver.refresh()

    def back(self):
        self.__selenium_web_driver.back()

    def forward(self):
        self.__selenium_web_driver.forward()

    def quit(self):
        self.__selenium_web_driver.quit()

    def get_current_url(self):
        return self.__selenium_web_driver.current_url

    def get_page_source(self):
        return self.__selenium_web_driver.page_source

    @SupportedBy(WebDriverType._MOBILE)
    def get_contexts(self):
        return self.__selenium_web_driver.contexts

    @SupportedBy(WebDriverType._MOBILE)
    def get_current_context(self):
        return self.__selenium_web_driver.current_context

    @SupportedBy(WebDriverType._MOBILE)
    def switch_to_context(self, context_name):
        self.__selenium_web_driver.switch_to.context(context_name)

    def switch_to_frame(self, frame_reference):
        self.__selenium_web_driver.switch_to.frame(frame_reference)

    def switch_to_parent_frame(self):
        self.__selenium_web_driver.switch_to.parent_frame()

    def switch_to_default_content(self):
        self.__selenium_web_driver.switch_to.default_content()

    def get_alert(self):
        return self.__selenium_web_driver.switch_to.alert

    def is_alert_present(self):
        try:
            alert = self.__selenium_web_driver.switch_to.alert
            return True
        except NoAlertPresentException:
            return False

    def wait_for_alert_present(self):
        wait_for(self.is_alert_present)

    def get_cookies(self):
        return self.__selenium_web_driver.get_cookies()

    def get_cookie(self, name):
        return self.__selenium_web_driver.get_cookie(name)

    def delete_cookie(self, name):
        self.__selenium_web_driver.delete_cookie(name)

    def delete_all_cookies(self):
        self.__selenium_web_driver.delete_all_cookies()

    def add_cookie(self, cookie_dict):
        self.__selenium_web_driver.add_cookie(cookie_dict)

    def get_desired_capabilities(self):
        return self.__selenium_web_driver.desired_capabilities

    def get_screenshot_as_file(self, filename):
        return self.__selenium_web_driver.get_screenshot_as_file(filename)

    def get_screenshot_as_png(self):
        return self.__selenium_web_driver.get_screenshot_as_png()

    def get_screenshot_as_base64(self):
        return self.__selenium_web_driver.get_screenshot_as_base64()

    save_screenshot = get_screenshot_as_file

    def get_current_window_handle(self):
        return self.__selenium_web_driver.current_window_handle

    def get_window_handles(self):
        return self.__selenium_web_driver.window_handles

    def switch_to_window(self, window_reference):
        self.__selenium_web_driver.switch_to.window(window_reference)

    def close_window(self, window_reference="current"):
        if window_reference == "current":
            self.__selenium_web_driver.close()
        else:
            current_window = self.get_current_window_handle()
            self.switch_to_window(window_reference)
            self.__selenium_web_driver.close()
            self.switch_to_window(current_window)

    def set_window_size(self, width, height, window_reference="current"):
        self.__selenium_web_driver.set_window_size(width, height, window_reference)

    def get_window_size(self, window_reference="current"):
        return self.__selenium_web_driver.get_window_size(window_reference)

    def set_window_position(self, x, y, window_reference="current"):
        self.__selenium_web_driver.set_window_position(x, y, window_reference)

    def get_window_position(self, window_reference="current"):
        return self.__selenium_web_driver.get_window_position(window_reference)

    def get_orientation(self):
        return self.__selenium_web_driver.orientation

    def set_orientation(self, value):
        self.__selenium_web_driver = value

    def get_application_cache(self):
        return self.__selenium_web_driver.application_cache

    def get_log_types(self):
        return self.__selenium_web_driver.log_types

    def get_log(self, log_type):
        return self.__selenium_web_driver.get_log(log_type)

    @SupportedBy(WebDriverType._MOBILE)
    def swipe(self, start_x, start_y, end_x, end_y, duration=None):
        self.__selenium_web_driver.swipe(start_x, start_y, end_x, end_y, duration)

    @SupportedBy(WebDriverType._MOBILE)
    def flick(self, start_x, start_y, end_x, end_y):
        self.__selenium_web_driver.flick(start_x, start_y, end_x, end_y)

    @SupportedBy(WebDriverType._MOBILE)
    def app_strings(self, language=None, string_file=None):
        return self.__selenium_web_driver.app_strings(language, string_file)

    @SupportedBy(WebDriverType._MOBILE)
    def reset_current_app(self):
        self.__selenium_web_driver.reset()

    @SupportedBy(WebDriverType._MOBILE)
    def hide_keyboard(self, key_name=None, key=None, strategy=None):
        self.__selenium_web_driver.hide_keyboard(key_name, key, strategy)

    @SupportedBy(WebDriverType.ANDROID)
    def key_event(self, key_code, meta_state=None):
        self.__selenium_web_driver.keyevent(key_code, meta_state)

    @SupportedBy(WebDriverType.ANDROID)
    def press_key(self, key_code, meta_state=None):
        self.__selenium_web_driver.press_keycode(key_code, meta_state)

    @SupportedBy(WebDriverType.ANDROID)
    def long_press_key(self, key_code, meta_state=None):
        self.__selenium_web_driver.long_press_keycode(key_code, meta_state)

    @SupportedBy(WebDriverType.ANDROID)
    def get_current_activity(self):
        return self.__selenium_web_driver.current_activity

    @SupportedBy(WebDriverType._MOBILE)
    def pull_file(self, path):
        return self.__selenium_web_driver.pull_file(path)

    @SupportedBy(WebDriverType._MOBILE)
    def pull_folder(self, path):
        return self.__selenium_web_driver.pull_folder(path)

    @SupportedBy(WebDriverType._MOBILE)
    def push_file(self, path, base64data):
        self.__selenium_web_driver.push_file(path, base64data)

{'fraction_non_alphanumeric': 0.07865736528156911, 'fraction_numerical': 0.0018198362147406734, 'mean_word_length': 3.934647044150661, 'pattern_counts': {'":': 2, '<': 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': '3265125', 'n_tokens_mistral': 6467, 'n_tokens_neox': 6132, 'n_words': 978}