Monday 5 September 2016

Selenium Webdriver - C# with NUNIT 3 using visual studio

Step1:  Open Visual studio
Step2: Choose Extension and updates from Tools Menu
Step3: Install 'NUnit 3 Test Adapter' and 'NUnit Template fro Visual Studio' - Follow below screen:

Step4: Create new Project from File-> New Project -> Test -> NUnit 3 Unit test Project:  Follow below screen

Step5: Double Click on CS File from View- > Solution Exploer

Step6: Install Selenium Webdriver, Webdriver support and chromedriver dlls by right clicking on Solution and choosing manage Nuget Package
Step 7: Install above DLLS , please follow below screen:

Step8:  Replace the code of CS file with below codes .

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace NUnit.Tests
{
    [TestFixture]
    public class TestClass
    {
        public static IWebDriver driver;

        [Test]
        public void TestMethod()
        {
            //To Launch Firefox
            driver = new FirefoxDriver();

            //To Navigate Given URL
            driver.Navigate().GoToUrl("http://newtours.demoaut.com");

            //Implicitly wait for 30 seconds
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
            //Enter credentials and login
            driver.FindElement(By.Name("userName")).SendKeys("mercury");
            driver.FindElement(By.Name("password")).SendKeys("mercury");
            driver.FindElement(By.Name("login")).Click();
        }
    }
}


Step8: Run Test from  Test -> Run-> All Tests menu