Swinger swinger = Swinger.getUserWith( topLevelComponent );
Or, if you do not have a top-level component available:
// will use the first JFrame returned by java.awt.Window.getWindows() as the topLevelComponent
Swinger swinger = Swinger.forSwingWindow();
All examples use the clickOn
method, but any other Swinger
method that takes a String selector could be used
swinger.clickOn( "component-name" );
Qualified usage:
swinger.clickOn( "name:component-name" );
swinger.clickOn( "type:JButton" )
swinger.clickOn( "type:javax.swing.Box" )
swinger.clickOn( "text:My Button's Label" )
The text selector also works with JTree
and JTable
.
swinger.clickOn( "text:A JTree Node" )
.clickOn( "text:A JTable Header or Cell" )
Note:
The getAt
throws a RuntimeException
if nothing is found.
getAll
returns all components matching the selector, or an empty list if none is found.
Component c = swinger.getAt( "component-name" );
List<Component> cs = swinger.getAll( "component-name" );
JButton b = swinger.getAt( JButton.class );
Component c = swinger.getAt( "type:ComponentType" );
Component c = swinger.getAt( "type:complete.path.ComponentType" );
List<JButton> cs = swinger.getAll( JButton.class );
Component c = swinger.getAt( "text:A Component Text" );
List<Component> cs = swinger.getAll( "text:Some Components Text" );
Matching any:
import static com.athaydes.automaton.selector.StringSelectors.matchingAny;
// throws an Exception if nothing is found
Component c = swinger.getAt( matchingAny( "text:colors", "text:sports" ) );
// finds all components matching the selector, or an empty list if nothing is found
List<Component> nodes = swinger.getAll( matchingAny( "text:colors", "text:sports" ) );
// uses `getAt` to find the component to clickOn
swinger.clickOn( matchingAny( "text:colors", "text:sports" ) );
Matching all:
import static com.athaydes.automaton.selector.StringSelectors.matchingAll;
// throws an Exception if nothing is found
Component c = swinger.getAt( matchingAll( "type:JButton", "text:sports" ) );
// finds all components matching the selector, or an empty list if nothing is found
List<Component> nodes = swinger.getAll( matchingAll( "type:JButton", "text:sports" ) );
// uses `getAt` to find the component to clickOn
swinger.clickOn( matchingAll( "type:JButton", "text:sports" ) );
Swinger swinger = Swinger.getUserWith( frame );
JTree tree = swinger.getAt( JTree.class );
List<Component> nodes = SwingUtil.collectNodes( tree, "colors", "red" );
swinger.doubleClickOn( nodes );
Works the same way as the byText
selector.
import static com.athaydes.automaton.assertion.AutomatonMatcher.hasText
assertThat( myComponent, hasText( "Some text" ) );
Extends hasText
to also work with anything that might have a value (uses the following methods to find a Component's value: getValue
, getText
, getSelected
, getColor
).
import static com.athaydes.automaton.assertion.AutomatonMatcher.hasValue
assertThat( myComponent, hasValue( "Some text" ) );
assertThat( myColorPicker, hasValue( Color.BLUE ) );
Check if a component is selected (usually used with the Hamcrest is
matcher).
import static com.athaydes.automaton.assertion.AutomatonMatcher.selected
assertThat( myComponent, is( selected() ) );
Check if a component is visible (usually used with the Hamcrest is
matcher) as reported by the component's isVisible
method. This is NOT the same as showing
(see below).
import static com.athaydes.automaton.assertion.AutomatonMatcher.visible
assertThat( myComponent, is( visible() ) );
Check if a component is showing on the screen (usually used with the Hamcrest is
matcher) as reported by the component isShowing
method.
import static com.athaydes.automaton.assertion.AutomatonMatcher.showing
assertThat( myComponent, is( showing() ) );
You may have noticed that the Automaton
intentionally uses the method getAt
, which has special syntax in Groovy, to find elements. So anywhere you see getAt
, you can use instead the []
operator:
// by type
JButton b = swinger[ JButton ]
// by name
def c = swinger[ 'component-name' ]
// by text
def d = swinger[ 'text:Something' ]
// complex selectors
def e = swinger[ matchingAny( 'click-me', 'text:Click me' ) ]
def f = swinger[ matchingAll( 'type:JButton', 'text:Click me' ) ]