Share this page

# UFT Developer for Selenium .NET 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 C# UFT Developer for Selenium test project in Visual Studio using the UFT Developer plugin. For more details, see Create a .NET UFT Developer for Selenium project.

The following class is created:

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Support.PageObjects;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace LeanFtSeleniumTestProject1
    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    	{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{

    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

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.

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Text.RegularExpressions;

    namespace LeanFtSeleniumTestProject1
    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    	{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{
    			var driver = new ChromeDriver();
    			driver.Url = "http://www.google.com/en";
    			driver.Navigate();

    			var element = driver.FindElement(By.Name(new Regex("^btn")));

    			driver.Close();
    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

Back to top

## Search by attributes

To search for elements by their attributes, use the UFT Developer for Selenium Attributes locator.

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Collections.Generic;

    namespace LeanFtSeleniumTestProject1
    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    	{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{
    			var driver = new ChromeDriver();
    			driver.Url = "http://www.google.com/en";
    			driver.Navigate();

    			var attributes = new Dictionary<string, object>
    			{
    				{"class", "gsfi lst-d-f"},
    				{"id", "lst-ib"}
    			};

    			var element = driver.FindElement(By.Attributes(attributes));

    			driver.Close();
    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

Back to top

## Search by visible text

To search for elements by their attributes, use the UFT Developer for Selenium VisibleText locator.

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace LeanFtSeleniumTestProject1

    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    		{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{
    			var driver = new ChromeDriver();
    			driver.Url = "http://www.google.com/en";
    			driver.Navigate();

    			var element = driver.FindElement(By.VisibleText("Google Search"));

    			driver.Close();
    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

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).

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace LeanFtSeleniumTestProject1
    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    	{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{
    			var driver = new ChromeDriver();
    			driver.Url = "http://www.google.com/en";
    			driver.Navigate();

    			var element = driver.FindElement(new ByEach(
    			By.Name("btnK"),
    			By.Type("submit")
    			));

    			driver.Close();
    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

Back to top

## Take a snapshot

To take a snapshot of the element, use the UFT Developer for Selenium getSnapshot utility.

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace LeanFtSeleniumTestProject1
    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    	{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{
    			var driver = new ChromeDriver();
    			driver.Url = "http://www.google.com/en";
    			driver.Navigate();

    			var element = driver.FindElement(By.Name("btnK"));
    			var snapshot = element.GetSnapshot();

    			driver.Close();
    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

Back to top

## Highlight an element

To highlight an element, use the UFT Developer for Selenium highlight utility.

    using System;
    using LeanFT.Selenium;
    using By = LeanFT.Selenium.By;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.PageObjects;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace LeanFtSeleniumTestProject1
    {
    	[TestClass]
    	public class LeanFtSeleniumTest
    	{
    		[ClassInitialize]
    		public static void ClassInitialize(TestContext context)
    		{
    		}

    		[TestInitialize]
    		public void TestInitialize()
    		{
    		}

    		[TestMethod]
    		public void TestMethod1()
    		{
    			var driver = new ChromeDriver();
    			driver.Url = "http://www.google.com/en";
    			driver.Navigate();

    			var element = driver.FindElement(By.Name("btnK"));
    			element.Highlight();

    			driver.Close();
    		}

    		[TestCleanup]
    		public void TestCleanup()
    		{
    		}

    		[ClassCleanup]
    		public static void ClassCleanup()
    		{
    		}
    	}
    }

Back to top

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