Thursday, May 29, 2014

Selenium WebDriver test in C#

I was trying simple C# test by using Selenium. It is very simple to use and noticed that few changes. You should have NUnit, Visual Studio 2013 and Selenium DLLs. You can go through the links, which are available at the end of post.

Bing Search - Selenium Test - C# Code

using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Support; using OpenQA.Selenium.IE; using NUnit.Framework; namespace BingSearch { [TestClass] public class SearchTests { public static IWebDriver WebDriver; [TestInitialize] public void setupAppTest() { try { System.Console.WriteLine("Test setup started..."); InternetExplorerOptions ieOptions = new InternetExplorerOptions(); ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true; ieOptions.IgnoreZoomLevel = true; WebDriver = new InternetExplorerDriver(ieOptions); } catch (Exception ex) { System.Console.WriteLine("Exception Occured in Init @ " + ex.Source); } } [TestMethod] public void testBingSearch() { //Navigate to the site WebDriver.Navigate().GoToUrl("http://www.bing.com"); // Get Search text field info IWebElement query = WebDriver.FindElement(By.Name("q")); // Enter Search String query.SendKeys("Selenium"); // Submit the form query.Submit(); // Sleep for 5 seconds System.Threading.Thread.Sleep(5000); //Get all the links from result page System.Collections.ObjectModel.ReadOnlyCollection links = WebDriver.FindElements(By.TagName("a")); //Print all the links foreach (IWebElement link in links) { System.Console.WriteLine(link.GetAttribute("href")); } //Assert the title text Assert.AreEqual("Selenium - Bing", WebDriver.Title); } [TestCleanup] public void testCleanup() { // Cleanup activites WebDriver.Quit(); WebDriver.Dispose(); System.Console.WriteLine("Test cleanedup & completed successfully.."); } } }


Below links might be useful to make setup to execute the Selenium tests.
How To - Setup C#, NUnit, Selenium
Capturning Screenshots

5 comments:

Unknown said...

It was very nice article and it is very useful to Manual Testing learners.We also provide Cub training software online training.

Suseela said...



That is very interesting; you are a very skilled blogger. I have shared your website in my social networks..!


Online Reputation Management

Pooja Khot said...

Advanto (formerly Peers IT Solutions,Pune) was established in 2012 by a group of IT professionals who had a passion
for teaching and who wanted to pass on the knowledge they had accumulated over a period of 10 years to people
who want to make a career in the IT industry.We strive to teach the latest and most Advanced technologies to our candidates so that
they are not left behind while the industry is making progress.Candidates can easily adapt to their work as they will be in sync with the Same Technology that
is being used in the industry.

Pooja Khot said...

Advanto (formerly Peers IT Solutions,Pune) was established in 2012 by a group of IT professionals who had a passion
for teaching and who wanted to pass on the knowledge they had accumulated over a period of 10 years to people
who want to make a career in the IT industry.We strive to teach the latest and most Advanced technologies to our candidates so that
they are not left behind while the industry is making progress.Candidates can easily adapt to their work as they will be in sync with the Same Technology that
is being used in the industry.

Unknown said...

Coded ui Training in Chennai by Vishwa
Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.
I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
Coded UI training in chennai