Share this page

# JavaScript code samples

OpenText Functional Testing for Developers (Selenium) extends the WebDriver API with additional locators and utilities. OpenText Functional Testing for Developers (Selenium) locators are used the same as Selenium locators.

This topic includes code samples for common use cases.

In this topic:

## Define OpenText Functional Testing for Developers's By object

To start using OpenText Functional Testing for Developers (Selenium), define OpenText Functional Testing for Developers's By object by providing it the Selenium By object.

    var WebDriver = require("selenium-webdriver");
    var LeanFTForSelenium = require("leanft-selenium-js-sdk");
    var By = LeanFTForSelenium.By(WebDriver.By);

Back to top

## Use regular expressions in locators

OpenText Functional Testing for Developers (Selenium) extends Selenium by adding an ability to use regular expressions for values passed to native Selenium locators.

Example: passing a regular expression to the name Selenium locator.

Back to top

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();
    driver.get("http://www.google.com");

    var element = driver.findElement(By.name(/^btn/));

    driver.quit();

## Search by attributes

To search for elements by their attributes, use the OpenText Functional Testing for Developers (Selenium) attributes locator.

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();

    driver.get("http://www.google.com");

    var element = driver.findElement(By.attributes({
                                    "class": "gsfi lst-d-f",
                                    "id": "lst-ib"
    }));

    driver.quit();

Back to top

## Search by visible text

To search for elements by their visible text, use the OpenText Functional Testing for Developers (Selenium) visibleText locator.

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();

    driver.get("http://www.google.com/en");

    var element = driver.findElement(By.visibleText("Google Search"));

    driver.quit();

Back to top

## Search by hierarchy

To search for elements by their hierarchy, use the OpenText Functional Testing for Developers (Selenium) chained locator.

The chained locator receives an array of By objects, where each By represents a level in the hierarchy, starting with the highest level.

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();

    driver.get("http://www.google.com");

    // Search for the button with property name="btnK" and a parent element, form, with an id="searchform".
    var element = driver.findElement(By.chained([
                                    By.id("searchform"),
                                    By.name("btnK")]));

    driver.quit();

Back to top

## Search by several locators

To search for an element by combination of several locators, use OpenText Functional Testing for Developers (Selenium) locator By.each, which searches for an element that matches all the provided identification properties (AND operator).

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();

    driver.get("http://www.google.com");

    // Search for the element with name="btnK" and type="submit".
    var element = driver.findElement(By.each([
                                    By.name("btnK"),
                                    By.type("submit")
    ]));

    driver.quit();

Back to top

## Take a snapshot

To take a snapshot of the element, use the OpenText Functional Testing for Developers (Selenium) getSnapshot utility.

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var Utils = LeanFT4S.Utils;
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();

    driver.get("http://www.google.com");

    var element = driver.findElement(By.name("btnK"));

    var snapshot = Utils.getSnapshot(element);

    driver.quit();

Back to top

## Highlight an element

To highlight an element, use the OpenText Functional Testing for Developers (Selenium) highlight utility.

    var webdriver = require("selenium-webdriver");
    var Builder = webdriver.Builder;
    var LeanFT4S = require("leanft-selenium-js-sdk");
    var Utils = LeanFT4S.Utils;
    var By = LeanFT4S.By(webdriver.By);

    var driver = new Builder().forBrowser("chrome").build();

    driver.get("http://www.google.com");

    var element = driver.findElement(By.name("btnK"));

    Utils.highlight(element);

    driver.quit();

Back to top

Tips to improve your search results... OpenText™ Functional Testing for Developers (Selenium) JavaScript SDK All versions OpenText™ Functional Testing for Developers (Selenium) SDK 25.2 All View Local Help Center View Online Help Center Version: