Setting up and working with the JavaScript SDK

This page covers the basics of installing the JavaScript SDK and structuring your JavaScript test.

Install the JavaScript SDK as a Node.js module

Before you can use the LeanFT JavaScript SDK that was installed with LeanFT, you must install it as a Node.js module.

  1. If a proxy server is used in your organization, you must configure the npm proxy (contact your administrator if needed).

    From a command line, run the following command:

    • For http:

      > npm config set proxy http://<Your Proxy server address or IP>:<Proxy server port>

    • For https:

      > npm config set https-proxy http://<Your Proxy server address or IP>:<Proxy server port>

    For more information, see the npm documentation.

  2. Create a folder for your testing project where you will save all your JavaScript tests.

  3. If you are working with the Jasmine or Mocha frameworks, use the relevant npm install command to install the framework.

    Jasmine

    1. To install, go to https://www.npmjs.com/package/jasmine.
    2. After installation, initialize your testing project:

      In a command line, change to the directory you created for your test project, and run

      > jasmine init

      to create the spec directory and configuration json.

    Mocha:

    To install, go to https://mochajs.org/.

  4. Install the LeanFT JavaScript SDK in your testing project folder:

    1. In the command line, change to the directory that you created for your testing project.
    2. Run the following command:

      Note: Be sure to include the space+period (" .") at the very end.

      Windows <LeanFT installation folder>\SDK\JavaScript\js_installation.bat .
      Linux / Mac <LeanFT installation folder>/sdk/JavaScript/js_installation.sh .

Back to top

Structure your JavaScript test

To create a JavaScript test:

  1. Copy the following template.

    Tip: The elements of the template are describe in the table below.

    Note:  

    • You can write your test in any IDE that supports JavaScript, or in any other text editor.
    • This template uses BDD style.
    • This template can be used both for Jasmine and Mocha. Note the inline comments, however, regarding the difference for each.
    var LFT = require("leanft");
    var SDK = LFT.SDK;
    var Web = LFT.Web;
    var expect = require("leanft/expect");
    var whenDone = LFT.whenDone;
      
    
    describe("HP Site",function(){
           // set the default Jasmine time out (if not using Jasmine: remove this line)
    	jasmine : jasmine.DEFAULT_TIMEOUT_INTERVAL = 30 * 1000;
    
    	// set the default Mocha time out (if not using Mocha: remove this line)
    //mocha: this.timeout(30 * 1000);
    var browser; // For Jasmine, use beforeAll(); for Mocha, use before() beforeAll(function(done){
    		LFT.init(); 
    		 whenDone(done);
    	});
    	
    	beforeEach(function(done){
    		LFT.beforeTest();        
    		Web.Browser.launch("chrome").then(function(launchedBrowser){
    			browser = launchedBrowser;
    		})
    		whenDone(done);
    	});
    
    	it("Should describe the purpose of your test case",function(done){
    		//Navigate to the web site you want to test
    		browser.navigate("http://54.210.48.65:8080");
    
    		//Add steps here
    		browser.$(Web.Element({
    			id: "TabletsImg"
    		})).click();
    
    		//gets the list of results
    var firstProduct = browser.$(Web.Element({ tagName: "LI", innerText: /.*HP ElitePad 1000 G2.*/ })); //gets the price
    var priceElement = firstProduct.$(Web.Element({ tagName: "a", className: /.*productPrice.*/ })); expect(priceElement.innerText()).toEqual("$1,009.00 "); whenDone(done); }); afterEach(function(done){ LFT.afterTest(); if(browser){ browser.close(); } whenDone(done); }); afterAll(function(done){ LFT.cleanup(); whenDone(done); }); });

     

    Element Description
    vars

    Variables that define the required LeanFT modules to include and simplified names for the SDK technologies you need to reference.

    Timeout interval

    Must be defined at the head of every test.

    • Jasmine: jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;
    • Mocha: //this.timeout(20*1000);
    LFT.init

    This step connects to the LeanFT runtime engine and enables the creation of the HTML run report. It must run before each test.

    Use this step to configure the runtime engine address and HTML run report details. For details, see Configure the LeanFT runtime engine and HTML report.

    Note: This step does not start the runtime engine. You must ensure that the LeanFT runtime engine is active before you run your JavaScript test.

    browser (parent object) setup
    • The browser variable is defined with global scope at the test fixture level.

    • The Web.Browser.launch step is defined in the beforeEach section, and closed in the afterEach section, so that a clean browser is launched for each test case.

      If you want to launch browsers with different properties in different test cases, you can launch the browser within the test case.

      You can also use Web.Browser.attach steps inside your test case to perform steps on new browsers or tabs opened during the test.

      The launched browser is returned to the launchedbrowser variable when the step's promise is fulfilled. Only then, the browser variable is set. For more details, see JavaScript: Synchronization, promises, and error handling.

      Note: The template provided on this page is for a Web application.

      For a mobile application, use the same logic. But instead of launching a browser, use one of the lock device methods available for the LFT.Mobile.MobileLab object.

    it The test case section. You can have as many test cases as required in your JavaScript file. This section often starts with navigating to your AUT, followed by the required test steps.
    afterEach Runs after each test case. It closes the browser, clears the SDK initialization, and synchronizes the final done with the LeanFT runtime engine.
  2. Save the file:

    Jasmine

    Save the file in the spec subfolder under the testing project folder that you created when setting up the JavaScript SDK.

    Use the following format:

    <myjavascripttest>_spec.js
    Mocha:

    In the testing project folder that you created when setting up the JavaScript SDK, create a subfolder called test.

    Save the file in the test folder.

  3. Create test object descriptions. See Describe JavaScript test objects below.

