Locating the Unlocatable elements with logical keywords
recently completed the setup of a new test automation framework for a legacy Swing application. One of the biggest challenges I faced was locating Swing components. These components are created by a proprietary framework that doesn’t allow assigning unique names to them. Writing multiple loops to locate elements wasn’t a feasible option due to the high development and maintenance costs.
To overcome this, I used an alternative approach to locate components logically. The solution involves describing components based on:
1?? The Swing hierarchy
2?? The visual location of the component
Look on the login window below:
For example, to find the JTextField next to a JLabel, I would use the following locator logic:
领英推è
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import static SwingLocator.And;
import static SwingLocator.isShowing;
import static SwingLocator.instanceOf;
import static SwingLocator.hasText;
import static SwingLocator.overlapsHorizontally;
import static ComponentUtils.findComponent; // Explicit static import for findComponent
public class DemoApp {
private JFrame demoApp;
public void setPassword(String password) {
JTextField passwordField = findComponent(
And(
isShowing(),
instanceOf(JTextField.class),
overlapsHorizontally(
isLabelWithText("Password")
)
),
demoApp
);
passwordField.setText(password);
}
private Object isLabelWithText(String text) {
return And(
instanceOf(JLabel.class),
hasText(text)
);
}
}
I was pleasantly surprised by how quickly tests can be created using this approach and how stable they are. Developing this test automation framework from scratch took me three months of dedicated effort.
This also reminded me of my experience years ago working on Selenium tests for an endurance company. I faced a similar problem there with a proprietary framework that made locating elements very challenging. A logical description for locating elements, like the one I’ve used now, would have been incredibly helpful back then!
To analyze the Swing hierarchical structure for this project, I used Swing Explorer, which made the process much more manageable.
It’s been an exciting and rewarding journey to bring automation to a legacy system!
#TestAutomation #Java #Swing #LegacySystems #AutomationFramework
Software Development Engineer
1 个月Can it be apply on the legacy application which is not a Swing application? I am not familiar with the UI setup of the legacy applications. Thanks for sharing.