Example #1
0
def child_nodes_g(context, selector, only_elements=True):
    germanium = find_germanium_object(context)
    element = _element(germanium, selector)

    code = load_script(__name__, 'child-nodes.min.js')

    return germanium.js(code, element, only_elements)
Example #2
0
    def get_box(self):
        from germanium.static import S, js
        code = load_script(__name__, 'box.min.js')

        target_element = S(self._selector).element()

        if not target_element:
            raise Exception("The passed selector (%s) for finding "
                            "the bounding box didn't matched any elements." %
                            self._selector)

        top, right, bottom, left, \
        center, middle, width, height = js(code, target_element)

        self._box = {
            "top": top,
            "right": right,
            "bottom": bottom,
            "left": left,
            "center": center,
            "middle": middle,
            "width": width,
            "height": height
        }

        return self
def get_style_g(context, selector, name):
    germanium = find_germanium_object(context)

    # FIXME: get styles from hidden attributes, see get_attributes
    if isinstance(selector, WebElement):
        element = selector
    else:
        element = _element(germanium, selector)

    code = load_script(__name__, 'get-style.min.js')

    return germanium.js(code, element, name)
    def _find_element_list(self):
        # Since this filter sorts elements that would appear visually right of
        # or left of things, we don't need to get the raw element list using
        # `_find_element_list` but we can work with the filtered list.
        elements = self.locator.element_list()

        left_of_elements = OrderedDict()
        for selector in self.left_of_filters:
            for left_of_element in selector.element_list():
                left_of_elements[left_of_element] = 1

        right_of_elements = OrderedDict()
        for selector in self.right_of_filters:
            for right_of_element in selector.element_list():
                right_of_elements[right_of_element] = 1

        above_elements = OrderedDict()
        for selector in self.above_filters:
            for above_element in selector.element_list():
                above_elements[above_element] = 1

        below_elements = OrderedDict()
        for selector in self.below_filters:
            for below_element in selector.element_list():
                below_elements[below_element] = 1

        js_arguments = []

        code = load_script(__name__, 'positional-filter.min.js')

        js_arguments.append(code)
        js_arguments.append(len(above_elements))
        for above_element in above_elements:
            js_arguments.append(above_element)
        js_arguments.append(len(right_of_elements))
        for right_of_element in right_of_elements:
            js_arguments.append(right_of_element)
        js_arguments.append(len(below_elements))
        for below_element in below_elements:
            js_arguments.append(below_element)
        js_arguments.append(len(left_of_elements))
        for left_of_element in left_of_elements:
            js_arguments.append(left_of_element)
        js_arguments.append(len(elements))
        for element in elements:
            js_arguments.append(element)

        result_elements = self._germanium.js(*js_arguments)

        return result_elements
    def _find_element_list(self):
        """
        Finds a single element using the given code.
        """
        code = load_script(__name__, 'text.min.js')

        elements = self._germanium.js(code, self._root_element,
                                      self._searched_text, self._exact_match,
                                      self._trim_text)

        if not elements:
            return []

        if not isinstance(elements, list):
            raise Exception("Code `%s` is not returning a list of elements." %
                            self._code)

        return elements
def get_attributes_g(context, selector, only_visible=True):
    """
    Return the attributes for the element that is obtained
    from the selector as a dictionary object.
    :param context:
    :param selector:
    :param only_visible:
    :return:
    """
    germanium = find_germanium_object(context)

    if isinstance(selector, WebElement):
        element = selector
    else:
        element = germanium.S(selector).element(only_visible=only_visible)

    if not element:
        raise Exception("Unable to find '%s' to get_attributes." % selector)

    code = load_script(__name__, 'get-attributes.min.js')

    return germanium.js(code, element)
