Skip to content

Extending SwingLibrary

Janne Härkönen edited this page Sep 14, 2011 · 1 revision

Extending SwingLibrary

SwingLibrary keywords cover almost all standard Swing components. However, if the tested application contains custom Swing components, a need to extend SwingLibrary arises.

There are two basic approaches:

  1. Create an independent custom library, in which case both the SwingLibrary and the custom library need to be imported
  2. Extend the SwingLibrary directly, in which case only one library import is needed.

Creating independent custom library

It is perhaps easiest to use JavalibCore as a starting point. All the normal rules of writing test libraries apply. Note that it is possible to import SwingLibrary classes in a custom test library, since SwingLibrary will be available during runtime anyway.

Extending SwingLibrary

It's easy just subclass org.robotframework.swing.SwingLibrary and pass a keyword pattern to the superclass:

package com.acme;

import org.robotframework.swing.SwingLibrary;

public class SwingLibraryPlusMyCustomKeywords extends SwingLibrary {
    public SwingLibraryPlusMyCustomKeywords() {
        super("com/some/own/keyword/*.class");
    }
}

The custom keyword classes must be inside com.some.own.keyword package.

It is also possible to provide multiple keyword patterns if required:

package com.acme;

import org.robotframework.swing.SwingLibrary;
import java.util.ArrayList;
import java.util.List;

public class SwingLibraryPlusMyCustomKeywords extends SwingLibrary {
    private static List<String> PATTERNS = new ArrayList<String>() {{
        add("com/some/own/keyword/*.class");
        add("some/other/package/**/keyword/**/*.class");
    }};

    public SwingLibraryPlusMyCustomKeywords() {
        super(PATTERNS);
    }
}

Creating keywords for custom components

SwingLibrary uses jemmy as it's internal driver. You should be aware of couple of concepts if you are creating your own keywords using jemmy.

Operators

All the GUI operations are done through operators. They are responsible for finding the component under operation and for the method invocations going through the Event dispatching thread.

In jemmy you usually create your operator by providing the context from which the component is to be looked for and some kind of identifier for choosing the right component. This can be an index or a name. You can also provide a chooser, a class that does some additional checking for finding your component.

Context

As mentioned above the operators need a context from which they try to find the component under operation. In SwingLibrary the context is set by using for example Select Window or Select Dialog keywords. Your keyword can access that context with org.robotframework.swing.context.Context.getContext().

Example

Creating Disable Text Component keyword:

package com.some.own.keyword;

import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.swing.context.Context;

import org.netbeans.jemmy.operators.JTextComponentOperator;
import org.netbeans.jemmy.operators.ContainerOperator;

@RobotKeywords
public class MyKeywords {
    @RobotKeyword("Disables the given textfield.\n")
    @ArgumentNames({"textFieldName"})
    public void disableTextComponent(String textFieldName) {
        ContainerOperator context = (ContainerOperator) Context.getContext();
        JTextComponentOperator operator = new JTextComponentOperator(context, textFieldName);
        operator.setEnabled(false);
    }
}