Back to top

Describe JavaScript test objects

To create LeanFT steps that perform operations on your application, you need to uniquely describe a test object in your application, and you need to specify a LeanFT method or property that is supported for that test object type.

For details on the available test object types and supported operations, see the JavaScript SDK Reference.

See also:

Back to top

Find child objects

You can use the .$$ notation to find all child objects of an object in your application, or all child objects that match a specified filter.  

To get all child objects:

TestObject.$$(null,true).then........

To get all child objects of a specified type:

TestObject.$$(Technology.Type()).then........

To get all child objects that match the specified filter:

TestObject.$$(Technology.Type{prop1:value1, prop2:value2}).then........

For example:

browser.$$(Web.Link{innerText: /.*buy.*/}).then(function(links){
    //do something with the returned links
})

Back to top

Validate values

You can use the assert and expect functions supported by your testing framework to validate expected values. This information is included in the run results in the HTML report.

In addition, LeanFT provides customized expect and verify functions.

  • The LeanFTexpect function differ from Jasmine's in that you can use a catch-then callback to avoid a false expectation from failing the test.

  • The LeanFTverify function compares the actual value against the expected value, and returns a Boolean response.

  • The LeanFTexpect/verify functions support the following matchers, including negative (.not) assertions:

    toBe toEqual. toNotEqual toBeNull
    toBeCloseTo(value, NumOfDecimalDigits) toBeFalsey, toBeFalsy toBeTruthy
    toContain toBeGreaterThan, toBeLessThan toBeUndefined
    toBeDefined toMatch  

    The syntax for these matchers is the same as the Jasmine syntax.
    For details, see the information about expectations/verfications and matchers at http://jasmine.github.io/2.4/introduction.html

For example:

it("should provide search results in auto-complete after user types a query",function(done){
	//Navigate to the web site under test.
	browser.navigate("http://www.mySite.com");
	//Open the search widget.
	browser.$({id: "search_widget"}).click();
	//Type in the search.
	browser.$(Web.Edit({name: "abc"})).setValue("xyz");
	//Verify that the results appear on the screen.var results = browser.$(".js_results");
	expect(results.exists()).toBeTruthy();
	verify(results.exists()).toBeTruthy();
	whenDone(done);
});

Back to top

Run your test

  1. Manually start the LeanFT runtime engine.

  2. In a command line, change to the directory you created for your test project, and run:

    Jasmine

    > jasmine

    Mocha:

    Windows: > mocha –reporter=.\node_modules\leanft.report\mocha\mochaReporter.js

    Linux/Mac: > mocha –reporter=./node_modules/leanft.report/mocha/mochaReporter.js

    After the run completes, you can open the generated HTML run report. The report is created in the RunResults folder under your testing project folder. For details on customizing your report, see JavaScript: Configuring the LeanFT runtime engine and HTML report.

Back to top

JavaScript limitations

The following LeanFT features are not supported when working with the JavaScript SDK:

  • Application models
  • ALM/BPT integration

Back to top

See also: