Solution 1:

From the Selenium Documentation:

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;

class GoogleSuggest
{
    static void Main(string[] args)
    {
        IWebDriver driver = new FirefoxDriver();

        //Notice navigation is slightly different than the Java version
        //This is because 'get' is a keyword in C#
        driver.Navigate().GoToUrl("http://www.google.com/");
        IWebElement query = driver.FindElement(By.Name("q"));
        query.SendKeys("Cheese");
        System.Console.WriteLine("Page title is: " + driver.Title);
        driver.Quit();
    }
}

Solution 2:

  1. Install the NuGet packet manager

    Download link: https://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c

  2. Create a C# console application

  3. Right-click on the project → Manage NuGet Packages. Search for "Selenium" and install package Selenium.Support.

You are done now, and you are ready to write your code :)

For code with Internet Explorer, download the Internet Explorer driver.

Link: http://selenium-release.storage.googleapis.com/index.html

  • Open 2.45 as its the latest release
  • Download IEDriverServer_x64_2.45.0.zip or IEDriverServer_Win32_2.45.0.zip
  • Extract and simply paste the .exe file at any location, for example C:\
  • Remember the path for further use.

Overall reference link: Selenium 2.0 WebDriver with Visual Studio, C#, & IE – Getting Started

My sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.IE;

namespace Selenium_HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new InternetExplorerDriver("C:\\");
            driver.Navigate().GoToUrl("http://108.178.174.137");
            driver.Manage().Window.Maximize();
            driver.FindElement(By.Id("inputName")).SendKeys("apatra");
            driver.FindElement(By.Id("inputPassword")).SendKeys("asd");
            driver.FindElement(By.Name("DoLogin")).Click();

            string output = driver.FindElement( By.XPath(".//*[@id='tab-general']/div/div[2]/div[1]/div[2]/div/strong")).Text;

            if (output != null  )
            {
                Console.WriteLine("Test Passed :) ");
            }
            else
            {
                Console.WriteLine("Test Failed");
            }
        }
    }
}

Solution 3:

To set up the IDE for Selenium in conjunction with C# is to use Visual Studio Express. And you can use NUnit as the testing framework. The below links provide you more details. It seems you have set up what is explained in the first link. So check the second link for more details on how to create a basic script.

How to setup C#, NUnit and Selenium client drivers on Visual Studio Express for Automated tests

Creating a basic Selenium web driver test case using NUnit and C#

Sample code from the above blog post:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Step a
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;

namespace NUnitSelenium
{
    [TestFixture]
    public class UnitTest1
    {
        [SetUp]
        public void SetupTest()
        {
        }

        [Test]
        public void Test_OpeningHomePage()
        {
            // Step b - Initiating webdriver
            IWebDriver driver = new FirefoxDriver();
            // Step c: Making driver to navigate
            driver.Navigate().GoToUrl("http://docs.seleniumhq.org/");

            // Step d
            IWebElement myLink = driver.FindElement(By.LinkText("Download"));
            myLink.Click();

            // Step e
            driver.Quit();

            )

        }
    }

Solution 4:

One of the things that I had a hard time finding was how to use PageFactory in C#. Especially for multiple IWebElements. If you wish to use PageFactory, here are a few examples. Source: PageFactory.cs

To declare an HTML WebElement, use this inside the class file.

private const string _ID ="CommonIdinHTML";
[FindsBy(How = How.Id, Using = _ID)]
private IList<IWebElement> _MultipleResultsByID;

private const string _ID2 ="IdOfElement";
[FindsBy(How = How.Id, Using = _ID2)]
private IWebElement _ResultById;

Don't forget to instantiate the page object elements inside the constructor.

public MyClass(){
    PageFactory.InitElements(driver, this);
}

Now you can access that element in any of your files or methods. Also, we can take relative paths from those elements if we ever wish to. I prefer pagefactory because:

  • I don't ever need to call the driver directly using driver.FindElement(By.Id("id"))
  • The objects are lazy initialized

I use this to write my own wait-for-elements methods, WebElements wrappers to access only what I need to expose to the test scripts, and helps keeps things clean.

This makes life a lot easier if you have dynamic (autogerated) webelements like lists of data. You simply create a wrapper that will take the IWebElements and add methods to find the element you are looking for.