Cross Browser Testing using CodedUI Test

Before starting this topic I would like to clear in beginning that Visual Studio 2013 uses Selenium WebDriver component to achieve cross browser testing as of now.

So to integrate WebDriver component with Visual Studio follow below given steps-

Start the Visual Studio and go to Tools menu and click on Extensions and Updates…

CUIT

System will open Extensions and Updates window. Make sure you are in Online section. Search the ‘Selenium Component’ and download the same.

CUIT1

It will download the .msi file.

CUIT2

Install it on running machine.

CBT

Once you installed it, it keeps all the dll and files in below location-

C:\Program Files (x86)\Common Files\microsoft shared\VSTT\Cross Browser Selenium Components

CUIT3

Now restart the system and write below method-

CUIT4

CurrentBrowser property is a place where you define your browser type. Now run it and see the magic.

Synchronization or Wait in Selenium WebDriver (C#)

Synchronization or wait helps to handle dependencies while executing the script because sometime tools execution speed does not match with the application speed or some web elements response time does not match with script actions.

So to handle synchronization, selenium webdriver provides two effective ways and those are-

  • Implicitly Wait
  • Explicitly Wait

An implicitly Wait tell to WebDriver to hold for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Here we go with sample code to achieve Implicitly Wait-

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;

namespace TestProject
{
[TestClass]
public class UnitTest1
{
IWebDriver driver;
[TestMethod]
public void TestMethod1()
{
driver = new ChromeDriver(@”D:\Download\VisualStudio_Projects”);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(10));

driver.Navigate().GoToUrl(“http://www.bing.com”);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id(“sb_form_q”)).SendKeys(“Mayank”);
driver.FindElement(By.Id(“sb_form_go”)).Click();
driver.Quit();

}
}
}

Explicit Wait is intelligent wait and for a particular Web Element. Using explicit wait we basically instruct the wait to x unit of time span before giving up and along with it we can include condition that if particular webelement appear with in x unit of time span then move to next line of code.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;

namespace TestProject
{
[TestClass]
public class UnitTest1
{
IWebDriver driver;
[TestMethod]
public void TestMethod1()
{
driver = new ChromeDriver(@”D:\BackUp\MyData\VisualStudio_Projects”);
driver.Navigate().GoToUrl(“http://www.bing.com”);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id(“sb_form_q”)).SendKeys(“Mayank”);

WebDriverWait load = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
load.Until(ExpectedConditions.ElementExists(By.Id(“sb_form_go”)));

driver.FindElement(By.Id(“sb_form_go”)).Click();
driver.Quit();

}
}
}

Hope above code snippets will help to resolve any synchronization issue.

How to setup Selenium WebDriver with Visual Studio 2013?

Before setting up Selenium with Visual Studio, we should make sure that machine should have installed Visual Studio 2013 and NuGet.

So if above pre-requisites are in right place then start Visual Studio and create new project.

VS

Select Visual C# > Test > Unit Test Project. Provide project name, location and click on OK button.

VS1

Select your project and right click. Click on Manage NuGet Packages…option.

VS2

Make sure you are pointing to ‘Online‘. Search the term ‘Selenium‘ and install Selenium WebDriver and Selenium WebDriver Support Classes. Now close the ‘Manage NuGet Package‘ window.

VS3

We will use Chrome browser to execute our first selenium script, so for this download Chrome driver from here.

Download and extract the file.

Open UnitTest1.cs file and include following namespaces-

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

Now under TestMethod1() add following code-

IWebDriver driver = new ChromeDriver(@”D:\Download\VisualStudio_Projects”);
driver.Navigate().GoToUrl(“http://www.bing.com”);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id(“sb_form_q”)).SendKeys(“Mayank”);
driver.FindElement(By.Id(“sb_form_go”)).Click();
driver.Quit();

VS4

Now run the method. It will open the Chrome browser with bing.com and will search the term.

Comment out if above steps are not working for you.

How to click on HtmlButton within HtmlCell with CodedUI (C#)

Sometime it’s hard to iterate through HtmlCell and click on Html Button or Radio Button or Check-boxes which is within HtmlCell. In my previous post, I have already described How to get InnerText of HtmlCell and now I am providing sample code to click on HtmlButton.

Here we go-

public void ClickonButton ()
{
HtmlTable PackageTable = new HtmlTable(browser);
PackageTable.SearchProperties.Add(“Id”, “TableID”);
PackageTable.DrawHighlight();
HtmlCell Cell = new HtmlCell(PackageTable);
Cell.FilterProperties[HtmlCell.PropertyNames.RowIndex] = RowNumber.ToString();
Cell.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = CellNumber.ToString();
HtmlInputButton SaveBtn = new HtmlInputButton(Cell);
SaveBtn.SearchProperties.Add(“Id”, “btnPubPkg”);
SaveBtn.DrawHighlight();
Mouse.Click(SaveBtn);
}

Comment it out if it does not work for you.

How to do Database Testing with CodedUI (C#)?

Here I am providing sample code to start Database Testing with CodedUI (C#)-

//Below code willl login and open the DB connection.
string connetionString = “Data Source=DBSeverName;Initial Catalog=DBName;User ID=UserName;Password=DBPassword”;
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();

//Below code will help to execute the Select query.
String Selectquery = “Select FirstName from Emp where EmpID in (01)”;

SqlCommand command = new SqlCommand(Selectquery, connection);
SqlDataReader reader = command.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(reader);

//Below code will help to verify the data which above code fetch from the particular table.
String rowText;
rowText = DB.dataTable.Rows[0][DB.dataTable.Columns[0]].ToString();
if (rowText.Contains(“Mayank”))
{
Console.WriteLine(“FirstName is macthing with expected result”);
}
else
{
Console.WriteLine(“Verification is not passed”);
}

//It will close the connection.
connection.Close();

How to verify availability of options in a Drop-Down field with CodedUI (C#)

It’s easy to get to know if particular option is available in drop-down field, as we just select that option and it verifies that…OK option is available. However if we want to verify that particular option is not available in some drop-down field then we need to think about some trick. One of trick is given below to get to know that option is not available-

public Boolean CheckOption(browser, String DropDownObjectID, String Option)
{
HtmlComboBox SelectObject = new HtmlComboBox(browser);
SelectObject.SearchProperties.Add(“Id”, DropDownObjectID);
SelectObject.DrawHighlight();
String[] Selectoption = SelectObject.GetContent();
Boolean result = false;
foreach (String data in Selectoption)
{

if (data.Contains(Option))
{
result=true;
}
}

return result;

}

How to identify HtmlTag.A object with CodedUI (C#)

I invested many hours to find out that how we can identify class: ‘HtmlTag.A’ object? Eventually I have found that we can use ‘HtmlCustom’ class to identify ‘HtmlTag.A’ object. Below syntax will help to find it and click on it-

HtmlCustom Tab = new HtmlCustom(browser);
Tab.SearchProperties.Add(“InnerText”, “ManageUser”, “TagName”, “A”);
Mouse.Click(Tab);

Provide your comment if it is not working in your case.