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.
This article presents a JavaScript solution. Similar solutions can be implemented in other scripting languages supported by Squish. We'll start by implementing a function, which returns true if the current size of the given component isn't less than the return value of getPreferredSize().
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 like to use Google Analytics to get a better understanding of how
you use the website.
By agreeing to this, your usage data will be stored in the USA and processed
by Google LLC. Both Google as well as federal US agencies can access this data
and combine it with any other data about you, such as your search history,
personal accounts or any other data known to Google.