Ejemplo n.º 1
0
def close_remote_browser():
    r"""Close remote browser session which is connected to the target browser.

    Closes only the remote browser session and leaves the target browser
    running. This makes it possible to continue using the existing browser
    for other tests.

    It is important to use this keyword to free up the resources i.e.
    unnecessary chrome instances are not left to run. However,
    it is good to monitor chromedriver processes as those might be still
    running.

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

        CloseBrowserSession

    Related keywords
    ----------------
    \`CloseAllBrowsers\`, \`CloseBrowser\`, \`OpenBrowser\`
    """
    driver = browser.get_current_browser()
    if driver is not None:
        if _close_remote_browser_session(driver):
            browser.remove_from_browser_cache(driver)
    else:
        logger.info("All browser windows already closed")
Ejemplo n.º 2
0
def close_browser():
    r"""Close current browser.

    This will also close remote browser sessions if open.

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

        CloseBrowser

    Related keywords
    ----------------
    \`CloseAllBrowsers\`, \`CloseRemoteBrowser\`, \`OpenBrowser\`
    """
    driver = browser.get_current_browser()
    if driver is not None:
        _close_remote_browser_session(driver, close_only=True)
        browser.remove_from_browser_cache(driver)

        # Clear browser re-use flag as no original session open anymore
        BuiltIn().set_global_variable('${BROWSER_REUSE}', False)
        driver.quit()
    else:
        logger.info("All browser windows already closed")
Ejemplo 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()))