示例#1
0
def select_g(context,
             selector,
             text=None,
             *argv,
             index=None,
             value=None,
             **kw):
    germanium = find_germanium_object(context)
    select_element = _element(germanium, selector)

    s = Select(select_element)

    if text is not None:
        for single_text in _ensure_list(text):
            s.select_by_visible_text(single_text)
    if index is not None:
        for single_index in _ensure_list(index):
            single_index = int(single_index)
            s.select_by_index(single_index)
    if value is not None:
        for single_value in _ensure_list(value):
            s.select_by_value(single_value)

    if not text and not index and not value:
        raise Exception(
            "`select()` was called on an element, but no text, index or "
            "value was provided. In case you want to deselect all values "
            "on a multi select use `deselect()` instead.")
示例#2
0
def deselect_g(context,
               selector,
               text=None,
               *argv,
               index=None,
               value=None,
               **kw):
    germanium = find_germanium_object(context)
    select_element = _element(germanium, selector)

    s = Select(select_element)

    if text is not None:
        for single_text in _ensure_list(text):
            s.deselect_by_visible_text(single_text)
    if index is not None:
        for single_index in _ensure_list(index):
            single_index = int(single_index)
            s.deselect_by_index(single_index)
    if value is not None:
        for single_value in _ensure_list(value):
            s.deselect_by_value(single_value)

    if not text and not index and not value:
        s.deselect_all()
def _ensure_selectors(items):
    items = _ensure_list(items)

    for i in range(len(items)):
        items[i] = _ensure_selector(items[i])

    return items
def find_germanium_object(items):
    """
    Finds the germanium object in the given call. Searches it
    first in the parameter list, and if is not there, it searches
    it in the self reference, as either self.germanium or self._germanium.
    :param items:
    :return:
    """
    items = _ensure_list(items)

    for item in items:
        if isinstance(item, GermaniumDriver):
            return item

    self = items[0]  # get the self/context reference

    _germanium = getattr(self, "germanium", None)

    if _germanium:
        return _germanium

    return self._germanium
示例#5
0
def waited(closures, *extra_closures, while_not=None, timeout=10):
    """
    Executes a function given as argument every 400 milliseconds until it returns something,
    and returns that something.

    If it goes more than the timeout in seconds, then this function returns `None`.

    If the waiting closures throw exception, then it is assumed as a temporary false
    result, but the wait will continue. This is intentional so you can wait for an element
    even if the page is still loading.

    :param closures:
    :param while_not:
    :param timeout:
    :param extra_closures:
    """
    while_not = _ensure_list(while_not)
    closures = list(_ensure_list(closures))
    closures.extend(extra_closures)

    def closure_try_catch():
        for closure in closures:
            try:
                result = _resolve_closure(closure)
                return result
            except Exception as e:
                print("WARNING: waiting as false since: %s" % e)

    starting_time = _current_time_in_millis()
    current_time = starting_time
    delta = float(timeout) * 1000

    if delta <= 0:
        raise Exception("You need to specify a timeout that is greater than 0. The timeout is expressed in seconds.")

    closure_result = False

    while current_time - starting_time < delta:
        starting_closure_eval_time = current_time

        for while_not_closure in while_not:
            try:
                if while_not_closure():
                    return None
            except Exception as e:
                print("Waiting failed, since while_not condition raised exception: %s" % e)
                return None

        closure_result = closure_try_catch()

        if closure_result:
            break

        current_time = _current_time_in_millis()
        ending_closure_eval_time = current_time

        # Execution times don't count, and must be subtracted from the
        # potential delay.
        actual_execution_time = ending_closure_eval_time - starting_closure_eval_time

        # we do the sleep only if the execution time is quicker than 400ms,
        # otherwise we already waited for the response from the server.

        if actual_execution_time < 400:
            time.sleep(float(400 - actual_execution_time) / 1000.0)

    if not closure_result:
        return None

    return closure_result