Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture Fullpage Screenshot #1459

Open
F3licity opened this issue Sep 27, 2019 · 4 comments
Open

Capture Fullpage Screenshot #1459

F3licity opened this issue Sep 27, 2019 · 4 comments

Comments

@F3licity
Copy link

@F3licity F3licity commented Sep 27, 2019

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.

@aaltat
Copy link
Contributor

@aaltat aaltat commented Sep 27, 2019

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.

@pekkaklarck
Copy link
Member

@pekkaklarck pekkaklarck commented Sep 27, 2019

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.
@F3licity
Copy link
Author

@F3licity F3licity commented Dec 3, 2019

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.

@aaltat
Copy link
Contributor

@aaltat aaltat commented Dec 4, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.