Esempio n. 1
0
def open_window():
    r"""Open new tab.

    Uses javascript to do this so javascript has to be enabled.

    Examples
    --------
    .. code-block:: robotframework

        OpenWindow

    Related keywords
    ----------------
    \`CloseAllBrowsers\`, \`CloseBrowser\`, \`CloseOthers\`, \`GoTo\`,
    \`OpenBrowser\`, \`SwitchWindow\`
    """
    script = 'window.open()'
    javascript.execute_javascript(script)
    window_handles = window.get_window_handles()
    current_window_handle = window.get_current_window_handle()
    index = window_handles.index(current_window_handle)
    new_window_index = index + 1
    window.switch_to_window(window_handles[new_window_index])
    try:
        xhr.setup_xhr_monitor()
    except QWebDriverError:
        logger.debug('XHR monitor threw exception. Bypassing jQuery injection')
Esempio n. 2
0
def switch_window(index, timeout=0):  # pylint: disable=unused-argument
    r"""Switch to another tab.

    Examples
    --------
    .. code-block:: robotframework

        SwitchWindow     1
        SwitchWindow     NEW    # Switches to latest opened tab

    Parameters
    ----------
    index : str
        Index of the tab starting from one and counting from left to right.
        OR
        Special keyword "NEW" which can be used to move to the latest opened tab.
    timeout : str | int
        How long we search before failing.

    Raises
    ------
    ValueError
         If the window index is out of reach

    Related keywords
    ----------------
    \`CloseBrowser\`, \`CloseWindow\`, \`CloseOthers\`, \`GoTo\`, \`OpenWindow\`
    """
    window_handles = window.get_window_handles()
    logger.info("Current browser contains {} tabs".format(len(window_handles)))
    if index.isdigit():
        if int(index) == 0:
            raise QWebValueError('SwitchWindow index starts at 1.')
        i = int(index) - 1
        if i < len(window_handles):
            correct_window_handle = window_handles[i]
            window.switch_to_window(correct_window_handle)
            return
        logger.debug('Tried to select tab with index {} but there'
                     ' are only {} tabs open'.format(index, len(window_handles)))
    elif index == "NEW":
        window.switch_to_window(window_handles[-1])
        return
    else:
        raise QWebValueError(
            'Given argument "{}" is not a digit or NEW'.format(index))
    raise QWebDriverError(
        'Tried to select tab with index {} but there are only {} tabs open'
        .format(index, len(window_handles)))
Esempio n. 3
0
def close_window():
    r"""Close current tab and switch context to another window handle.

    If you need to change to specific tab, use switch window keyword.

    Examples
    --------
    .. code-block:: robotframework

        CloseWindow

    Related keywords
    ----------------
    \`CloseBrowser\`, \`CloseOthers\`, \`GoTo\`, \`OpenWindow\`, \`SwitchWindow\`
    """
    driver = browser.get_current_browser()
    window_handles = window.get_window_handles()
    logger.info("Current browser has {} tabs".format(len(window_handles)))
    if len(window_handles) == 1:
        logger.info("Only one tab, handle closing without changing context")
        browser.remove_from_browser_cache(driver)  # remove from browser cache
        driver.close()
    else:
        logger.info(
            "Multiple tabs open, can change window context to another one")
        current_window = window.get_current_window_handle()
        current_index = window_handles.index(current_window)
        logger.info("Current index {}".format(current_index))
        driver.close()
        # "refresh" window handles
        window_handles = window.get_window_handles()
        current_length = len(window_handles)
        logger.info(
            "After closing, {} tabs remain open".format(current_length))
        # if current index is more than new length, move to last handle
        if current_index > (len(window_handles) - 1):
            window.switch_to_window(window_handles[(current_index - 1)])
        # move to next window (as browsers do)
        else:
            window.switch_to_window(window_handles[current_index])
        logger.info("Changed context to tab with url {}".format(
            window.get_url()))
Esempio n. 4
0
def close_others():
    r"""Close all windows except the first window.

    If you have a test that may open new windows, this keyword closes them
    and switches to the first window.

    Examples
    --------
    .. code-block:: robotframework

        CloseOthers

    Raises
    ------
    NoSuchWindowException
        If other windows cannot been closed

    Related keywords
    ----------------
    \`CloseBrowser\`, \`CloseWindow\`, \`GoTo\`, \`OpenWindow\`, \`SwitchWindow\`
    """
    window_handles = window.get_window_handles()
    logger.info("Current browser has {} tabs".format(len(window_handles)))
    if len(window_handles) == 1:
        return

    driver = browser.get_current_browser()
    while len(window_handles) > 1:
        try:
            window_handle = window_handles.pop()
            window.switch_to_window(window_handle)
            driver.close()
        except NoSuchWindowException:
            logger.info('Failed to close window')
    first_window_handle = window_handles.pop()
    window.switch_to_window(first_window_handle)

    number_of_handles = len(window.get_window_handles())
    if number_of_handles != 1:
        raise Exception(
            'Expected 1 window open, found {0}'.format(number_of_handles))