Selenium webdriver with Nunit framework

In this tutorial, lets use Nunit framework with UI automation framework Selenium Webdriver. 

Installation:

- Create the project in Visual Studio and Install the Nunit framework , reference - https://www.qafeast.com/nunit/nunit_framework

- Install the Selenium webdriver framework, reference - https://www.qafeast.com/selenium-csharp/selenium_webdriver_installation1

Let's have the below folder structure to form the framework, 

Framework

The Framework is a structure to write the code to achieve Reusability, Maintainance, Easy of use...

Test Folder - Test method with Nunit Annotations
Page Folder - Page objects for the web page
Util Folder - Hook.cs with setup and Teardown method
                   - Parse program for Excel,Json,XML,Db connection
Reports Folder - To save the output.xml file

We will take a simple example of LoginTest to navigate to https://www.qafeast.com/demo and click scrolling,

Let's have Login Page objects in LoginPage.cs in Page Folder,

class LoginPage
    {
        IWebDriver driver;
        public LoginPage(IWebDriver driver)
        {
            this.driver = driver;
        }
        public void EnterUserName()
        {
            driver.FindElement(By.Id("username")).SendKeys("Tester");
        }
        public void EnterPassword()
        {
            driver.FindElement(By.Id("pwd")).SendKeys("Tester");
        }
    }
 

 

Login Test in Test Folder,      

namespace SeleniumNunit.test
{
    [TestFixture]
    class MakeTest : Hooks // Inherit the Hook.cs file to invoke the browser for every method and close the browser
    {
        [Test]
        public void login()
        {
            driver.FindElement(By.XPath("//ul[@class='lftside - lists']/li/label[text()='Scrolling']")).Click();
            LoginPage login = new LoginPage(driver);
            login.EnterUserName();
            login.EnterPassword();
        }
}

 

Setup and Teardown method in Hook.cs folder

    [SetUpFixture]
    class Hooks
    {
        public IWebDriver driver;
        [SetUp]
        public void SetUp()
        {
            ChromeOptions chromeOptions = new ChromeOptions();
            driver = new ChromeDriver(chromeOptions);
            driver.Navigate().GoToUrl("https://www.qafeast.com/demo");
        }
        [TearDown]
        public void TearDown()
        {
            driver.Quit();
        }
    }

 

Open the command prompt and run the command to execute the test ,

nunit3-console ..\..\..\bin\debug\projectname.dll --where "method == login" --result=..\..\..\Reports\Output.xml

The sample code is written and pushed in Github - https://github.com/qafeast/selenium-csharp-pom

 

Related Tutorials