Wednesday, November 22, 2017

How to set up Selenium WebDriver .NET C# project with NUnit in Visual Studio

Prerequisite:

➽ Windows as OS
➽ Visual Studio is installed

Step 1: Creating Solution and Project in VS

1. Open Visual Studio
2. Click File -> Mouse over New -> Click Project...
3. Select Templates -> Visual C# -> Test at the left side
4. Enter a Solution and Project Name -> Click OK


Step 2: Adding/Installing Selenium WebDriver package to the project

Installing Selenium.WebDriver and Selenium.Support
1. Right click on the Project -> Click "Manage NuGet Packages..."
2. In the middle panel: Click Browse -> Do search by Selenium
3. Click "Selenium.WebDriver" -> Click Install button -> Follow the installation instruction
4. After successful installation - click "Selenium.Support" -> Click Install button -> Follow the installation instruction

Step 3: Adding/Installing NUnit package to the project

Installing NUnit and NUnit3TestAdapter
1. Search by NUnit
2. Click NUnit from the list after search -> Click Install button -> Follow the installation instruction
3. After successful installation of NUnit - click NUnit3TestAdapter -> Click Install button -> Follow the installation instruction

Step 4: Creating a Test

1. In Solution Explorer at the right side - Double click on UnitTest1.cs -> Code snippet will display in the middle panel
2. Rename UnitTest1.cs as MyFirstSeleniumTest.cs at the right panel
3. Replace the existing code by your own.

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace MyFirstSeleniumTest
{
    [TestFixture]
    public class MyFirstSeleniumTest
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "https://link.testproject.io/bsg";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheUntitledTest()
        {
            driver.Navigate().GoToUrl(baseURL);
            driver.FindElement(By.LinkText("Free Sign Up")).Click();
            driver.FindElement(By.Id("first-name")).Click();
            driver.FindElement(By.Id("first-name")).Clear();
            driver.FindElement(By.Id("first-name")).SendKeys("Ripon Al Wasim");
            driver.FindElement(By.Id("email")).Clear();
            driver.FindElement(By.Id("email")).SendKeys("riponalwasim@gmail.com");
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

Step 5: Running the Test

1. Right click anywhere at the code editor
2. Click Run Tests from the pop up

No comments:

Post a Comment