How to use Java Inspector for Swing Desktop Applications
How to use Java Inspector for Swing Desktop Applications
Introduction
Java Inspector is a tool for inspecting Java Swing GUIs. It works in a similar way as developer plugins for HTML browsers but for Java Swing toolkit.
With Swing Explorer you can visually browse through application component hierarchy.
Below is shown how basic Swing application is inspected by Java Inspector.
Download Java Inspector from platform nexus
Please find inspector in rpaplatform repository under:
Starting Java Inspector
To launch the inspector:
java -jar easy-rpa-java-inspector-app-<version>.jar
Inspector is started under default version of java which is subject to your operating system - it can be x64, x86 or other. If you need to inspect the application running under a certain OS architecture then use the appropriate java in command line.
When started, the first screen of Java Inspector displays the list of Java processes. Not all of them can be inspected so it is user responsibility to correctly define a required application.
Consider an example: let's start InputVerification found in Oracle documentation
Now start Java Inspector using startup
Even though InputVerification is a single Java desktop application running on the machine, we still see other java processes. Clearly the one we are in is the first list with title "InputVerificationDemo". Let's go and attach to it
After inspector is attached, the explorer window appears with tree of elements rendered in the left pane
Double click on tree element will display the element inside inspector and single click will highlight it
You can vice-versa select it straight in the display and the element will be automatically highlighted in the tree
Retrieving Element Selector
Say we want our robot to change the value of APR, this is the field we selected on last screen.
It's typical to use element name
or class
to find element, so let's check the Properties panel to see its values
Since we don't have unique name for this element and there are multiple elements on current screen with the same class name, we'll find all the elements and retrieve the one we needed by index
List<JavaElement> elements = driver.findElements(JavaSearch.className("javax.swing.JTextField")); JavaElement loanAmountInput = elements.get(0); JavaElement aprInput = elements.get(1); // this our APR field JavaElement yearsInput = elements.get(2); JavaElement paymentInput = elements.get(3); // now let's change its value aprInput.clear(); aprInput.sendKeys("12.5");