Executing JavaScript in webpage in LeanFT in C#.Net

Below example illustrates how to execute java script in a web page in LeanFT. Note that RunJavaScript method of Page object to execute a JavaScript.

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

namespace LeanFtTestProject1
{
    [TestClass]
    public class JavaScriptTest : UnitTestClassBase<LeanFtTest>
    {
        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            GlobalSetup(context);
        }

        [TestInitialize]
        public void TestInitialize()
        {

        }

        [TestMethod]
        public void ExecuteJavaScript()
        {
           IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer);

            // Navigate to www.softpost.org
            browser.Navigate("https://www.softpost.org/selenium-test-page/");
            browser.Sync();

            String html = browser.Page.RunJavaScript("document.body.innerHTML;");
            Console.WriteLine("HTML Source of the page" + html);
            Console.WriteLine("Scrolling to 120,100");
            browser.Page.RunJavaScript("window.scrollTo(120,100);");

            String documentState = browser.Page.RunJavaScript("document.readyState");
            Console.WriteLine("Document State is " + documentState);


            browser.Close();
        }

        [TestCleanup]
        public void TestCleanup()
        {
        }

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

You may also like...