Open topic with navigation

# LeanFT for Selenium JavaScript code samples

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

This topic includes code samples for common use cases.

In this topic:

## Define LeanFT's By object

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

LeanFT 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 LeanFT 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 LeanFT 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 LeanFT 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 LeanFT 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 LeanFT 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 LeanFT 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