Tuesday 10 March 2015

C# bindings for Selenium

Prerequisites:
- Visual Studio is installed, click link for 'Visual Studio' installation.

1. Open the Visual Studio
 

2. Select the 'Console Application'.
    - Change the 'Name', 'Solution Name' and click on 'OK' button.


3. Download the Webdriver binding for C# language.
      - Open the URL: http://www.seleniumhq.org/download/
      - Click on 'Download' link for 'C#'.


 


4. Extract the downloaded zip file.


5. Add the webdriver dll's to the 'References'.
     - Right click and select 'Add Reference'.



6. Select all the webdriver related dll's and click on 'OK' button.


7. Webdriver dll's are added to the 'References'.

8. Add the code to launch the firefox browser and search for 'C#'.
    - Once code is added execute the program, clickto execute the program.

Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace WDTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.google.com");

            //Type C#
            driver.FindElement(By.Id("lst-ib")).SendKeys("C#");

            //Thread.sleep is used for initial/basic level coding,
            //In real time its better not to use 'Thread.sleep'.
            System.Threading.Thread.Sleep(2000);
            driver.FindElement(By.Name("btnG")).Click(); 

            System.Threading.Thread.Sleep(10000);

            driver.Close();
            driver.Quit();
        }
    }
}

9. Webdriver code is getting executed.

Environment used:
Above sample program is executed in below environment.
- Firefox browser version 33.0.1
- Windows 8 operating system.
- Microsoft Visual C# 2010 Express. 
- Selenium(Webdriver)-dotnet-2.45.0

Friday 6 March 2015

C# - NUnit GUI Runner installation.

1. Open the URL: http://nunit.org/?p=download
    Click the link 'NUnit-2.6.4.msi' to download.
  

2. Click on 'NUnit-2.6.4.msi', which is downloaded.

3. Click on 'Next' button.

4. Accept the license aggrement and click on 'Next' button.

5. Click on 'Complete' button.

6. Click 'Install' button.

7. Installation is in progress.

8. Click 'Finish' button.

9. Open the NUnit GUI Runner.
    Note: Below screenshot shows launching NUnit GUI Runner in Windows 8 Operating System.

10. NUnit GUI Runner is opened.

C# - Nunit Script Creation and Execution

Prerequisites:
- Visual Studio is installed, click link for 'Visual Studio' installation .
- Nunit is installed, click link for 'NUnit GUI Runner' installation.

1. Open the Visual Studio

2. Select the Class Library.
   Enter Name and Solution Name.
   Click 'OK' button.

3. Class is created.

4. Expand 'References' in 'Solution Explorer'.


5. Mouse right click on 'References', select 'Add Reference'.

6. Select the 'Browse' tab, navigate to the folder where NUnit dll's are present.
    Select the dll's and click 'ok' button. 
          ex:C:\Program Files (x86)\NUnit 2.6.4\bin\framework


7. 'nunit.framework' is added in the 'References'.

8. Add Nunit attributes to the class, method and some assertion code to perform validation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace NunitTest1
{
    [TestFixture]
    public class Class1
    {
        [TestCase]
        public void Test1()
        {
            int actualResults = 30;
            int expectedResults = 30;

            Assert.AreEqual(expectedResults, actualResults);
        }

    }
}

9. Build the solution.


10. Open the 'Nunit-GUI Runner' installed in your machine.


11. Select 'File' --> 'Open Project' in Nunit GUI Runner.

12. Select the project dll which is created at step 9 and click on 'Open' button.

13.  All the classes and methods marked by NUnit attributes are shown in the NUnit GUI Runner.

14. Select the method and click 'Run' button.
 
15. You can see the result as PASSED.

16. Now lets verify the failed results.
      Go to the code modify the Actual Results 30 to 40.

17. Rebuild the solution.

18. Go to Nunit GUI runner, now click on the 'Run' button again.

19. You can see the results as failed, since actualResults(40) and expectedResults(30) doesn't match.