SeleniumLibrary icon indicating copy to clipboard operation
SeleniumLibrary copied to clipboard

Capture Fullpage Screenshot

Open F3licity opened this issue 5 years ago • 5 comments

This is not a bug, but rather a new feature. Often the browser page is longer that is shown and the user needs to scroll down to see the rest of the page.

Robot Framework often takes a screenshot on failure, but the screenshot captured only shows half of the page and might not include the problematic area. It would be nice to have a keyword that captures a whole length image.

This is my python script to implement such functionality

def capture_fullpage_screenshot(url):
    """ Creating a Full-Page screenshot.

    Use this keyword for debugging purposes.
    Creates an image called full_page_screenshot.png.
    """
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("enable-automation")
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-gpu")
    driver = webdriver.Chrome(options=options)
    current_window = driver.get_window_size()

    driver.get(url)

    time.sleep(12)

    height = driver.execute_script("return document.body.scrollHeight")
    driver.set_window_size(1920, height)
    driver.save_screenshot("full_page_screenshot.png")

    driver.set_window_size(current_window["width"], current_window["height"])

However, it is not yet properly working, as it does not work inside docker. And it has a hardcoded file name.

F3licity avatar Sep 27 '19 12:09 F3licity

Well, I was leaning to the Selenium API enhancement direction and make use of that. The Selenium Firefox has: get_full_page_screenshot_as_file which should work out of the box with Selenium 4. I am against doing this with the implementation provided in the first comment.

aaltat avatar Sep 27 '19 13:09 aaltat

This functionality would be nice, but I see various problems in the proposed implementation:

  • Not sure would the newly opened browser see the exact same page than the original one. The new browser would also miss possible custom configuration that the original had.
  • 12 second sleep.

pekkaklarck avatar Sep 27 '19 13:09 pekkaklarck

I just wanted to let you know that I have amended the above script so that is useful for me in my project.

This is the solution I am currently using:

def capture_fullpage_screenshot(url):
    """ Creating a Full-Page screenshot.

    Use the already running selenium driver and create a screenshot.
    It works when running headless.
    Aim of this library is to be used for debugging.
    """
    seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')

    current_window = seleniumlib.driver.get_window_size()

    seleniumlib.driver.get(url)

    time.sleep(4)

    height = seleniumlib.driver.execute_script("return document.body.scrollHeight")
    seleniumlib.driver.set_window_size(1920, height)

    ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    screenshotpath = os.path.join(os.path.sep, ROOT_DIR, 'reports' + os.sep)

    now = datetime.now()
    timestamp = now.strftime("%H%M%S")

    BuiltIn().log_to_console(screenshotpath + "full_page_screenshot_{}.png".format(timestamp))
    seleniumlib.driver.save_screenshot(
            screenshotpath + "full_page_screenshot_{}.png".format(timestamp)
    )
    time.sleep(4)

    seleniumlib.driver.set_window_size(current_window["width"], current_window["height"])

It is still not sufficient to be integrated to the Selenium Library I am afraid. I wouldn't know how to remove the sleeps, for example, as the time is needed for the resizing of the window. Having said that, I should also say that this version makes use of the already open session.

F3licity avatar Dec 03 '19 10:12 F3licity

Knowing when to webapp is ready is actually quite difficult problem and therefore removing the sleep may require application specific logic. I would still lean to use the Selenium API and make an implementation based on that, even when it works only with Firefox. But it's good that you got your keyword improved and I bet you learned lot of things.

aaltat avatar Dec 04 '19 06:12 aaltat

I've created PR #1762, it adds a new keyword Capture Full Page Screenshot. It also add a boolean parameter full_screen to Capture Page Screenshot.

kumy avatar Dec 12 '21 21:12 kumy