Example #7
0
    def _find_element_list(self):
        if not self._root_element:
            return self._element

        js_arguments = []

        code = load_script(__name__, 'inside-filter.min.js')

        js_arguments.append(code)
        js_arguments.append(0)  # ignore without_children
        js_arguments.append(1)
        js_arguments.append(self._root_element)
        js_arguments.append(0)  # no containing_elements
        js_arguments.append(0)  # no outside_elements
        js_arguments.append(0)  # no containing_all selectors
        js_arguments.append(0)  # no containing_all element/groupIds pairs

        js_arguments.append(len(self._element))
        for element in self._element:
            js_arguments.append(element)

        result_elements = self._germanium.js(*js_arguments)

        return result_elements
    def _find_element_list(self):
        # Since the inside/outside/contains/without children
        # works with the DOM structure, it might be used to find
        # invisible elements. So we need to get the raw list of
        # elements.

        inside_elements = OrderedDict()
        for selector in self.inside_filters:
            element_list = selector.element_list()

            # if we have an inside element that itself can't be found,
            # don't bother to search the elements further
            if not element_list:
                return []

            for inside_element in element_list:
                inside_elements[inside_element] = 1

        # in case there are no inside_elements, we just use the regular
        # find_element_by... on the selenium instance
        if not inside_elements:
            inside_elements = OrderedDict()
            inside_elements[None] = None

        elements = OrderedDict()
        for inside_element in inside_elements:
            self._locator.set_root_element(inside_element)
            inside_found_elements = self._locator._find_element_list()

            if not inside_found_elements:
                inside_found_elements = []

            for element in inside_found_elements:
                elements[element] = 1

        elements = list(elements)

        outside_elements = OrderedDict()
        for selector in self.outside_filters:
            element_list = selector.element_list(only_visible=False)

            # if we have an outside element that itself can't be found,
            # don't bother to search the elements further
            if not element_list:
                return []

            for outside_element in element_list:
                outside_elements[outside_element] = 1

        containing_elements = OrderedDict()
        for selector in self.containing_filters:
            element_list = selector.element_list(only_visible=False)

            # if we don't have any elements that we're supposed to
            # contain, it means the selector isn't matching, so don't
            # bother, trying to find further, otherwise we will match
            # a lot of false positives.
            if not element_list:
                return []

            for containing_element in element_list:
                containing_elements[containing_element] = 1

        # `containing_all` needs to create groups for each selector
        # and then filter the resulting elements against the
        # groups.
        #
        # We will create a dictionary that holds all the elements,
        # linked with all the group indexes they belong to, as a
        # string CSV.
        #
        # The search will remove all the elements that don't contain
        # all the groups.

        group_index = -1
        containing_all_elements = OrderedDict()
        for selector in self.containing_all_filters:
            group_index += 1
            element_list = selector._find_element_list()

            # if we have things we need to contain, but the selectors
            # don't return the elements, we don't bother so we don't
            # get false positives. eg A().contains(Text("missing")) will
            # match all A elements otherwise, the contains becomes bogus.
            if not element_list:
                return []

            for containing_all_element in element_list:
                # if the same selector for a group returns the same element multiple times,
                # make sure it's in our map only once.
                if containing_all_element in containing_all_elements:
                    containing_all_elements[containing_all_element].add(str(group_index))
                    continue
                items = set()
                items.add(str(group_index))
                containing_all_elements[containing_all_element] = items

        js_arguments = []

        code = load_script(__name__, 'inside-filter.min.js')

        js_arguments.append(code)
        js_arguments.append(1 if self.without_children else 0)
        js_arguments.append(0)  # FIXME: remove
        js_arguments.append(len(containing_elements))

        for containing_element in containing_elements:
            js_arguments.append(containing_element)

        js_arguments.append(len(outside_elements))
        for outside_element in outside_elements:
            js_arguments.append(outside_element)

        js_arguments.append(len(self.containing_all_filters))  # groupCount
        js_arguments.append(len(containing_all_elements))
        for containing_all_element in containing_all_elements:
            js_arguments.append(containing_all_element)
            js_arguments.append(",".join(containing_all_elements[containing_all_element]))

        js_arguments.append(len(elements))
        for element in elements:
            js_arguments.append(element)

        result_elements = self._germanium.js(*js_arguments)

        return result_elements