Share this page

# UFT Developer for Selenium Java 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:

## Create a project

To start using UFT Developer for Selenium, create a Java UFT Developer for Selenium test project in Eclipse or IntelliJ IDEA using the UFT Developer 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

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.

    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 UFT Developer for 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 UFT Developer for 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 UFT Developer 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

To take a snapshot of the element, use the UFT Developer 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

To highlight an element, use the UFT Developer 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

Tips to improve your search results... UFT Developer for Selenium SDK (Java) 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: