LeanFT C# Tutorial Part 2 – Create First Test

In case you missed on part 1, check it out here.

Now that we are familiar with the code skeleton, we can start writing our first test. I’m taking a basic scenario where we are searching for Leanft on my blog. For doing this, firstly I’ll have to import the web namespace in LeanFT SDK because I want to automate a web application. Here’s how it needs to be done:-

using HP.LFT.SDK.Web;

Now I’ll declare a Browser object as an instance variable to my LeanFtTest class like this:-

namespace LeanFtTestProject1
{
 [TestClass]
 public class LeanFtTest : UnitTestClassBase<LeanFtTest>
 {
 //The Browser object on which the test will be run
 IBrowser browser;

After that, we can use the TestInitialize() method to launch a browser so that a fresh browser
will open before each test. This can be done as follows:-


[TestInitialize]
public void TestInitialize()
{
browser = BrowserFactory.Launch(BrowserType.InternetExplorer);
}

Here, BrowserFactory is a class provided by LeanFT SDK’s Web namespace. The launch method can be used to launch either IE, Chrome, Firefox or Edge. For version support, you can have a look at the availability matrix here (you will need an HP Passport account for this).

Similar to TestInitialize(), I’ll be using the TestCleanup() method which closes the browser instance as follows:-


[TestCleanup]
 public void TestCleanup()
 {
 browser.Close();
 }

Now I just need to write a test method which will contain my test logic. For the sake of this tutorial, I’ll be going to a search engine called “DuckDuckGo” and then I’ll search for learn2automate. Once my blog opens, I’ll be searching for LeanFT and see that there should be a link that contains the text “LeanFT”. Here’s the full code for doing this:-


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK;
using HP.LFT.SDK.Web;

namespace LeanFtTestProject1
{
 [TestClass]
 public class LeanFtTest : UnitTestClassBase<LeanFtTest>
 {
 //The Browser object on which the test will be run
 IBrowser browser;

 [ClassInitialize]
 public static void ClassInitialize(TestContext context)
 {
 GlobalSetup(context);
 }

 [TestInitialize]
 public void TestInitialize()
 {
 browser = BrowserFactory.Launch(BrowserType.Firefox);
 }

 [TestMethod]
 public void TestMethod1()
 {
 try
 {
 //Navigate to Search Engine
 browser.Navigate("www.duckduckgo.com");

 //Search for Learn2Automate blog
 IEditField txtSearch = browser.Describe<IEditField>(new EditFieldDescription
 {
 Type = "text",
 TagName = "INPUT",
 Name = "q"
 });
 txtSearch.SetValue("learn2automate");

 IButton btnSearch = browser.Describe<IButton>(new ButtonDescription
 {
 ButtonType = "submit",
 TagName = "INPUT",
 Name = "S"
 });
 btnSearch.Click();


 //Select the first search result
 ILink firstResult=browser.Describe<ILink>(new LinkDescription
 {
 TagName ="A",
 InnerText = As.RegExp(".*learn2automate.*"),
 Index = 0
 });

 Assert.IsTrue(firstResult.Exists(10));

 firstResult.Click();

 //Search for LeanFT if the blog opens
 IEditField searchBox = browser.Describe<IEditField>(new EditFieldDescription
 {
 Type = "text",
 TagName = "INPUT",
 Name = "s"
 });

 Assert.IsTrue(searchBox.Exists(10));

 searchBox.SetValue("leanft");

 IButton goButton=browser.Describe<IButton>(new ButtonDescription
 {
 ButtonType = "submit",
 TagName = "INPUT",
 Name = "Go"
 });

 goButton.Click();

 //Verify that the blog entry with title LeanFT opens
 ILink blogResult = browser.Describe<ILink>(new LinkDescription
 {
 Color = @"rgb(0, 102, 204)",
 TagName = "A",
 InnerText = As.RegExp(".*leanfT.*"),
 Index = 0
 });
 
 Assert.IsTrue(blogResult.Exists(10)); 
 blogResult.Highlight();

 }
 catch(Exception e)
 {
 Assert.Fail("Unexpected Error Occurred while searching for LeanFT on Learn2Automate. Error = " + e.Message);
 }
 
 }

 [TestCleanup]
 public void TestCleanup()
 {
 browser.Close();
 }

 [ClassCleanup]
 public static void ClassCleanup()
 {
 GlobalTearDown();
 }
 }
}

Lets understand it now. First of all we start with the try block. If you have been using UFT, you must be familiar with the pain of using the old school “On Error Resume Next”, so we can counter that here by using try catch block. In case you are new to programming, it would be better if you read about exception handling in C#.

Then we are using the good old navigate function on the browser object that we created in the TestInitialize method to navigate to the DuckDuckGo website.

After that, we create the edit box object where we will set the search term that we are looking for. In case you want to learn how we can create this statement, you should check out my post on using LFT’s Object Spy.

After setting the search term and clicking on the go button, I create the object for the result item and put in an assertion to verify that it should exist. Please note that this assertion enables me to exit the test method if the firstResult object is not found in a matter of 10 seconds.

Similar to the above approach, upon clicking the first result, I look for the search button on my blog and look for the term leanFT and verify the search result similarly. You must have noticed that LFT does not have any explicit wait statement, but we have to use the exists method on an object to do the synchronization.

One thing worth noticing here is that all my reporting is being done with Assertions. An alternative to this can be the Reporter object that is provided with LeanFT’s SDK. This can be used to create an HTML report of our test which is stored in the RunResults folder inside our C# project. In order to use the Reporter object, you need to call the following method:-

Reporter.ReportEvent("Blog entry with text - leanft should appear", "Blog Found", HP.LFT.Report.Status.Passed);

In order to run your test, just right click your Test Method in the Test Explorer(in case you are not able to see the Test Explorer, goto Menu option Test -> Windows -> Test Explorer) and run or debug your test.

I hope by now you are able to create a basic script in LeanFT. If you have doubts, feel free to comment !

LeanFT C# Tutorial Part 3 – Object Identification Center

Happy Automating,

Harshit Kohli

Advertisements

Eat, Sleep, Automate. REPEAT !

Tagged with: , , , , , , , , ,
Posted in LeanFT
3 comments on “LeanFT C# Tutorial Part 2 – Create First Test
  1. […] LeanFT C# Tutorial Part 2 – Create First Test […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats
  • 168,130 hits
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 796 other followers

%d bloggers like this: