Share this page

# Java 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:

## Create a project

To start using OpenText Functional Testing for Developers (Selenium), create a Java test project in Eclipse or IntelliJ IDEA using the relevant UFT Developer plugin. For more details, the OpenText Functional Testing for Developers Help Center.

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

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.

    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

To search for elements by their visible text, use the OpenText Functional Testing for Developers (Selenium) attributes 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.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

To search for elements by their visible text, use the OpenText Functional Testing for Developers (Selenium) visibleText 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.visibleText("Google Search"));
            Utils.highlight(element);

            driver.quit();
        }
    }

Back to top

## Search by several locators

To search for an element by combination of several locators, you can use OpenText Functional Testing for Developers (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

To take a snapshot of the element, use the OpenText Functional Testing for Developers (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

To highlight an element, use the OpenText Functional Testing for Developers (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

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