Application layout validation is a very difficult task no matter the test automation tool. Some level of layout validation can be achieved even using tools which verify object properties and don’t rely on screenshot comparison. For Java applications it can be done by analyzing the return value of the getPreferredSize() method of the Component and JComponent classes. This value holds the size the component prefers do be rendered in.
Next, we’ll define a wrapper for above function, which calls the Squish API functions test.pass or test.fail depending on the isSufficientlySized result. This allows us to check the Squish Results for which components were analyzed and the outcome of the analysis.
Next we’ll create a function which generates a list of all components contained in the given frame or window. Function fetchAllComponents(obj) recursively traverses all components whose obj is an ancestor.
Finally, we’ll implement the verifyDialogLayout(dlg, output) function, which is called directly from the test case. It takes two arguments: main component object reference (frame,window) and image filename, where the screenshot of the main component is stored in the event the case validation for correct layout is not a positive value. All components whose size is not sufficient will be drawn with a red border on the captured screenshot. This is done by creating an overlay of suitably shaped JDialog instances.
To present this solution in action we use the Java Swing application AddressBook. First, we need to make some components smaller than they should be. To do that we implement function messWithSizes(), which sets incorrect size for some components.
Additionally, the screenshot was generated with failed components marked as follows:
We would love to improve this site! For this, we ask you for permission to use
a cookie (a piece of data stored in your web browser) which helps us understand
how the site is used.
Sure, go ahead!
Please refer to our Privacy
Policy in case you change your mind, or if you want to learn more about how
we take care of your privacy.