Exploring the API

The basic idea behind the API is that you use the Center class to login, switch roles, open and close tabs, and log out. You then use the Tab instance returned by Center methods to interact with your page. Another class, UIContext, has several wait and other utility methods available. We'll talk about these in more detail below after discussing the Selenium API that Platform's API is built on.

There are two packages that make up the entire Platform Selenium API: com.onenetwork.platform.tools.test.ui which holds Center and UIContext, and com.onenetwork.platform.tools.test.ui.components which holds all the various Components used in your ExtJS pages.

Selenium API

Platform's API is built on top of Selenium's WebDriver API, which consists of 2 main classes. They are useful to know because Platform's API may be lacking in some areas and you may have to use the Selenium API directly to achieve what you want.


  • WebDriver: This is responsible for opening the browser, navigating to urls, and searching for elements in the DOM. It only searches within one document at a time, so if you want to interact with elements in an iframe, you must switch to that context using the switchTo() method.

  • WebElement: This class represents a DOM element in Selenium, and has methods for searching child elements, clicking, simulating typing, and more.

There is a problem in Selenium with searching under iframe; to search for an element under an iframe, you'll first switch to that context using WebDriver.switchTo() and then execute WebDriver.findElement(), instead of WebElement.findElement() on the iframe.

CommandCenter

This class is responsible for initiating the Selenium test when you call the static login method. Internally, it will construct a UIContext (unless you pass in your own - more on that later) and WebDriver instances.

There are several properties read in from a CommandCenter.properties file that can be used to customize test execution. The file should be located under apps/<your module>/test-src and can have the following properties:

  • web.protocol: http or https. Defaults to http.

  • server.host.name: Defaults to localhost.

  • CommandCenter.width: Browser window will be resized to this width after login.

  • CommandCenter.height: Browser window will be resized to this height after login.

  • ui-test.browser: The browser to use for the test. Possible values are internetExplorer, firefox or chrome. Defaults to internetExplorer.

  • selenium.timeouts.implicitlyWait: Will be set on WebDriver.Timeouts.implicitlyWait(long, TimeUnit) before login. Value should be provided in seconds.

  • selenium.timeouts.scriptTimeout: Will be set on WebDriver.Timeouts.setScriptTimeout(long, TimeUnit) before login. Value should be provided in seconds.

UIContext

This class is best described as the "backbone" for Platform's API. An instance is created internally when logging in with CommandCenter and it is passed around internally and used for its utility methods.

Internally, it stores a map of xtype to Component, because Platform's Selenium API has a class to represent most Ext components. UIContext can be subclassed and its internal map can be overridden so that xtypes point to any replacement Component you may want to use to provide additional functionality. To see the current map of xtypes to Java classes, check the Javadocs for UIContext.

It has a few methods which might be useful to you:

  • waitUntilElementExists: This will wait until a WebElement exists before returning. It takes as input a By instance, which allows you to search the DOM in several different ways. We generally use XPath most often. Example:

    // Using "//" will search globally in the current document while ".//" will search under the provided SearchContext.
    WebElement myElem = uiContext.waitUntilElementExists(searchCtxt, By.xpath(".//div/span/a"));
  • xPathContainsClass: This is intended to be used in conjunction with a By.xpath method call, helping you search for an element with a specific class. Example:

    String xpathStr = ".//div[" + UIContext.xpathContainsClass("my-class") + "]";
    WebElement myElem = uiContext.waitUntilElementExists(searchCtxt, By.xpath(xpathStr));
  • waitUntil: This takes in an ExpectedCondition instance and allows you to specify the condition in which to return from the call. Example:

    // Waits until the field is present on the create page.
    final Tab tab = cc.openTabFromMenu("Advanced Components", "Templates", "Create Task");
    TextField field = uiContext.waitUntil(new ExpectedCondition<TextField>() {
    @Override
    public TextField apply(WebDriver d) {
    return ((TextField) tab.getFieldByLabel("Name"));
    }
    });

There is a waitFor method which will wait for a specified number of milliseconds, but it's recommended to use the waitUntil* methods because they wait for the page to reach a certain state and, as a result, are more precise and less likely to cause sporadic timing issues in your tests.

Tab and Container

Once you have a Center instance you can open a Tab. This is the main class you will interact with because it represents a tab in NEO and has several methods to fetch different kinds of components from your page. Most of these methods are actually on a parent class called Container, which is essentially a Platform wrapper for WebElement.

  • getX: This is really a set of methods that start with "get" and which fetch a component under the current context. Some examples are getButton(), getFieldByLabel, and getReport. An exhaustive list is available in the Javadocs for Container.

  • clickMenuItem: This method will click a menu item that's under a button. It takes a variable number of Strings, the first of which being the button text. Example:

    // In an actionable report:
    report.clickMenuItem("Actions", "Update Items");
  • waitForSuccessMessage: This will wait until the page has a success message. It's useful for ensuring a page doesn't contain errors after submitting to the server.

Grid

The Grid class represents an Ext.grid.EditorGridPanel and has a few basic methods for getting and setting cell values:

  • getCellValue: Returns the text within the cell as a String.

  • startEditing: This is used to start editing on a cell within an editable grid. It is used in conjunction with getEditor and stopEditing. Example:

    ((TextField) grid.startEditing(0, "Name").getEditor()).setValue("John Doe");
    ((BooleanField) grid.startEditing(0, "Male").getEditor()).setValue(true);
    grid.stopEditing();

To find more methods of these components and other components, please check out the Javadocs.