.NET 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.
Create a project
To start using OpenText Functional Testing for Developers (Selenium), create a C# OpenText Functional Testing for Developers (Selenium) test project in Visual Studio using the UFT Developer plugin. For more details, see Create a .NET OpenText Functional Testing for Developers (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() { } } }
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.
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() { } } }
Search by attributes
To search for elements by their attributes, use the OpenText Functional Testing for Developers (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() { } } }
Search by visible text
To search for elements by their attributes, use the OpenText Functional Testing for Developers (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() { } } }
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).
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() { } } }
Take a snapshot
To take a snapshot of the element, use the OpenText Functional Testing for Developers (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() { } } }
Highlight an element
To highlight an element, use the OpenText Functional Testing for Developers (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() { } } }