LeanFT for Selenium Java 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.

 

Create a project

To start using LeanFT for Selenium, create a Java LeanFT for Selenium test project in Eclipse or IntelliJ IDEA using the LeanFT plugin. For more details, see Create a Java test project.

The following class is created:

package com.company;

import static org.junit.Assert.*;
				
import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;
import java.util.HashMap;
import java.util.regex.Pattern;

public class SeleniumTest  {

    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {
    }
}

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.

package com.company;

import static org.junit.Assert.*;
				
import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;
import java.util.HashMap;
import java.util.regex.Pattern;

public class SeleniumTest  {

    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name(Pattern.compile("^btn")));

        driver.quit();
    }
}

Back to top

Search by attributes

A LeanFT for Selenium locator.

To search for elements by their attributes.

package com.company;

import static org.junit.Assert.*;
				
import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class SeleniumTest  {

    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

        Map<String, String> attributes = new HashMap<>();
        attributes.put("class", "gsfi lst-d-f");
        attributes.put("id", "lst-ib");

        WebElement element = driver.findElement(By.attributes(attributes));

        driver.quit();
    }
}

 

Back to top

Search by visible text

A LeanFT for Selenium locator.

To search for elements by their visible text.

package com.company;
  
import static org.junit.Assert.*;
				
import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;
import java.util.HashMap;
import java.util.regex.Pattern;
 
public class SeleniumTest  {
 
    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }
 
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }
 
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }
 
    @Before
    public void setUp() throws Exception {
    }
 
    @After
    public void tearDown() throws Exception {
    }
 
    @Test
    public void test() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
 
        WebElement element = driver.findElement(By.visibleText("Google Search"));
        Utils.highlight(element);
 
        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 ByEach, which searches for an element that matches all the provided identification properties (AND operator).

package com.company;

import static org.junit.Assert.*;
				
import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;
import java.util.HashMap;
import java.util.regex.Pattern;

public class SeleniumTest  {

    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

        // Search for the element with name="btnK" and type="submit".
        WebElement element = driver.findElement(new ByEach(
                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.

package com.company;

import static org.junit.Assert.*;
				
import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;

import java.awt.image.RenderedImage;
import java.util.HashMap;
import java.util.regex.Pattern;

public class SeleniumTest  {

    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name("btnK"));
        RenderedImage 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.

package com.company;

import static org.junit.Assert.*;

import com.hpe.leanft.selenium.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.*;
import com.hpe.leanft.selenium.By;
import com.hpe.leanft.selenium.ByEach;

import java.util.HashMap;
import java.util.regex.Pattern;

public class SeleniumTest  {

    public SeleniumTest() {
    //Change this constructor to private if you supply your own public constructor
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name("btnK"));
        Utils.highlight(element);

        driver.quit();
    }
}

Back to top