Selenium Webdriver And NUnit With Visual Studio

Selenium WebDriver

Selenium WebDriver API Commands and Operations

Find Element

  1. By ID

    Example:
    1. <span id="spanName">name</span>  
    2. IWebElement element = driver.FindElement(By.Id("spanName"));  
  2. By Class Name

    Example:
    1. <div class="mobile"><span>Nokia</span></div><div class="mobile"><span>IPhone</span></div><div class="mobile"><span>Samsung</span></div>  
    2. IList<IWebElement> mobiles = driver.FindElements(By.ClassName("mobile"));  
  3. By Tag Name

    Example:
    1. <iframe src="..."></iframe>  
    2. IWebElement frame = driver.FindElement(By.TagName("iframe"));  
  4. By Name

    Example:
    1. <input name="fname" type="text"/>  
    2. IWebElement firstname = driver.FindElement(By.Name("fname"));  
  5. By Link Text

    Example:
    1. <a href="http://www.google.com/search?q=india">india</a>  
    2. IWebElement country = driver.FindElement(By.LinkText("india"));  
  6. By Partial Link Text

    Example:

    1. <a href="http://www.google.com/search?q=india">search for india</a>  
    2. IWebElement country = driver.FindElement(By.PartialLinkText("india"));  
  7. By CSS

    Example:
    1. <div id="emp"><span class="admin">sysAdmin</span><span class="user">guest</span></div>  
    2. IWebElement user = driver.FindElement(By.CssSelector("#emp span.admin"));  
  8. By XPATH

    Example:
    1. <input type="text" name="fname" />  
    2. <INPUT type="text" name="lname" />  
    3. IList<IWebElement> inputs = driver.FindElements(By.XPath("//input"));  

NUnit

NUnit is the third party tool developed for unit testing in Visual Studio

You can get more information on NUnit.

To integrate Nunit in visual studio first create new project in visual studio.

class

Also add references using Nuget packages related to NUNIT and WebDriver

WebDriver

Please find the following code to write test cases using NUnit

  1. using NUnit.Framework;  
  2. using OpenQA.Selenium;  
  3. using OpenQA.Selenium.Chrome;  
  4. using OpenQA.Selenium.Firefox;  
  5.   
  6. namespace NunitSelenium  
  7. {  
  8.     [TestFixture]  
  9.     public class TestSeleniumClass  
  10.     {  
  11.   
  12.         IWebDriver driverFF;  
  13.         IWebDriver driverGC;  
  14.   
  15.         //declarations ..  
  16.         [SetUp]  
  17.         public void Init()  
  18.         {  
  19.             //All initializations  
  20.             driverFF = new FirefoxDriver();  
  21.             driverGC = new ChromeDriver(@"C:\Downloads\chromedriver_win32");  
  22.         }  
  23.   
  24.         [Test]  
  25.         public void TestFirefoxDriver()  
  26.         {  
  27.             driverFF.Navigate().GoToUrl("http://www.google.com");  
  28.             driverFF.FindElement(By.Id("lst-ib")).SendKeys("Selenium with C#");  
  29.             driverFF.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);  
  30.         }  
  31.   
  32.         [Test]  
  33.         public void TestChromeDriver()  
  34.         {  
  35.             driverGC.Navigate().GoToUrl("http://www.google.com");  
  36.             driverGC.FindElement(By.Id("lst-ib")).SendKeys("Selenium with C#");  
  37.             driverGC.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);  
  38.         }  
  39.     }  
  40. }  
Before executing the test application download the browser specific driver.

Please find the following url to download the driver.

And store the driver at the location C:\Downloads\chromedriver_win32.

NUnit-Gui

This is a desktop application tool. This tool is very useful for executing the test cases and then also it provides the status of executed test cases. That how many test cases passed, failed and also give the proper exception.

The input for this tool is the dll or exe of code where the test cases return.

Then this tool extracts the test cases and shows it in the left panel and we can run all or individual test case from this tool. Also this tool provides the test result in xml format so that we can create HTML using this xml and create test report.

Here's the screenshot:
 
create test report

Also the following is the XML data:
  1. <?xml version="1.0" encoding="utf-8" standalone="no" ?>  
  2.     -  
  3.     <test-results name="C:\Users\aprabhu\Documents\Visual Studio 2015\Projects\seleniumDemo\NunitSelenium\bin\Debug\NunitSelenium.dll" total="2" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2015-10-06" time="16:37:01">  
  4.         <environment nunit-version="2.6.4.14350" clr-version="2.0.50727.5420" os-version="Microsoft Windows NT 6.1.7601 Service Pack 1" platform="Win32NT" cwd="C:\Program Files (x86)\NUnit 2.6.4\bin" machine-name="AMITP" user="aprabhu" user-domain="CLARICE" />  
  5.         <culture-info current-culture="en-IN" current-uiculture="en-US" /> -  
  6.         <test-suite type="Assembly" name="C:\Users\aprabhu\Documents\Visual Studio 2015\Projects\seleniumDemo\NunitSelenium\bin\Debug\NunitSelenium.dll" executed="True" result="Success" success="True" time="33.767" asserts="0">  
  7.             -  
  8.             <results>  
  9.                 -  
  10.                 <test-suite type="Namespace" name="NunitSelenium" executed="True" result="Success" success="True" time="33.756" asserts="0">  
  11.                     -  
  12.                     <results>  
  13.                         -  
  14.                         <test-suite type="TestFixture" name="TestSeleniumClass" executed="True" result="Success" success="True" time="33.754" asserts="0">  
  15.                             -  
  16.                             <results>  
  17.                                 <test-case name="NunitSelenium.TestSeleniumClass.TestChromeDriver" executed="True" result="Success" success="True" time="17.253" asserts="0" />  
  18.                                 <test-case name="NunitSelenium.TestSeleniumClass.TestFirefoxDriver" executed="True" result="Success" success="True" time="16.446" asserts="0" />  
  19.                             </results>  
  20.                         </test-suite>  
  21.                     </results>  
  22.                 </test-suite>  
  23.             </results>  
  24.         </test-suite>  
  25.     </test-results>  
Here's the output 

Run

output

Selenium with Csharp

 

Up Next
    Ebook Download
    View all
    Learn
    View all