How can I create a screenshot of the browser with QF-Test?

983 Views Asked by At

QF-Test allows me to take a screenshot of the whole desktop with:

from imagewrapper import ImageWrapper
iw = ImageWrapper(rc)
screenshot = iw.grabScreenshot()

How can I take a screenshot of only the browser?

2

There are 2 best solutions below

0
On

Use the DomNode API to get the geometry of a node in the DOM. You can use those to specify which are should be included in the screenshot:

from imagewrapper import ImageWrapper
import os.path

iw = ImageWrapper(rc)

# Get root node of the DOM
doc = rc.getComponent("genericDocument")
root = doc.getRootElement()
# Get the geometry (x,y,width,height)
geometry = root.getLocationOnScreen()
# Use the geometry to limit the screenshot
screenshot = iw.grabScreenshot(*geometry)

# Optional: Save the screenshot on disk.
# Use a gloabl variable to set the folder where screenshots should be collected
folder = rc.lookup("screenshotFolder") 
filename = os.path.join(folder, "name.png")
rc.logMessage('Saving screenshot as %s' % filename, report=True)
iw.savePng(filename, screenshot)

# Optional: Add screenshot to the log
rc.logImage(screenshot, "Login Screen", report=True)
1
On

For the sake of completeness:

  1. If you are testing a Java application you can also use the grabImage function. This function works even when the component is hidden by another window. Unfortunately (until now), grabImage doesn't work for Web.

    from imagewrapper import ImageWrapper
    iw = ImageWrapper(rc)
    image = iw.grabImage(rc.getComponent("myComponentId")) # this methods also has some optional/additional parameters. In the qftest script editor, type iw. and then press Ctrl+Space to see the full "documentation".
    rc.logImage(image)
    
  2. When using the script from Aaron Digulla, keep in mind, that the SUT window has to be in the foreground/visible on the screen. There are some options to ensure this [for example: "Raise SUT windows automatically" + event] (See: http://www.qfs.de/qftest/manual/en/opt_play.html). Keep in mind, that you can also change these options during runtime (See: http://www.qfs.de/qftest/manual/en/user_scripting.html#usec_scripting_options).