I installed ’ Image Comparison using aShot Keywords’ plugin to play with screenshots. I tried to use method ‘takeWebElementScreenshot’ which require the repository object name to take screenshot of and filename to save the screenshot. This is how i am doing it
This is reference from documentation to use this keyword
Blockquote @com.kms.katalon.core.annotation.Keyword static void takeWebElementScreenshot(com.kms.katalon.core.testobject.TestObject object, java.lang.String filename, int timeout = 10, com.kms.katalon.core.model.FailureHandling flowControl = FailureHandling.OPTIONAL)
Take the picture of a specific web element using AShot and save to screenshots folder in current working project Parameters: object - the web element need to take picture filename - the image will be save with the name
Not sure what I am doing wrong which is generating blank screenshots???
The WebUI.waitForElementVisible(TestObject, 3) keyword will NOT throw exception when the timeout expired even if the target element IS NOT visible. The keyword will just write a single warning message. So it is likely you didn’t recognize the failure.
You had better use other keyword that throws Exception when the timeout expired. For example,
The “scaling of screen resolution” really matters when you take a screenshot.
What do I mean “screen resolution”? it is this:
I have ever used AShot and got troubles because I did not mange the “scaling of screen resolution” properly.
I do not know if the ‘Image Comparison using aShot Keywords’ plugin takes care of it. If the plugin does NOT mange the scaling of screen resolution properly, it is quite likely that the screeshot image taken by the plugin becomes something unexpected.
Possibly, the targeted web element IS NOT visible or it IS visible and IS blank after 3 seconds. Therefore you got a blank screenshot.
Its not the case as I used different timeouts and it always passed ‘waitForElementVisible’ action. I also used ‘waitForElementPresent’ keyword with sucess but keeps getting balnk screenshot. The web element I am talking about displays for around 30 seconds and then disappear so I believe this duarion is enough to take a screenshot.
Why not you just try and the built-in keyword, rather than the custom one?
After some attempts with that plugin, now I used the builtin keyword for taking element screenshot but there is no builtin method available for comparison so I used custom keyword of mentioned plugin
Blockquote @com.kms.katalon.core.annotation.Keyword static boolean areMatched(java.lang.String expectedImgPath, java.lang.String actualImgPath, com.kms.katalon.core.model.FailureHandling flowControl = FailureHandling.STOP_ON_FAILURE)
Verify two images are matched or not Returns: Parameters: expectedImgPath - the location of expected image actualImgPath - the location of actual image
which successfully compared two images but there is still one problem. If I ran my test on different screen as compared to the one where I captured my reference screenshot (expected image), it still fail due to different images resolution I guess.
Is there any method available where I can mention difference ratio (expected between images due to different screen) so that during images comparison, this ratio will be considered to avoid test failing on different screen?
Your finding proves that the “Image Comparison using AShot Keyword” does not take the device pixel ratio into account appropriately. Possibly you shouldn’t use it.
I looked at the source code of the plugin
I found this code is problematic.
See the signagure of takeScalingScreenshot keyword:
This has a parameter named dpr, which stands for device pixcel ratio. the dpr parameter is actually used when AShot object is to take screenshot. This is the way ALL of method should follow.
However all other Keyword methods in this plugin doesn’t take device pixcel ratio into account. The author of this program was a bit lazy, I guess. He did not test the code using 2 machines with different screen resolution settings as @zahid.asim did.
Therefore I would conclude that this plugin is incomplete. This will never work appropriately if you use the keyword on 2 machines with different scaling of screen resolution.
You can use the Katalon built-in keyword to take screenshots
You can use the AShot’s ImageDiffer class directly to compare 2 image files
You don’t need the faulty plugin. You can read the source of the plugin, and will find that the plugin does not do much. You don’t need it. Rather, it disturbs you.
The following is my wrapper library of AShot. It fully takes Device Pixcel Ratio taken into account.
My code just support taking screeenshots into images and save it into file while taking DPR into account. It does not implement a method for image comparison, because you don’t need it.
Provided that you get 2 images, then your code can directly call AShot’s image comparison method with the 2 images as params. It’s so easy.