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]);
}

}

}

}