Screenshot APIs

Sahi's Screenshot APIs takes the screen/browser screen-shot while running the sahi scripts.

Take Screenshot

Takes a screen shot and embeds the image in the playback log.
Since Sahi works without bringing the window into focus, we first need to call _focusWindow to bring the window into focus.
info Sahi currently takes the full window (desktop) screenshot, not just the browser window
info NOTE: For Desktop Applications only format and resizePercentage properties($props) are supported.
warning
You can skip executing Screenshot APIs in your script, by using _skipScreenShots API or adding _sahi.SKIP_SCREENSHOTS=true.
If Screenshots skipping is enabled, this action will be skipped.
If Screenshots skipping is enabled using Skip Screenshots action, this action will be skipped.

Parameters
$fileSysPathstring optional
file path to copy screenshot in file system.
file path to copy screenshot in file system. Relative path resolves relative to files folder of the current project.
$noLogboolean optionaltrue to prevent embedding the image in the playback log.
Default is false.
$propsobject optionalproperties for image format(format) and image compression in precentage(resizePercentage)

1. format, string, optional, can be GIf, JPG or PNG
Default is "sahi.takeScreenShot_image.format" property defines in config/sahi.properties, this can be overridden in userdata/config/userdata.properties

2. resizePercentage, integer, optional
Default is "sahi.takeScreenShot_image.resize_percentage" property defines in config/sahi.properties, this can be override in userdata/config/userdata.properties
100 means no compression
Return Value

Modes Supported :
Raw Script
_focusWindow(); // bring window into focus
_takeScreenShot();
// captures screenshot in D:\dev\capture.jpg(compresses file size to 75% of the full size with jpg format)
// without embedding image in the playback log
_takeScreenShot("D:\\dev\\capture.jpg", true, {format:'jpg', resizePercentage:75});

Sahi Pro Classic API :_takeScreenShot


Take Screenshots

Takes a screen shot after every step and embeds the image in the playback log.
warning
You can skip executing Screenshot APIs in your script, by using _skipScreenShots API or adding _sahi.SKIP_SCREENSHOTS=true.
If Screenshots skipping is enabled, this action will be skipped.
If Screenshots skipping is enabled using Skip Screenshots action, this action will be skipped.

Parameters
$enableboolean true for enabling screenshots, false to disable
$directorystring optionalfile path directory to copy screenshot in file system
$noLogboolean optionaltrue to prevent embedding the image in the playback log.
Return Value

Modes Supported :
Raw Script
_focusWindow(); // bring window into focus
// Enable screenshots
_takeScreenShots(true);
// perform actions
// screenshot will be taken after each step
// disable screenshots
_takeScreenShots(false);
// screenshots will not be taken now.

Sahi Pro Classic API :_takeScreenShots


Take Page Screenshot

    info NOTE: API supports screenshot for elements with horizontal scrollbar also from Sahi Pro: 9.1.0..
    info NOTE: Default value of trim is changed to false from Sahi Pro: 9.1.0. To make previous version scripts continue as it is, change sahi.screenshot.image.trim property to true in userdata.properties.
    infoNOTE: Point number '6' in $props was added Since Sahi Pro: 6.2.1. For old document Refer here
    info NOTE: For Desktop Applications, only format and resizePercentage properties($props) are supported, other properties($props) are not.
Takes a full page screenshot of a web page by scrolling to the end of the page and embeds the image in the playback log. If the element attribute is specified, a full page screenshot of the browser element is taken, instead of the web page. Since Sahi works without bringing the window into focus, we first need to call _focusWindow to bring the window into focus.
warning
You can skip executing Screenshot APIs in your script, by using _skipScreenShots API or adding _sahi.SKIP_SCREENSHOTS=true.
If Screenshots skipping is enabled, this action will be skipped.
If Screenshots skipping is enabled using Skip Screenshots action, this action will be skipped.

Parameters
$elementHTML DOM element optionalIf specified, a full page screenshot of the element would be taken
$fileSysPathstring optional
File system path to save a copy of the screenshot, additionally to. Relative paths will be resolved relative to current executing script.
File system path to save a copy of the screenshot, additionally to. Relative paths will be resolved relative to files folder of the current project.

Default is null.
$noLogboolean optionalPass true to prevent embedding the image in the playback log.
Default is false.
$propsobject optionalProperties to specify for image delay, scrollLimit, trim, format and image compression in percentage(resizePercentage).

1. delay, integer, optional, time delay between scroll and screenshot in milliseconds.
For long pages, the page is scrolled, individual screenshots are taken and then stitched.
Sometimes images on the page may load slowly on scroll and a wait may be needed between scroll and screenshot.
Default is 100 ms.

2. scrollLimit, integer, optional, height in pixels. Maximum scroll size in pixels for a page/element. Default is 200000.

3. trim, boolean, optional, Trim the image for blank space. Default is false.

4. format, string, optional, can be GIF, JPG or PNG
Default is "sahi.takeScreenShot_image.format" property defined in config/sahi.properties. This can be overridden in userdata/config/userdata.properties

