Skip to content

Latest commit

 

History

History
186 lines (140 loc) · 4.87 KB

swing-cheat-sheet.md

File metadata and controls

186 lines (140 loc) · 4.87 KB

Swing Automaton Cheat-sheet

Creating a Swinger

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();

Default Swinger Selectors

All examples use the clickOn method, but any other Swinger method that takes a String selector could be used

By name

swinger.clickOn( "component-name" );

Qualified usage:

swinger.clickOn( "name:component-name" );

By type

swinger.clickOn( "type:JButton" )
swinger.clickOn( "type:javax.swing.Box" )

By text

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" )

Finding Components

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.

By name

Component c = swinger.getAt( "component-name" );
List<Component> cs = swinger.getAll( "component-name" );

By type

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 );

By text

Component c = swinger.getAt( "text:A Component Text" );
List<Component> cs = swinger.getAll( "text:Some Components Text" );

Using Complex matchers

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" ) );

Opening nodes in a JTree

Swinger swinger = Swinger.getUserWith( frame );

JTree tree = swinger.getAt( JTree.class );
List<Component> nodes = SwingUtil.collectNodes( tree, "colors", "red" );
swinger.doubleClickOn( nodes );

Automaton Matchers

hasText

Works the same way as the byText selector.

import static com.athaydes.automaton.assertion.AutomatonMatcher.hasText

assertThat( myComponent, hasText( "Some text" ) );

hasValue

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 ) );

selected

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() ) );

visible

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() ) );

showing

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() ) );

For Groovy users

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' ) ]