Wednesday, 21 November 2018

Selenium in c# a quick start


Selenium a Brief Summary


Growing trend in software testing is the use of  test automation. When the influence of  Agile methodology coming into place testing become more time consuming and continues. Repeated testing more hardest part in the software development.

Many tools available for test automation and the trending one is selenium.

Selenium is a test automation  tool,which support various technologies and it supports cross platform.
Selenium is not a single tool its a suite of tools.


Selenium automating only on web applications and operate on different browsers and OS.


Selenium suite containing following tools:

  1. Selenium IDE
  2. Selenium WebDriver
  3. Selenium RC
  4. Selenium Grid
Advantages :
  1. Open  source tool
  2. Can be use in various technologies
  3. Execute scripts in various OS
  4. Support  mobile devices
Disadvantages :
  1. Support only in web based application
  2. cant access control within in the browser
  3. no default report generation
Selenium automation tests can be run as a Console Application, Selenium Service, and NUnit Framework. Since NUnit is used by .NET developers for unit testing, QA engineers can also develop and run Selenium tests with NUnit.

NUnit Framework

NUnit is a unit-testing framework for .NET applications in which the entire application is isolated into diverse modules. Each module is tested independently to ensure that the objective is met. 

This framework is very easy to work with and has user friendly attributes for working.

Nunit is an open source framework. Latest version is 3.
It only provides some custom attributes and some static Assert classes.

Some of the custom attributes are:
  • TestFixture
  • Setup
  • TearDown
  • Test
  • Category
  • Ignore
  • TestCase
  • Repeat
  • MaxTime
The [TestFixture]  attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method.

Workout Example :


Prior to working out Ensure your system having visual studio 


Visual Studio 2017 -> New Project -> Class Library -> ProjectName





Right click on solution ->Manage Nuget packages for solution






Add packages for both Selenium and NUnit



  1. In NuGet search box, Choose Browse tab and type Nunit in search text box.
  2. Choose NUnit and click on Install button.
NUnit package adding in vs



For setup TestRunners, we need to add Nunit Test Adapter from NuGet packages. Follow the below steps:
  1. Choose NUnit3TestAdapter and click on Install button.



Now the setup are ready.  We can create  test case to check the code is working fine. 

Include namespace NUnit. Framework in namespaces section. 


Sample Code:





using NUnit.Framework;
namespace TestProject
{
    class TestDemo
    {
        [TestFixture]
        public class MyTest
        {
            [Test]
            public void TestingStarted()
            {
                string testStatus = "Success";
                Assert.That("Success" == testStatus);
            }
        }
    }
}

After build the Code:
Choose visual studio Test Menu -> Windows ->  Test Explorer.



Test Explorer shows Test function in Not Run Tests section. Choose Run All button to execute test cases.

In below of Test Explorer, it will show the result of TestingStatus test result. In the below screenshot, it is showing the result 'Test Passed - TestingStatus '.




Now you created a sample tests in NUnit. Unit testing in .Net is done like this.


We have to integrate the test cases to run against the web application so Selenium WebDriver is needed.



Selenium Web Driver

Web Driver is a automating web application testing tool. This provide APIs that easily integrate with any programming language such as C#, java, python. The use of Selenium Web driver is  to basically test any web application.


Add Selenium web driver into the project 



  1. Choose Selenium.WebDriver and click on Install button.


Now you added Selenium We driver.

we can create a sample test case for interacting with the browser.
In this example I have selected Firefox as my browser.

 1. create a new class in the same project


 2. Add references for selenium



using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
The above references can provide all classes and properties required to integrate the selenium webdrivers

 3. Add necessary test cases..here I write code for just open the google.com in firefox.



using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace TestProject
{
    class Program
    {
        IWebDriver driver;

        [SetUp]
        public void startBrowser()
        {
            driver = new FirefoxDriver();
        }

        [Test]
        public void test()
        {
            driver.Url = "http://www.google.co.in";
        }

        [TearDown]
        public void closeBrowser()
        {
            driver.Close();
        }

    }
}
We can have webdrivers as per the web browser.  Like for chrome browser we do have ChromeDriver, for Firefox browser we do have FirefoxDriver and so on.

Here I'm using FirefoxDriver. 


An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. 


 4. Build the solution 


 5. Run the project and now you can see the  FireFox browser opened with google.









Hope you got an overall idea about Selenium and Nunit.



   Happy coding!





No comments:

Post a Comment