5. resizePercentage, integer, optional
Default is "sahi.takeScreenShot_image.resize_percentage" property defined in config/sahi.properties. This can be overridden in userdata/config/userdata.properties. 100 means no compression.

6. mode, integer, optional
Default is 1.
Set to 2 only if element parameter is specified and element has scrollbar or element is inside a scrolled element.
Return Value

Modes Supported :
Raw Script
_focusWindow(); // Brings window into focus
_takePageScreenShot(); // Captures the full browser page screenshot
_takePageScreenShot(_table("t4")); // Captures the table screenshot

// Captures table screenshot with jpg format, compresses file size to 75% of the full size, saves it to D:\dev\capture.jpg
// without embedding image in the playback log
_takePageScreenShot(_table("t4"), "D:/dev/capture.jpg", true, {format:'jpg', resizePercentage:75});

// Captures full div screenshot by scrolling to the end of the div.
// Use it in cases where a div has its own scrollbar.
_call(_div("myDIV").scrollIntoView()); // scroll the div into view first
_takePageScreenShot(_div("myDIV"), null, null, {mode:2}); // the div itself will be scrolled and screen shots taken

Sahi Pro Classic API :_takePageScreenShot


Skip Screenshots

When enabled, Screenshot APIs namely _takeScreenShot, _takeScreenShots and _takePageScreenShot will be skipped. Capturing screenshot from Callback functions will also be skipped.
When enabled, actions namely Take Screenshot, Take Screenshots and Take Page Screenshot will be skipped. Capturing screenshot from Callback functions will also be skipped.

Parameters
$enableboolean True to skip screenshots, false to disable.
Return Value

Modes Supported :
Raw Script
// Skip capturing screenshots
_skipScreenShots(true);

_focusWindow();
_takeScreenShot(); // This will be skipped

// Enable capturing screenshots
_skipScreenShots(false);
// Now, screenshot APIs will not be skipped.

Sahi Pro Classic API :_skipScreenShots


Skip Screenshots Comparison

When enabled, APIs that compare screenshots will be skipped. Thus, APIs namely _assertSnapShot, _assertEqualImages and _compareImages will be skipped.
When enabled, action Compare Images that compares screenshots will be skipped.

Parameters
$enableboolean True to skip comparing screenshots, false to disable.
Return Value

Modes Supported :
Raw Script
// Skip comparing screenshots
_skipScreenShotsComparison(true);

_focusWindow();
_takePageScreenShot(null, "D:/dev/images/login.png")

// Below line will be skipped
var $same = _compareImages("snapshot_login_page.png", "D:/dev/images/login.png");

// Enable comparing screenshots
_skipScreenShotsComparison(false);

Sahi Pro Classic API :_skipScreenShotsComparison




Compare Images

Takes the path of the 2 images and checks if both images are same or not.
infoThis action is dependent on GraphicsMagick software.
To configure the GraphicsMagick on the system, refer Configuring GraphicsMagick
warning
This action can be skipped using _skipScreenShotsComparison API or adding _sahi.SKIP_ASSERT_SNAPSHOTS=true.
This action can be skipped using Skip Screenshots Comparison action.

Parameters
$f1string
File path of the first image. Relative paths will be resolved relative to current executing script.
File path of the first image. Relative paths will be resolved relative to files folder of the current project.
$f2string
File path of the second image. Relative paths will be resolved relative to current executing script.
File path of the second image. Relative paths will be resolved relative to files folder of the current project.
$differenceThresholdfloat optionalIf the difference between first and second image is lesser than or equal to the differenceThreshold number, they are considered to be same images.
Default is 20.
Return Value
booleantrue if both images are same, else false

Modes Supported :
Raw Script
_navigateTo("http://sahi.co.in/demo/training");
_takePageScreenShot(null, "D:/dev/images/login.png")
var $same = _compareImages("snapshot_login_page.png", "D:/dev/images/login.png");
_assert($same);

Sahi Pro Classic API :_compareImages


Configuring GraphicsMagick

To configure GraphicsMagick, follow the following steps.

Configure GM on Windows

Configure GM on Linux

Configure GM on Mac

Skip Screenshots

Screenshot capturing can be skipped using _skipScreenShots API. With this, Screenshot APIs namely _takeScreenShot, _takeScreenShots and _takePageScreenShot will be skipped. Capturing screenshot from Callback functions will also be skipped.
Screenshot capturing can be skipped using Skip Screenshots action. With this, actions namely Take Screenshot, Take Screenshots and Take Page Screenshot will be skipped. Capturing screenshot from Callback functions will also be skipped.

Screenshot comparison can be skipped using _skipScreenShotsComparison API. With this, APIs that compare screenshots will be skipped. Thus, APIs namely _assertSnapShot, _assertEqualImages and _compareImages will be skipped.
Screenshot comparison can be skipped using Skip Screenshots Comparison action. With this, action Compare Images that compares screenshots will be skipped.

To skip screenshots in report on aborted, set property reports.skip_screenshots_on_aborted as true in userdata.properties file.

To skip screenshots in report on healed, set property reports.skip_screenshots_on_healed as true in userdata.properties file.

Notes