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.

 

Define LeanFT's By object

To start using LeanFT for Selenium, you need to 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

A LeanFT for Selenium locator.

To search for elements by their attributes.

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

A LeanFT for Selenium locator.

To search for elements by their visible text.

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

A LeanFT for Selenium locator.

To search for elements by their hierarchy, you can 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

A LeanFT for Selenium locator.

To search for an element by combination of several locators, you can 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

A LeanFT for Selenium utility.

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

A LeanFT for Selenium utility.

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