Quantcast
Channel: Relevant Codes » Java
Viewing all articles
Browse latest Browse all 4

HP LeanFT & Java – First Steps

$
0
0

As some of you may already be aware that HP LeanFT is finally here. Underneath, it is the UFT SDK which allows coding your tests in C# and Java in your favorite IDE (although the documentation is focused around Visual Studio and Eclipse). There is also support for versioning software (Git, Subversion), continuous integration and BDD/TDD frameworks. Also, because it uses the UFT SDK under the covers, you can automate Standard Windows, WPF, .NET, Mobile etc. apart from Web from your favorite IDE.

LeanFT seems to be a very capable tool, does everything just as neatly as QTP but it feels much more powerful thanks to a proper coding environment. If you are already well-versed with QTP, you would find a lot of similarities here and will be able to leverage a lot of your QTP experience.

Download

To download, visit this link. You will be asked to fill out some details, after completing which you will be directed to the download page:

After you download and install LeanFT, don’t forget to check the option for Eclipse (if you are using Java) which will install the necessary templates and also deploy all plugins required.

The installation will add the following directory to your system PATH:

C:\Program Files (x86)\HP\LeanFT\bin

Licensing

To learn more about licensing, visit this link.

Browser Setup

A few setting(s) you should perform for different browsers before we dive into the code:

Firefox

In the Firefox browser menu, select Add-ons and then click Extensions. Drag the Agent.xpi file from the %LeanFT%\Installations\Firefox folder to the Firefox Extension page and click ‘Install now’.

Chrome

If you are testing against Chrome like in this example, ensure the LeanFT plugin is enabled from Chrome Settings -> Extensions.

Internet Explorer

With IE, go to Settings -> Manage Addons where HP Functional Testing Agent must be enabled.

LeanFT Test

HP provides a LeanFT-JUnit template which would perform all of the initializations. In this post, I will show how to use the template but also how to perform the configuration without it, so you don’t have to use a template or even extend the UnitTestClassBase. More info here.

If you use the LeanFT-JUnit template or extend from UnitTestClassBase, all required configurations would already be taken care of. No extra steps required. UnitTestClassBase extends from UnitTestBase and the you can see the entire code of this class here. Below is the project structure and sample code using the template:

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
 
import unittesting.UnitTestClassBase;
 
import com.hp.lft.sdk.web.*;
 
public class LeanFtMercuryTours extends UnitTestClassBase {
    @BeforeClass
    public static void beforeClass() throws Exception {
        globalSetup(LeanFtMercuryTours.class);
    }
 
    @AfterClass
    public static void afterClass() throws Exception {
        globalTearDown();
    }
 
    @Test
    public void test() throws Exception {      
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);
        browser.navigate("http://newtours.demoaut.com");
        browser.describe(EditField.class, new XPathDescription("//input[@name='userName']")).setValue("test");
        browser.describe(EditField.class, new XPathDescription("//input[@name='password']")).setValue("test");
        browser.describe(Image.class, new XPathDescription("//input[@name='login']")).click();
        browser.close();
    }
}

Running the above code will generate the below report in your projectFolder/RunResults directory. View the live report here.

If a security alert pops up while executing the above code, click ‘Allow Access’ to allow LeanFT to communicate over network.

If you would like to use another test framework or have your own custom implementation, extending UnitTestBaseClass is not required. For that, add %LeanFT%/SDK/Java directory to your PATH and use the code below to perform the initial settings for your run:

ModifiableSDKConfiguration config = new ModifiableSDKConfiguration();
config.setServerAddress(new URI("ws://localhost:5095"));
SDK.init(config);
Reporter.init();

In the above configuration, I have used the default port 5095. It is possible to use another port, visit this link for more information.

Let’s use the above configuration to run the same code as the template from the main() method:

import com.hp.lft.report.Reporter;
import com.hp.lft.sdk.ModifiableSDKConfiguration;
import com.hp.lft.sdk.SDK;
import com.hp.lft.sdk.web.*;
 
public class LeanFtMercuryTours {
    public static void main(String[] args) throws Exception {
        // initial setup for your custom implementation
        ModifiableSDKConfiguration config = new ModifiableSDKConfiguration();
        config.setServerAddress(new URI("ws://localhost:5095"));
        SDK.init(config);
        Reporter.init();
 
        // test code
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);
        browser.navigate("http://newtours.demoaut.com");
        browser.describe(EditField.class, new XPathDescription("//input[@name='userName']")).setValue("test");
        browser.describe(EditField.class, new XPathDescription("//input[@name='password']")).setValue("test");
        browser.describe(Image.class, new XPathDescription("//input[@name='login']")).click();
        browser.close();
 
        Reporter.generateReport();
        SDK.cleanup();
    }
}

Attaching to an Existing Browser

LeanFT has the ability to attach to any open browser using the attach() method.

BrowserDescription browserDesc = new BrowserDescription.Builder().title("Welcome: Mercury Tours").build();
Browser browser = BrowserFactory.attach(browserDesc);

This may not come as a surprise if are a QTP developer, but a well desired functionality if you are switching over from Selenium.

Reporter

Another feature/usage that QTP users would find similar is with the Reporter object. To report events, simply call Reporter.reportEvent as you would in QTP.

Reporter.reportEvent("StepName", "Description", Status.Passed);

Limitations

Below are a few limitations with LeanFT (from the docs):

  • The LeanFT Eclipse plugin currently does not provide the application model functionality.
  • The LeanFT HTML Report cannot be viewed in a Firefox browser.
  • When using the Highlight option in the application model for a test object with VRI objects defined, the highlight operation succeeds only if exactly one object matches the description (including the VRI definitions). If multiple objects match, nothing is highlighted. Workaround: Use the VRI Preview option to check the description.

Viewing all articles
Browse latest Browse all 4

Trending Articles