Web application testing in leanft in C sharp

In this article, we will see how to automate web application with different browsers like Internet explorer, Firefox and chrome. Before you dive into the automation of browsers, ensure that HP Functional testing agent add-on (extension) is installed and enabled in your browsers. Below code will launch softpost.org and then click on first link containing text as “LeanFT”. We can use below example with little tweaks to handle many scenarios. For example – Suppose on the given page, there is a list of links with transaction ids and you want to click on the first link. In this scenario, you can change the InnerText property of the link so that LeanFT will identify the link.
 
InnerText =As.RegExp(@”d*”)

 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK.Descriptions;
using HP.LFT.UnitTesting;
using HP.LFT.Common;
using HP.LFT.SDK.Web;
using HP.LFT.SDK;
using HP.LFT.Report;
using HP.LFT.SDK.StdWin;
using System.Diagnostics;

namespace leanFT
{
    [TestClass]
    public class UnitTest1
    {

   
        [TestMethod]
        public void VerifySoftPost()
        {
            SDK.Init(new SdkConfiguration());
            Reporter.Init(new ReportConfiguration());
            IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer);
        
            
       browser.Navigate("softpost.org");

       var leanFTLink = browser.Describe<ILink>(new LinkDescription
       {
           TagName = @"A",
           InnerText =As.RegExp(@".*LeanFT.*"),
           Index = 1
       });

       leanFTLink.Click();
       browser.Close();
        Reporter.GenerateReport();
        SDK.Cleanup();
        }
    }
}

We can also write web tests using application model as illustrated in below example. Please notice how we have passed the browser to the calculator model.
 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK.Descriptions;
using HP.LFT.UnitTesting;
using HP.LFT.Common;
using HP.LFT.SDK.Web;
using HP.LFT.SDK;
using HP.LFT.Report;
using HP.LFT.SDK.StdWin;
using System.Diagnostics;

namespace leanFT
{
    [TestClass]
    public class UnitTest1
    {

        [TestMethod]
        public void TestCalculator()
        {
            SDK.Init(new SdkConfiguration());
            Reporter.Init(new ReportConfiguration());
            Process.Start(@"d:WindowsSystem32calc.exe");
           IWindow win = Desktop.Describe<IWindow>(new WindowDescription
            {
                IsOwnedWindow = false,
                IsChildWindow = false,
                WindowClassRegExp = @"CalcFrame",
                WindowTitleRegExp = @"Calculator"
            });

           Trace.WriteLine("Calculator window title is " + win.WindowTitleRegExp);
           

           var button8 = win.Describe<HP.LFT.SDK.StdWin.IButton>(new HP.LFT.SDK.StdWin.ButtonDescription
           {
               Text = string.Empty,
               WindowId = 138,
               NativeClass = @"Button"
           });
           button8.Click();

          var result = win.Describe<IStatic>(new StaticDescription
           {
               WindowId = 150,
               NativeClass = @"Static"
           });
          Trace.WriteLine("Result text contains " + result.Text);

          var calculatorModel = new CalculatorModel();
          calculatorModel.CalculatorWindow.ButtonPlus.Click();
          calculatorModel.CalculatorWindow.Button3.Click();
          calculatorModel.CalculatorWindow.ButtonEquals.Click();


          IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer);
          var calculatorModel1 = new CalculatorModel(browser);
          calculatorModel1.GooglePage.SearchEditField.SetValue("11");

          Trace.WriteLine("Result of addition is " + result.Text);
          Assert.AreEqual("11", result.Text, "Addition of 8 and 3");
           win.Close();
           Reporter.GenerateReport();
           SDK.Cleanup();

        }

     
    }
}                    

Web development and Automation testing

solutions delivered!!