Significance of alwaysRun=true @Test annotation property.

alwaysRun=true property informs the system that TestNg should run the test method if depends on @Test method fails also. Basically it helps to achieve the soft dependency, the feature of TestNG which helps to execute the testng test methods in order. Below is the code example:

AlwaysRun

So above code states that system will execute the Close() method if Login method fails also.

How to Launch Chrome browser with Selenium WebDriver?

Before thinking how we can run the our application on Chrome browser, download the ChromeDriver from here.

To lunch chrome browser with Selenium Webdriver takes less than a minute and 3 lines of code. So here we go-

System.setProperty(“webdriver.chrome.driver”, “D:\\Chrome_Driver\\chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“http://seleniumeasy.com/”);

How to compare two PDF files with ITextSharp and C#

I have struggled lot to compare two PDF files and display the differences. Finally I came with approach where I am extracting all the text from PDF files, splitting by lines, comparing line by line and showing the differences.

So if you have same kind of requirement, you can use below code to resolve it. Include the following namespaces while using below code-

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.Linq;
using System.IO;

static string FirstFile, SecondFile;

public static void CompareTwoPDF(string FirstPDF, string SecondPDF)
{
if (File.Exists(FirstPDF) && File.Exists(SecondPDF))
{
PdfReader reader = new PdfReader(FirstPDF);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
FirstFile += PdfTextExtractor.GetTextFromPage(reader, page, strategy);
}
PdfReader reader1 = new PdfReader(SecondPDF);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
SecondFile += PdfTextExtractor.GetTextFromPage(reader1, page, strategy);
}
}
else
{
Console.WriteLine(“Files does not exist.”);
}

List<string> File1diff;
List<string> File2diff;
IEnumerable<string> file1 = FirstFile.Trim().Split(‘\r’, ‘\n’);
IEnumerable<string> file2 = SecondFile.Trim().Split(‘\r’, ‘\n’);
File1diff = file1.ToList();
File2diff = file2.ToList();

if (file2.Count() > file1.Count())
{
Console.WriteLine(“File 1 has less number of lines than File 2.”);
for (int i = 0; i < File1diff.Count; i++)
{
if (!File1diff[i].Equals(File2diff[i]))
{
Console.WriteLine(“File 1 content: ” + File1diff[i] + “\r\n” + “File 2 content: ” + File2diff[i]);
}

}

for (int i = File1diff.Count; i < File2diff.Count; i++)
{
Console.WriteLine(“File 2 extra content: ” + File2diff[i]);
}

}
else if (file2.Count() < file1.Count())
{
Console.WriteLine(“File 2 has less number of lines than File 1.”);

for (int i = 0; i < File2diff.Count; i++)
{
if (!File1diff[i].Equals(File2diff[i]))
{
Console.WriteLine(“File 1 content: ” + File1diff[i] + “\r\n” + “File 2 content: ” + File2diff[i]);
}

}

for (int i = File2diff.Count; i < File1diff.Count; i++)
{
Console.WriteLine(“File 1 extra content: ” + File1diff[i]);
}
}
else
{
Console.WriteLine(“File 1 and File 2, both are having same number of lines.”);

for (int i = 0; i < File1diff.Count; i++)
{
if (!File1diff[i].Equals(File2diff[i]))
{
Console.WriteLine(“File 1 content: ” + File1diff[i] + “\r\n” + “File 2 Content: ” + File2diff[i]);
}

}

}

}

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&#8221;);
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&#8221;);
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&#8221;);
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.