How to identify Windows ToolBar using CodedUI

Below code will help to click on buttons on tool bar which generally comes when we try to login into the application or try to download any file from the application.

As first action we do on the webpage so first we have to identify the webpage and then tool bar which pop-ups after performing the some actions. We have to use WinToolBar class to identify Tool Bar. So here you go-

BrowserWindow MainWin = new BrowserWindow();
MainWin.SearchProperties[BrowserWindow.PropertyNames.Name] = “Page Title Name”;
WinToolBar ToolStrip = new WinToolBar(MainWin);
ToolStrip.SearchProperties[WinToolBar.PropertyNames.Name] = “Notification”;
ToolStrip.DrawHighlight();
WinButton No= new WinButton(ToolStrip);
No.SearchProperties[WinButton.PropertyNames.Name] = “No”;
No.DrawHighlight();
Mouse.Click(No);

ToolBar1

 

How to generate logs with CodedUI?

Why logs? Well, it helps to debug the problem in our script. Usually logging and capturing the screenshots are not enable by default with CodedUI and to make it, we need to make some set up.

Follow the below steps to enable logging and capturing the screenshots-

1: Go to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE location and search for ‘QTAgent32_40.exe.config’ and ‘QTAgent32.exe.config’. Make sure you have edit permission for these config files.

2: Modify the value for ‘EqtTraceLevel‘:

Log

3: Add App.config file in your project and add the following code under the configuration node:

Log1

4: Add following code in CodedUITest1.cs file-

Log2

5: Now run the test.

6: Open Test Explorer and click on Output hyperlink-

Log3

7: Click on UITestActionLog.html link.

Log4

8: Now check your script logs-

Log5

Log6

How to reuse browser window among TestMethods in CodedUI?

Below code helps to reuse opened browser:-

BrowserWindow driver;

[TestMethod]
public void CaptureScreenshot()
{

driver = BrowserWindow.Launch(new System.Uri(“https://www.facebook.com”));
driver.TechnologyName = “Web”;
driver.CloseOnPlaybackCleanup = false;
driver.Maximized = true;

}

[TestMethod]
public void secondtest()
{
driver = new BrowserWindow();
driver.SearchProperties.Add(“Title”, “Welcome to Facebook – Log In, Sign Up or Learn More”);

HtmlEdit usr = new HtmlEdit(driver);
usr.SearchProperties.Add(“Id”, “email”);
usr.SetProperty(“Text”, “technicalt”);
}

How to take screenshots in CodedUI?

Below code will help to take entire screen screenshot-

Image MyScreenshot;
BrowserWindow driver;

[TestMethod]
public void CaptureScreenshot()
{

driver = BrowserWindow.Launch(new System.Uri(“https://www.facebook.com”));
driver.TechnologyName = “Web”;
driver.CloseOnPlaybackCleanup = false;
driver.Maximized = true;
MyScreenshot = driver.CaptureImage();
MyScreenshot.Save(@”D:\Windowscreenshot.png”, System.Drawing.Imaging.ImageFormat.Png);

}

If we need to take screenshot of any particular UI object then we can use below code-

Image MyScreenshot;
BrowserWindow driver;

[TestMethod]
public void CaptureScreenshot()
{

driver = BrowserWindow.Launch(new System.Uri(“https://www.facebook.com”));
driver.TechnologyName = “Web”;
driver.CloseOnPlaybackCleanup = false;
driver.Maximized = true;
MyScreenshot = driver.CaptureImage();
MyScreenshot.Save(@”D:\Windowscreenshot.png”, System.Drawing.Imaging.ImageFormat.Png);

HtmlLabel Login = new HtmlLabel(driver);
Login.SearchProperties.Add(“id”, “loginbutton”);

MyScreenshot = Login.CaptureImage();
MyScreenshot.Save(@”D:\Buttoncreenshot.png”, System.Drawing.Imaging.ImageFormat.Png);

}

How to keep the browser open after a CodedUI test finishes?

When we run our first CodedUI test method we notice that system closes the browser after finishing the test method and we just wonder why it closes the browser. So answer is, CodedUI framework has been designed like that only where it closes the browser after finishing the tasks.

Same framework has provided an option where we can choose not to close browser after finishing the tasks and that option is ‘CloseOnPlaybackCleanup’ property. By using this property we can make sure that browser should not close after completing the tasks.

However, in most of cases Tester writes only a method and uses CloseOnPlaybackCleanup=false and thinks that framework should not close the browser but it does not work in this way.

CloseOnPlaybackCleanup=false says that if you have more than 1 test methods and then framework will not close the browser after the first method, it will complete all test methods tasks and then only it closes the browser. So where tester has only 1 [TestMethod] with CloseOnPlaybackCleanup=false and CodedUI closes the browser there it is working as expected.

Use below code to test it out-

CloseBrowser

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.

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;

}