Locating Swing™ Components

Introduction

Locating Swing components in JavaDriver can be done on the JavaDriver instance itself or on a WebElement that is fetched earlier. A WebElement in the context of JavaDriver represents a Swing™ component (or a Java/Fx™ node). JavaDriver bindings exposes findElement and findElements methods. The findElement method returns a WebElement object matching the query, and throws an exception if such an element cannot be found. The findElements returns a list of WebElements, possibly empty if no components match the query.

The “find” methods take a locator or query object called By.

This article discusses the By strategies that are implemented by JavaDriver for accessing Swing™ components.

By Id and Name

Swing™ allows the developers to provide a name to the component by using java.awt.Component#setName method. Since this is set by the developer, it should have good semantics. Developers should avoid providing non-unique names to the components.

This is the preferred way of locating a component.

By ClassName

Class in the context of JavaDriver refers to the Java class the component is derived from. In practical use, there may be many components that derive from the same class and it is prudent to use class name while fetching multiple components.

By TagName

JavaDriver creates a tag name for each of the components using the component class. You can use the tag name to locate a component.

Computing the TagName

The tag name is computed by finding the closest Swing/AWT superclass of the component class and converting it from CamelCase to camel-case. The prefix 'J' for swing components is stripped if needed.
Examples:
java.swingx.JTextField => text-field
java.swingx.JSpnner => spinner

By CSS

Marathon JavaDriver includes an inbuilt CSS Engine that provides locating components using CSS selectors. The CSS is customized to support various selection strategies for Java Swing components.

  1. You can use the tagName with the CSS selector.
  2. You can use attributes to filter the located components. The attribute matcher supports the following operators:
    • = Equals
    • *= Contains
    • ^= Starts with
    • $= Ends with
    • /= Regular expression match
  3. You can also pseudo classes to filter some of the components. JavaDriver supports the following pseudo classes
    • selected
    • enabled
    • displayed
    • hidden
    • instance-of(className)
  4. JavaDriver uses CSS pseudo elements to provide access to sub components within a main component. For example, you can use pseudo element to access a cell from the table. The following list provides the pseudo elements supported by individual components:
    ComponentPseudo ElementReturns
    javax.swing.JComboBoxnth-option(index)
    all-options
    ComboBoxOption
    List<ComboBoxOption>
    javax.swing.JEditorPanetag(name)
      — name is one of 'a', 'ol', 'ul' etc.
    EditorPaneTag
    javax.swing.JListnth-item(index)
    all-items
    ListItem
    List<ListItem>
    javax.swing.JTabbedPanenth-tab(index)
    all-tabs
    selected-tab
    TabbedPaneTab
    List<TabbedPaneTab>
    TabbedPaneTab
    TabbedPaneTabcomponent
    tab-component
    WebElement
    WebElement
    javax.swing.JTableHeadernth-item(index)
    all-items
    WebElement
    List<WebElement>
    javax.swing.JTableheader
    mnth-cell(row, column)
    all-cells
    mnth-cell-editor(row, column)
    JTableHeader
    TableCell
    List<TableCell>
    TableCellEditor
    javax.swing.JTreenth-node(index)
    all-nodes
    root
    JTreeNode
    List<JTreeNode>
    JTreeNode
    JTreeNodeeditorWebElement

Close Menu