Share this page

# UFT Developer for Selenium JavaScript code samples

UFT Developer for Selenium extends the WebDriver API with additional locators and utilities. UFT Developer for Selenium locators are used the same as Selenium locators.

This topic includes code samples for common use cases.

In this topic:

## Define UFT Developer's By object

To start using UFT Developer for Selenium, define UFT Developer'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

UFT Developer for 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 UFT Developer for 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 UFT Developer for 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 UFT Developer for 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 UFT Developer for 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 UFT Developer for 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 UFT Developer for 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... UFT Developer for Selenium SDK (JavaScript) Reference Version 15.0-15.0.2 UFT Developer SDK Reference All View Local Help Center View Online Help Center back Go back in browser history forward Go forward in browser history Remove search highlights next topic Go to next topic previous topic Go to previous topic Print the current topic Version: