TIP 2: Basic installation and using of Selenium WebDriver in Visual Studio (C#)

TIP 2: Basic installation and using of Selenium WebDriver in Visual Studio (C#)Hi, in this example I will show you very simple example how to install and use Selenium WebDriver for .NET Console Application. Let’s get started.

1) Create console application in Visual Studio

create_console_app

2) Install Selenium WebDriver through NuGet package manager

ng

 

ngsearch

At this moment you have WebDriver reference installed.

webdriver_installed

 

It’s time to write simple code to take a look how it works. First, I would like to show you how to open Firefox, navigate to the www.bing.com website, write text inside of search box and click search button:

        static void Main(string[] args)
        {
            //Initialize webdriver  
            IWebDriver webDriver = new FirefoxDriver();

            //Navigate by url to website
            webDriver.Navigate().GoToUrl("https://www.bing.com");

            //Find search field
            IWebElement searchTexBox = webDriver.FindElement(By.Id("sb_form_q"));

            //Set "search text"
            searchTexBox.SendKeys("world");

            //Find "search button"
            IWebElement searchButton = webDriver.FindElement(By.Id("sb_form_go"));

            //Click by "search button"
            searchButton.Click();

            Console.ReadLine();
        }

So, now you are ready to investigate and write powerful UI tests by using this technology!

I showed you example with FireFox, because it work out of box, but let’s try to run the same test with Google Chrome

        static void Main(string[] args)
        {
            //Initialize webdriver  
            //IWebDriver webDriver = new FirefoxDriver();
            IWebDriver webDriver = new ChromeDriver();

            //Navigate by url to website
            webDriver.Navigate().GoToUrl("https://www.bing.com");

            //Find search field
            IWebElement searchTexBox = webDriver.FindElement(By.Id("sb_form_q"));

            //Set "search text"
            searchTexBox.SendKeys("world");

            //Find "search button"
            IWebElement searchButton = webDriver.FindElement(By.Id("sb_form_go"));

            //Click by "search button"
            searchButton.Click();

            Console.ReadLine();
        }

you probably will get next exception:

An unhandled exception of type ‘OpenQA.Selenium.DriverServiceNotFoundException’ occurred in WebDriver.dll

Additional information: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html.

chrome_exception

Unfortunately, when i downloaded and installed chrome driver through NuGet, it did not help me, i think it can be caused by different supports versions of chrome driver for Google Chrome.

It generates next exception:

An unhandled exception of type ‘System.InvalidOperationException’ occurred in WebDriver.dll

Additional information: unknown error: unrecognized Blink revision: e8926f681fbb840b4f389e7e692343d4505722ce

web_driver
So, to resolve this problem you can just navigate to http://chromedriver.storage.googleapis.com/index.html (as described in exception) and download last stable version, let’s do it:

check_last_stable_version

 

here, you can see last release (stable) version:

 

check_last_stable_version_2

 

download chrome driver:

download_chromedriver

place it (for example) in solution folder

chdrexe

and set this path in your chromedriver constructor in C# code, in my case it should looks like this:

2015-11-15_2323

 

So, now it works!

Now you can sit, open the beer and enjoy by how it perfectly works :)

Download DEMO (Visual Studio 2013)

 

  • http://www.techiebuddhas.com Techie Buddhas

    worked like a charm, thanks a lot.