How to setup selenium c# web automation project

Recently wen automation is one of popular area and it is good to know about it. Today i am going to show you how to automate testing with Selenium web driver.

What is Selenium?

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.

(Source : http://www.seleniumhq.org/)

I am using visual studio 2013 professional.

Step 01: Setup Unit Testing project

Create unit test project

Step 02: Manager NuGet Packages

  • Selenium WebDriver
  • Selenium WebDriver Support classes
  • Selenium WebDriver Chrome Driver

wen_driver_and_support_classes

Step 03: Let’s add one html file. We can use this file to test. But in real world scenarios, we have to use QA server URL. For demo purpose I will use static html file.

html_file

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1 id="hello">Hello world</h1>
    <input type="button" value="Hello" class="btnHello" onclick="return alert('Hello by button click'); "/>
</body>
</html>

Now we will two methods into out unit testing class.
1. We will click the button
2. We will try to find an element which is not there

Here is out TestClass

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Chrome;
using System.Threading;
using System.IO;
using OpenQA.Selenium;

namespace WebAutomationUnitTest
{
	[TestClass]
	public class UnitTest
	{
		[TestMethod]
		public void TestMethod1()
		{
			using (var driver = new ChromeDriver())
			{
				try
				{
					string curDir = Directory.GetCurrentDirectory();
					driver.Url = new Uri(String.Format("file:///{0}/Index.html", curDir)).ToString();
					driver.Navigate();

                    var element = driver.FindElement(By.ClassName("btnHello"));
                    element.Click();

                    Thread.Sleep(5000);
				}
				catch (Exception)
				{

					throw;
				}

				
			}
		}

        [TestMethod]
        [ExpectedException(typeof(NoSuchElementException))]
        public void TestUndefinedElement()
        {
            using (var driver = new ChromeDriver())
            {
                try
                {
                    string curDir = Directory.GetCurrentDirectory();
                    driver.Url = new Uri(String.Format("file:///{0}/Index.html", curDir)).ToString();
                    driver.Navigate();

                    var element = driver.FindElement(By.ClassName("btnHello_not"));
                    element.Click();

                }
                catch (Exception)
                {

                    throw;
                }


            }
        }
	}
}

And lets run the test cases.

passed

OK. Lets say there is a element which will load by ajax. In that case we have to wait until the element is visible. In this king of scenario you have to use WebDriverWait option.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("btnDynamic")));
IWebElement element = driver.FindElement(By.Id("btnDynamic"));
element.Click();

You can download the project in here and test

(https://github.com/uwudamith/WebAutomationUnitTest)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s