This is a simple Katalon Studio project for demonstration purpose. You can clone this out onto you PC and run it using your Katalon Studio.
This demo was originally developed with Katalon Studio version 5.4.2, tested ok with v7.9.1.
I wanted to take an screenshot of a Web page. The target page was supposed to have the height over 10,000px.
I wanted to take screenshots of a web page in the Development environment and the Production Environment, and compare how much different they are. If a significant difference is found exceeding a certain criteria (e.g, diff > 3.0%), then I want my test to fail to notify developers of unexpected change found.
I tried Katalon's built-in WebUI.takescreenshot()
. I found, it can only take visual portion; it can not take the image of entire page.
By searching I found a Java Library called AShot.
WebDriver Screenshot utility
- Takes a screenshot of a WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser)
- Decorates screenshots
- Provides flexible screenshot comparison
I was impressed with the simplicity of the sample code of capturing the entire page by AShot. So I tried AShot in Katalon Studio.
I have found AShot works like a charm.
- clone this git repository onto your PC
- The jar of AShot is avaiblable at the Maven Repository.
ashot-1.5.4.jar
file is already included in the project'sDrivers
folder. - run Test Cases. The output PNG images will be written into
Screenshots
directory.
Execute [Test Suites/TS1_screenshot_taken_when_failed]
. It will call Test Cases/TC1_ks-built-in_screenshot_taken. This TestCase will fail intentionally. As this project is configured to take screenshot when execution failed (Project > Settings > Execution), Katalon Studio will automatically take a screenshot and save an image into the Reports folder. The HTML reports will show the image included.
Execute Test Cases/TC2_viewport_screenshot
. This demonstrates how to use the WebUI.takeScreenshot()
keyword which is a built-in Katalon Studio. This keyword takes an image of the viewport, not entire page.
Execute 'Test Cases/TC3_save_entire_page_image'. This demonstrates how to take entire page screenshot and save image into file. This uses a custom keyword: 'my.AShotWrapper', which is a thin wrapper of AShot. Another custom keyword 'my.DevicePixelRatioResolver' is used to detect Device-Pixel-Ratio of the current platform.
A sample output -> Screenshots/TC3_save_entire_page_image.png
Execute 'Test Cases/TC4_imageDiffer. This takes screenshots of a pair of URLs (almost similar but are different) to save them into files:
This compares 2 images and measure how much these are different. The difference is calculated to a figure. When the difference larger than the given criteria (e.g, 3.0 %), the test fails.
Sample output:
Here is an example web page, which has extremely large "height".