Selenium WebDriver is a free and open-source library for automated testing web applications. Selenium tests can be written in multiple programming languages such as Java, C#, Python and Ruby and multiple browsers we execute scripts.
Prerequisite:
- Download and install Visual Studio Frame Work.
- Your target browser is installed, such as Chrome or Firefox.
Set up Visual Studio Solution:
Create a new project in Visual Studio Select template ‘Templates’ → ‘Visual C#’ → ‘Test’ → ‘Unit Test Project’.
Add Selenium WebDriver package to the project.
Run the following command in ‘Package Manager Console’ (selecting menu ‘Tools’ → ‘NuGet Package Manager’ → ‘Package Manager Console’).
After Selenium WebDriver is installed, Solution References will appear like the following:
How to Create a test and run it:
- Add a new C# (a test) Class. Right mouse click the project name (in Solution Explorer) select ‘Add’ → ‘Unit Test’.
- Define the following class code to test and run it.
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Safari;
namespace TestAppDemo
{
[TestClass]
public class UnitTest2
{
static IWebDriver driverGC;
// static IWebDriver driverFF;
[AssemblyInitialize]
public static void setup(TestContext contest)
{
driverGC = new ChromeDriver(@”D:Documents StudySelenium detailschromedriver_win32″);
}
[TestMethod]
public void TestChromeDriver()
{
driverGC.Navigate().GoToUrl(“http://automationpractice.com/index.php”);
driverGC.FindElement(By.Id(“search_query_top”)).SendKeys(“CASUAL DRESSES”);
driverGC.FindElement(By.Id(“search_query_top”)).SendKeys(Keys.Enter);
}
[TestMethod]
public void TestClose()
{
driverGC.Quite();
}
}
}
Right Click on `class’ and select ‘Run Tests’.
After running successful, test case, it will show as below with pass mark:
We have to understand the below elements and their usages:
- By Id: Finds the Webpage element by its unique control id.
- By Name: Finds the Webpage element by the control name.
- By Class: Finds the Webpage element by the Class name.
- By LinkText: Finds the link element by its exact text displayed on web page
- By partialLinkText: Finds webpage element that contains the given text
- By TagName: Finds the Webpage element by the Tag name.
- By XPath: This is most popular and majorly used for locating webpage element in a easiest way.
- By CSS selector: Finds Webpage element by the CSS selectorEx.
What are the Advantages of Selenium Test using C#:
- It is Open source.
- It can be extended with different technologies.
- In multiple browsers we execute scripts.
- It supports multiple OS platforms and compatible with mobile devices.
- Test executes within the browsers, so focus is not needed while executing the script.
- Using Selenium Grids, you can execute tests in parallel.
- Web Driver is faster than Selenium RC because it has simpler architecture.
- Web Driver directly talks to the web browser whereas Selenium RC needs the help of the RC Server in order to do the same.
Disadvantages of Selenium Test:
- It supports only web based applications.
- It does not provide any facility for Object Repository.
- It does not provide any facility for Recovery Scenario.
- There is no report generation for test methods by default.
- Users need to depend on minimum one programming language.
For more details, you can go through this click here.
Read Other Blogs related to Selenium: