Friday, 21 October 2016

C# Selenium Cross Browser Test Automation in Visual Studio | Firefox | Chrome | Internet Explorer

Prerequisites and Tools:

Visual Studio Selenium 3+ Version for Visual Studio with C# Bindings.
Firefox, Chrome, Internet Explorer Drivers downloaded and Saved in Local Drive.

Step 1) Create new Project as below:





Step 2) To the Project add selenium using Nuget Package Manager Console.
 


Step 3) In Package manager Console type below commands to install Selenium: 

Install-Package Selenium.WebDriver –Pre

Install-Package Selenium.Support –Pre

Step 4) Right Click on references and make sure Selenium is added to the Project:

Step 5) Use below code and import required packages as below:

using System; using Microsoft.VisualStudio.TestTools.UnitTesting;

 using OpenQA.Selenium.Firefox;

 using OpenQA.Selenium;

 using OpenQA.Selenium.Chrome;

 using OpenQA.Selenium.IE;

 namespaceSeleniumwith_CSharp
 {
 [TestClass]
 publicclassUnitTest1
 {
 [TestMethod] publicvoidFirefox()
 {
 FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"F://New folder (2)//geckodriver-v0.10.0-win64", "geckodriver.exe"); //service.Port = 64444; service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"; IWebDriver Driver = new FirefoxDriver(service); Driver.Navigate().GoToUrl("https://za.linkedin.com/in/priyathamb"); Driver.Manage().Window.Maximize(); Driver.Quit();
 }
 [TestMethod]
 publicvoidGoogleChrome()
 {
 IWebDriver Driver = new ChromeDriver(@"F:\chrome"); Driver.Manage().Window.Maximize(); Driver.Navigate().GoToUrl("https://za.linkedin.com/in/priyathamb"); Driver.Quit(); } [TestMethod] publicvoidInternetExplorer() { IWebDriver Driver = new InternetExplorerDriver(@"F:"); Driver.Manage().Window.Maximize(); Driver.Navigate().GoToUrl("https://za.linkedin.com/in/priyathamb");
Driver.Quit();
 }
 }
 }

 Step 6): Run All Tests from Tests Explorer: