id | title | hide_title | sidebar_label | description | keywords | url | site_name | slug | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
csharp-with-selenium |
C# with Selenium:Tutorial to run your first test on Lambdatest |
true |
C# |
You can now run C# Selenium automated test cases on online scalable LambdaTest Selenium grid of 3000+ real desktop browsers and real operating systems. |
|
LambdaTest |
c-with-selenium-running-c-automation-scripts-on-lambdatest-selenium-grid/ |
In this topic, you will learn how to configure and run your C# automation testing scripts on LambdaTest Selenium cloud platform.
By the end of this topic, you will be able to:
- Set up an environment for testing your hosted web pages using C# with Selenium.
- Understand and configure the core capabilities required for your Selenium test suite.
- Test your locally hosted pages on LambdaTest platform.
- Explore advanced features of LambdaTest.
Note: All the code samples in this documentation can be found in the LambdaTest's Repository on GitHub. You can either download or clone the repository to quickly run your tests. <img loading="lazy" src={require('../assets/images/icons/github.png').default} alt="Image" className="doc_img"/> View on GitHub
Before you can start performing C# automation testing with Selenium, you would need to:
- Download and install Selenium WebDriver from its official website.
- Make sure you have the latest version of C#.
- .Net framework to deliver guidelines while developing a range of application using C#.
- Download Selenium WebDriver Language Binding for C# and extract them to appropriate folder. A .NET Core SDK of 2.1 or greater version.
- To execute tests on Linux or macOS, install .NET Runtime.
Clone the LambdaTest CSharp Selenium GitHub repository and navigate to the code directory.
git clone https://github.com/LambdaTest/CSharp-Selenium-Sample
cd CSharp-Selenium-Sample
Make sure you have your LambdaTest credentials with you to run C# automation scripts. You can obtain these credentials from the LambdaTest Automation Dashboard or by your LambdaTest Profile.
Set LambdaTest Username and Access Key in environment variables.
For Linux/macOS:
export LT_USERNAME="YOUR_USERNAME" export LT_ACCESS_KEY="YOUR ACCESS KEY"
For Windows:
set LT_USERNAME="YOUR_USERNAME" set LT_ACCESS_KEY="YOUR ACCESS KEY"
The following C# Selenium script tests a sample to-do list app by marking couple items as done, adding a new item to the list and finally displaying the count of pending items as output.
using System;
using OpenQA.Selenium;
using static System.Environment;
using OpenQA.Selenium.Safari;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace csharp_selenium_lambdatest
{
class SingleTest
{
public static void execute()
{
// Update your lambdatest credentials
String LT_USERNAME = GetEnvironmentVariable("LT_USERNAME");
String LT_ACCESS_KEY = GetEnvironmentVariable("LT_ACCESS_KEY");
IWebDriver driver;
ChromeOptions capabilities = new ChromeOptions();
capabilities.BrowserVersion = "latest";
Dictionary<string, object> ltOptions = new Dictionary<string, object>();
ltOptions.Add("username", LT_USERNAME);
ltOptions.Add("accessKey", LT_ACCESS_KEY);
ltOptions.Add("platformName", "Windows 10");
ltOptions.Add("project", "Demo LT");
ltOptions.Add("build", "C# Build");
ltOptions.Add("sessionName", "C# Single Test");
ltOptions.Add("w3c", true);
ltOptions.Add("plugin", "c#-c#");
capabilities.AddAdditionalOption("LT:Options", ltOptions);
driver = new RemoteWebDriver(new Uri("https://hub.lambdatest.com/wd/hub/"), capabilities);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
Console.WriteLine("Navigating to todos app.");
driver.Navigate().GoToUrl("https://lambdatest.github.io/sample-todo-app/");
driver.FindElement(By.Name("li4")).Click();
Console.WriteLine("Clicking Checkbox");
driver.FindElement(By.Name("li5")).Click();
// If both clicks worked, then te following List should have length 2
IList<IWebElement> elems = driver.FindElements(By.ClassName("done-true"));
// so we"ll assert that this is correct.
if (elems.Count != 2)
throw new Exception();
Console.WriteLine("Entering Text");
driver.FindElement(By.Id("sampletodotext")).SendKeys("Yey, Let's add it to list");
driver.FindElement(By.Id("addbutton")).Click();
// lets also assert that the new todo we added is in the list
string spanText = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span")).Text;
if (!"Yey, Let's add it to list".Equals(spanText))
throw new Exception();
((IJavaScriptExecutor)driver).ExecuteScript("lambda-status=passed");
}
catch
{
((IJavaScriptExecutor)driver).ExecuteScript("lambda-status=failed");
}
finally{
driver.Quit();
}
}
}
}
In the test script, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:
ChromeOptions capabilities = new ChromeOptions();
capabilities.BrowserVersion = 'latest';
Dictionary<string, object> ltOptions = new Dictionary<string, object>();
ltOptions.Add("username", LT_USERNAME);
ltOptions.Add("accessKey", LT_ACCESS_KEY);
ltOptions.Add("platformName", "Windows 10");
ltOptions.Add("project", "Demo LT");
ltOptions.Add("w3c", true);
ltOptions.Add("plugin", "c#-c#");
capabilities.AddAdditionalOption('LT:Options', ltOptions);
Note: You can generate capabilities for your test requirements with the help of our inbuilt Capabilities Generator.
Following are the commands to run your single and parallel tests.
Single test
dotnet run single
Parallel test
dotnet run parallel
To run your test in parallel using .NET Runtime, pass the following command in the terminal.
dotnet run parallel
You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid cloud using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app and pass toggle tunnel = true
via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live.
Refer our LambdaTest Tunnel documentation for more information.
Here’s how you can establish LambdaTest Tunnel.
Download the binary file of:
Open command prompt and navigate to the binary folder.
Run the following command:
LT -user {user’s login email} -key {user’s access key}
So if your user name is lambdatest@example.com and key is 123456, the command would be:
LT -user lambdatest@example.com -key 123456
Once you are able to connect LambdaTest Tunnel successfully, you would just have to pass on tunnel capabilities in the code shown below :
Tunnel Capability
ltOptions.Add("tunnel", true);