Skip to main content

Java Driver

Java Driver

This page describes only Java Driver specific features and methods which may be different in other drivers.

Implementation which is common for all drivers is described in Drivers page. Please, read it before.

Java driver automates applications with java UI, that is built from of java.awt.* package. This for example includes Swing, Oracle Forms, JavaFx.

Java Driver consists of EasyRPA driver implementation and java agent

Java agent is a jar package implementing JsonWire protocol. It attaches to target java application and starts http server.

At the moment there are 2 java agents available in the EasyRPA: 

  • easy-rpa-java-agent to automate Swing / Oracle Forms desktop applications
  • easy-rpa-javafx-agent to automate JavaFX desktop applications

Java Driver implementation is a part of engine. It makes use of JsonWire protocol while providing a familiar selenium-like programming interface to developer.


Prepare node for Java Automation

The following manual steps should be done to prepare node for Java automation

  • if you are running Automation process manually, make sure the required java agent jar is in the classpath of the run. Node agent does it automatically.
  • if Automation process attaches to the already running Java desktop application - make sure 'java.policy' grants all permissions for the required java agent jar. When running AP manually, specify the path to the java agent jar from your classpath. For node agent case, you need to refer the required java agent jar that is located in the node nexus repository.
grant codeBase "file:/<Path to a jar directory>/easy-rpa-java(fx)-agent.jar" {
	 permission java.security.AllPermission;
};


  • Java runtime for Automation Process run and Java application you are automating must be of the same Java version. By default node agent uses the same java runtime for AP run as specified for start the itself. To change the java runtime for automation process, you need to define java home parameter in node configuration:

CPU architecture must match: if target application runs under java x64 then AP must be also started under java x64

Driver Initialization


You can initialize the driver in your automation process by the following way:

@Driver
private JavaDriver jDriver;

Driver Params


Param nameMeasureDefault ValueDescription
DriverParams.All.EXPLICITLY_WAIT_TIMEOUT_SECONDSseconds5Wait functions default timeout in seconds.
DriverParams.All.EXPLICITLY_WAIT_POLLING_INTERVAL_MILLISECONDSmilliseconds500Wait functions default pooling interval in milliseconds.

Opening new applications


We have a specific article about opening applications in Java Driver so please refer to How to launch or connect to Java Application.

Browser navigation


getCurrentUrl

returns window (i.e. java top UI container) properties as JSON string

// returns {"title":"InputVerificationDemo","tagName":"window","component.class.name":"javax.swing.JFrame"}
String url = driver.getCurrentUrl();

Window management


getTitle

calls getTitle() on focused java.awt.Frame or java.awt.Dialog

String title = driver.getTitle();

getWindowHandle

returns identity hashcode of java.awt.Window (java top UI container) encoded as hex string

// returns "7abde323"
String handle = driver.getWindowHandle();

getWindowHandles

returns all window handles registered in sun.awt.AppContext

Set<String> handles = driver.getWindowHandle();

calls dispose() on current java.awt.Window

drive.close();

switch to window

Java driver can only switch windows inside a single Java application. To take over another application the one must instantiate one more driver

Java Driver supports switching to window by title or handle

  • switches to window (java top UI container) by its title
driver.switchTo().window("Login Success");
  • successively switches to each window by its  handle
Set<String> windowHandles = getDriver().getWindowHandles();
for (String handle : windowHandles ) {
	driver.switchTo().window(handle);
}

Working with UI Elements


Inspector

To inspect Java applications we suggest using Java inspector tool which is described in How to use Java Inspector for Swing Desktop Applications.

Find elements

The element locators supported by Java Driver:

Selector type
JavaSearch.id
JavaSearch.name
JavaSearch.cssSelector
JavaSearch.className
JavaSearch.tagName

JavaSearch.id and JavaSearch.name search for value from java.awt.Component.getName

// these are the same
driver.findElement(JavaSearch.id("username"));
driver.findElement(JavaSearch.name("username"));

JavaSearch.className searches by fully qualified java class name including all subclasses

// returns the elements of class javax.swing.JPasswordField
driver.findElements(JavaSearch.className("javax.swing.JPasswordField"));

// returns the elements of class javax.swing.JTextField including JPasswordField
// because JPasswordField extends JTextField 
driver.findElements(JavaSearch.className("javax.swing.JTextField"));


EasyRPA implements CSS finder for Java applications. You can use JavaDriver's findElement(s) call to quickly find a component in the application

