Open topic with navigation

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

In this topic:

## Create a project

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

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.

    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 LeanFT 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 LeanFT 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 LeanFT 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 LeanFT 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 LeanFT 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