visit
Note: The license of the Community Edition expires after 30 days of usage. To extend the license, you should sign-in to the IDE. Signing-in also lets you use the other powerful features of Visual Studio such as pushing source code to private Git, syncing Visual Studio settings, and more.
Note: It is recommended to install the Selenium WebDriver executable in the location where the Google Chrome browser is present. That way, you need not mention the location of the Selenium WebDriver when invoking the same in your test implementation.
Step 1: Create a new project of the type ‘NUnit Test Project (.Net Core)’ in Visual Studio.
Step 2: Give an appropriate name to the project and click Create
Step 3: Since the project is of the type NUnit (.Net Core), the newly created .cs file will contain the basic functionalities of the NUnit framework.
Step 4: Install the Selenium WebDriver (for Google Chrome) and NUnit framework. Execute the appropriate Package Manager(PM) commands to install the necessary packages.
To execute PM commands from the PM console, navigate to Tools -> NuGet Package Manager -> Package Manager Console.
Install-Package Selenium.WebDriver
Install-Package Selenium.Chrome.WebDriver
Install-Package NUnit
Install-Package NUnit3TestAdapter
Step 5: To verify the status of the package installation, execute the Get-Package command on the PM console.
PM> Get-Package
Id Versions
-- --------
Selenium.WebDriver {3.141.0}
Selenium.Chrome.WebDriver {79.0.0}
...... ......
...... ......
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace Selenium_Demo
{
class Selenium_Demo
{
String test_url = "//www.google.com";
IWebDriver driver;
[SetUp]
public void start_Browser()
{
// Local Selenium WebDriver
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
[Test]
public void test_search()
{
driver.Url = test_url;
System.Threading.Thread.Sleep(2000);
IWebElement searchText = driver.FindElement(By.CssSelector("[name = 'q']"));
searchText.SendKeys("LambdaTest");
IWebElement searchButton = driver.FindElement(By.XPath("//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']"));
searchButton.Click();
System.Threading.Thread.Sleep(6000);
Console.WriteLine("Test Passed");
}
[TearDown]
public void close_Browser()
{
driver.Quit();
}
}
}
[SetUp]
public void start_Browser()
{
// Local Selenium WebDriver
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
[Test]
public void test_search()
{
driver.Url = test_url;
System.Threading.Thread.Sleep(2000);
IWebElement searchText = driver.FindElement(By.CssSelector("[name = 'q']"));
searchText.SendKeys("LambdaTest");
......................................
......................................
}
Now, you can execute the test using the IDE. The status of the tests can be seen using the Test Explorer in Visual Studio. For accessing Test Explorer, go to View? Test Explorer.
To build the project and execute all the tests, go to Test ? Run All Tests. On successful execution, you would see a Green test in the Test Explorer window.
The test case using the NUnit framework gave a complete end-to-end demonstration. I will see you in the next tutorial of this Selenium C# tutorial series where we’ll run our first Selenium C# script with NUnit. Let’s Automate!!!
Previously published at