SelectorDescriptionExample
tagnameSelects an element with the given tagname. A tagname is computed by finding the Swing/AWT superclass of the component and converting CamelCase to camel-case


driver.findElement(JavaSearch.tagName("text-field")); // JTextField
driver.findElement(JavaSearch.tagName("spinner")); // JSpinner
*Gets all the elements, retuns list of elements


driver.findElements(JavaSearch.cssSelector("*"));
.Returns the same element
// tree and tree_1 are same
tree = driver.findElements(JavaSearch.cssSelector("tree"));
tree_1 = tree.findElement(JavaSearch.cssSelector("."));
#nameSelect a component with the given name


// the element has name "loanAmount" set through java.awt.Component#setName
driver.findElement(JavaSearch.cssSelector("#loanAmount"));
[attribute=value]Find components with the given value for the attribute. The operation can be *= (contains), /= (regex match), = (equals), ^= (startswith), $= (endswith).


// finds all the buttons whose text is equal to "Click Me"
driver.findElement(JavaSearch.cssSelector("button[text='Click Me']"));

// finds all the buttons whose text starts with "Click"
driver.findElement(JavaSearch.cssSelector("button[text^='Click']"));
:Find components for which returns true. Pseudoclasses avaiable are selected, enabled, displayed, hidden and instance-of("")


// finds all the text-fields which are enabled
driver.findElement(JavaSearch.cssSelector("text-field:enabled"));
::

find the pseudo elements


  • JComboBox
    • Available pseudoelements are nth-option(index) and all-options.
  • JList
    • Available pseudoelements are nth-item(index) and all-items.
  • JEditorPane
    • Available pseudoelements are tag(name), where name can be a, ol, ul etc.
  • JTabbedPane
    • Available pseudoelements are nth-tab(index), all-tabs and selected-tab.
  • JTableHeader
    • Available pseudoelements are nth-item(index) and all-items.
  • JTable
    • Available pseudoelements are header, mnth-cell(row, column), all-cells and mnth-cell-editor(row, column).
  • JTree
    • Available pseudoelements are nth-node(index), all-nodes, editor and root.

JComboBox

// Returns 2nd option from the combo-box
driver.findElement(JavaSearch.cssSelector("combo-box::nth-option(2)"));
// OR
combobox = driver.findElement(JavaSearch.tagName("combo-box"));
combobox.findElement(JavaSearch.cssSelector(".::nth-option(2)"));

// Returns list of all options from JComboBox driver.findElement(JavaSearch.cssSelector("combo-box::all-options"));

JList

// Returns 4th item from the list
driver.findElement(JavaSearch.cssSelector("list::nth-item(4)"));
// OR
list = driver.findElement(JavaSearch.tagName("list"));
list.findElement(JavaSearch.cssSelector(".::nth-item(4)"));

// Returns list of all the options from the list element
driver.findElement(JavaSearch.cssSelector("list::all-items"));

JEditorPane

editor = driver.findElement(JavaSearch.cssSelector("editor-pane"));
editor.findElement(JavaSearch.cssSelector(".::tag('a')[text='Title Page']"));

JTabbedPane

// Returns selected tab
tabbedPane = driver.findElement(JavaSearch.cssSelector("tabbed-pane"));
tabbedPane.findElement(JavaSearch.cssSelector(".::selected-tab"));

JTableHeader

tableHeader = driver.findElement(JavaSearch.tagName("table-header"));
tableHeader.findElement(JavaSearch.tagName(".::nth-item(3)"));

JTable

table = driver.findElement(JavaSearch.cssSelector("table"));
// To get particular cell
table.findElement(JavaSearch.cssSelector(".::mnth-cell(2, 3)"));
// To get all the cells
table.findElement(JavaSearch.cssSelector(".::all-cells"));
// To get the cell editor
table.findElement(JavaSearch.cssSelector(".::mnth-cell-editor(2, 3)"));

JTree

tree = driver.findElement(JavaSearch.cssSelector("tree"));
// To get particular node
tree.findElement(JavaSearch.cssSelector(".::nth-node(2)"));
// To get all the nodes
tree.findElement(JavaSearch.cssSelector(".::all-nodes"));
// To get the root
tree.findElement(JavaSearch.cssSelector(".::root"));
// To get the editor
tree.findElement(JavaSearch.cssSelector(".::editor"));
More components and selectors that can be used for it both with examples on ruby can be found here: https://marathontesting.com/marathonite-user-guide/java-swing-components/