def is_mobile_device(driver):
    # type: (AnyWebDriver) -> bool
    """
    Returns whether the platform running is a mobile device or not.

    :return: True if the platform running the test is a mobile platform. False otherwise.
    """
    is_mobile = """
if( navigator.userAgent.match(/Android/i) ||
    navigator.userAgent.match(/iPhone/i) ||
    navigator.userAgent.match(/iPad/i)   ||
    navigator.userAgent.match(/iPod/i) ) {
    return true;
} else {
    return false;
}
    """
    # TODO: Implement proper UserAgent handling
    driver = get_underlying_driver(driver)
    platform_name = driver.desired_capabilities.get("platformName", "").lower()
    # platformName sometime have different names
    is_mobile_platform = "android" in platform_name or "ios" in platform_name
    if not is_mobile_platform:
        try:
            is_mobile_platform = driver.execute_script(is_mobile)
        except WebDriverException as e:
            logger.warning(
                "Got error during checking if current platform is mobile. "
                "\n\t {}".format(str(e)))
            if "Method is not implemented" in str(e):
                # potentially mobile app
                is_mobile_platform = True
            else:
                is_mobile_platform = False
    return is_mobile_platform
Ejemplo n.º 2
0
def _traverse_dom_tree(driver, args_obj, dom_tree, frame_index, base_url):
    # type: (EyesWebDriver, dict, dict, int, str) -> None
    logger.debug('Traverse DOM Tree: index_tree {}'.format(frame_index))

    tag_name = dom_tree.get('tagName', None)  # type: str
    if not tag_name:
        return None

    if frame_index > -1:
        driver.switch_to.frame(frame_index)
        dom = driver.execute_script(_CAPTURE_FRAME_SCRIPT, args_obj)
        dom_tree['childNodes'] = dom

        src_url = None
        attrs_node = dom_tree.get('attributes', None)
        if attrs_node:
            src_url = attrs_node.get('eyes_core', None)

        if src_url is None:
            logger.warning('IFRAME WITH NO SRC')

        _traverse_dom_tree(driver, args_obj, dom, -1, src_url)
        driver.switch_to.parent_frame()

    is_html = tag_name.upper() == 'HTML'
    if is_html:
        logger.debug('Traverse DOM Tree: Inside HTML')
        css = _get_frame_bundled_css(driver, base_url)
        dom_tree['css'] = css

    _loop(driver, args_obj, dom_tree, base_url)
Ejemplo n.º 3
0
 def _try_capture_dom(self):
     try:
         dom_json = dom_capture.get_full_window_dom(self._driver)
         return dom_json
     except Exception as e:
         logger.warning(
             'Exception raising during capturing DOM Json. Passing...\n Got next error: {}'.format(str(e)))
         return None
Ejemplo n.º 4
0
def _get_frame_bundled_css(driver, base_url):
    # type: (EyesWebDriver, str) -> str
    if not general_utils.is_absolute_url(base_url):
        logger.warning('Base URL is not an absolute URL!')
    result = driver.execute_script(_CAPTURE_CSSOM_SCRIPT)
    css_string = ''
    for item in result:
        kind = item[0:5]
        value = item[5:]
        css = ''
        if kind == 'text:':
            css = value
        else:
            css = _download_css(base_url, value)

        stylesheet = tinycss2.parse_stylesheet(css, skip_comments=True,
                                               skip_whitespace=True)
        css = _serialize_css(base_url, stylesheet)
        css_string += css

    return css_string
Ejemplo n.º 5
0
def _parse_and_serialize_css(node, text, minimize=False):
    # type: (CssNode, tp.Text, bool) -> tp.Generator
    def is_import_node(n):
        return n.type == "at-rule" and n.lower_at_keyword == "import"

    stylesheet = tinycss2.parse_stylesheet(text,
                                           skip_comments=True,
                                           skip_whitespace=True)
    for style_node in stylesheet:
        if is_import_node(style_node):
            for tag in style_node.prelude:
                if tag.type == "url":
                    logger.debug("The node has import")
                    yield CssNode.create_sub_node(parent_node=node,
                                                  href=tag.value)
            continue

        try:
            if minimize:
                try:
                    # remove whitespaces inside blocks
                    style_node.content = [
                        tok for tok in style_node.content
                        if tok.type != "whitespace"
                    ]
                except AttributeError as e:
                    logger.warning(
                        "Cannot serialize item: {}, cause error: {}".format(
                            style_node, str(e)))
            serialized = style_node.serialize()
            if minimize:
                serialized = (serialized.replace("\n", "").replace(
                    "/**/", " ").replace(" {", "{"))

        except TypeError as e:
            logger.warning(str(e))
            continue
        yield CssNode.create_serialized_node(text=serialized)
Ejemplo n.º 6
0
def _parse_and_serialize_css(node, text, minimize=False):
    # type: (CssNode, tp.Text, bool) -> tp.Generator
    is_import_node = lambda n: n.type == 'at-rule' and n.lower_at_keyword == 'import'
    stylesheet = tinycss2.parse_stylesheet(text,
                                           skip_comments=True,
                                           skip_whitespace=True)
    for style_node in stylesheet:
        if is_import_node(style_node):
            for tag in style_node.prelude:
                if tag.type == 'url':
                    logger.debug('The node has import')
                    yield CssNode.create_sub_node(parent_node=node,
                                                  href=tag.value)
            continue

        try:
            if minimize:
                try:
                    # remove whitespaces inside blocks
                    style_node.content = [
                        tok for tok in style_node.content
                        if tok.type != 'whitespace'
                    ]
                except AttributeError as e:
                    logger.warning(
                        "Cannot serialize item: {}, cause error: {}".format(
                            style_node, str(e)))
            serialized = style_node.serialize()
            if minimize:
                serialized = serialized.replace('\n', '').replace(
                    '/**/', ' ').replace(' {', '{')

        except TypeError as e:
            logger.warning(str(e))
            continue
        yield CssNode.create_serialized_node(text=serialized)
Ejemplo n.º 7
0
 def get_css(url):
     if url.startswith("blob:"):
         logger.warning("Passing blob URL: {}".format(url))
         return ""
     return requests.get(url, timeout=CSS_DOWNLOAD_TIMEOUT).text.strip()