Executing JS in leanft in Java

While automating web applications, you might need to execute the JavaScript to perform certain operations. LeanFT allows you to execute custom JavaScript as shown in below example. We can use runJavaScript method of Page class to execute any valid JavaScript.
 
package leanft;

import com.hp.lft.report.Reporter;
import com.hp.lft.report.Status;
import com.hp.lft.sdk.web.*;
import com.hp.lft.verifications.Verify;
import org.junit.Test;

public class ExecuteJavaScriptTest extends BaseTest {

    @Test
    public void test() throws Exception{

        Browser browser = BrowserFactory.launch(BrowserType.INTERNET_EXPLORER);
        try{
            //Navigate to https://www.softpost.org/selenium-test-page/
            browser.navigate("https://www.softpost.org/selenium-test-page/");
            browser.sync();
            String html =  browser.getPage().runJavaScript("document.body.innerHTML;");
            System.out.println("HTML Source of the page" + html);
            System.out.println("Scrolling to 120,100");
            browser.getPage().runJavaScript("window.scrollTo(120,100);");

            String documentState = browser.getPage().runJavaScript("document.readyState");
            System.out.println("Document State is " + documentState);

        }
        catch(AssertionError ex){
           //Report the Exception
           Reporter.reportEvent("Exception","Test failed", Status.Failed, ex);
           throw ex;
        }
        finally{
            //Close the browser
            browser.close();
        }
    }
} 

Web development and Automation testing

solutions delivered!!