Web code samples (JavaScript SDK)

Web example

The following example shows

var assert = require("assert");
var LFT = require("leanft");
var expect = require("leanft/expect");
var fs = require("fs");
var Web = LFT.Web;
var ImageUtils = LFT.ImageUtils;
var browser;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;

// Browser name can be defined using the LFT_Browser env var, default is Chrome
var browserName = process.env.LFT_BROWSER || "Chrome";

describe("Web examples", function(){
	beforeAll(function(done){
		LFT.init();
		LFT.whenDone(done);
	});

	beforeEach(function(done){
		LFT.beforeTest();

		Web.Browser.launch(Web.BrowserType[browserName]).then(function(launched_browser){
			browser = launched_browser;
		});

		LFT.whenDone(done);
	});

	it("should show basic usage - navigate a web browser to a web site", function(done){
		browser.navigate("https://www.advantageonlineshopping.com/");
		var speakersCategoryLink = browser.$(Web.Link({
			innerText: "SPEAKERS Shop Now ",
		 	tagName: "DIV"
		}));
		speakersCategoryLink.click();
		browser.sync();
		var url = browser.url();
		expect(url).toBe("https://www.advantageonlineshopping.com/#/category/Speakers/4");

		LFT.whenDone(done);
	});
       
       it("should validate that login was successful", function(done){
           // Navigate to the Advantage Online Shopping web site.
	    browser.navigate("https://www.advantageonlineshopping.com/");

	    // Wait for the browser to complete the navigation.
	    browser.sync();

           // Open the logon dialog.
           var logonDialog = browser.$(Web.Element({
	        accessibilityName: "",
	        index: 7,
	        innerText: "",
	        tagName: "path"
	    }));
	    logonDialog.click();

	    var usernameEditField = browser.$(Web.Edit({
	        name: "username",
	        tagName: "INPUT",
	        type: "text"
	    }));

           var passwordEditField = browser.$(Web.Edit({
               name: "password",
               tagName: "INPUT",
               type: "password"
	    }));

	    // Set user and password.
	    usernameEditField.setValue("mercury");
	    passwordEditField.setValue("55940Fd62c96");
	
	    // Press the login button.
	    var signInButton = browser.$(Web.Button({
	        buttonType: "button",
	        name: "SIGN IN",
	        tagName: "BUTTON"
           }));
	    signInButton.click();
	
	    // Identify and verify that the username is visible on the top-right of the page after loggin in.
	    var mercuryWebElement = browser.$(Web.Element({
	        className: "hi-user containMiniTitle ng-binding",
	        innerText: "mercury",
	        tagName: "SPAN"
	    }));
	
	    expect(mercuryWebElement.isVisible()).toBeTruthy();
           LFT.whenDone(done);
	});			

	it("should find advantage demo link using XPath and verify the object. ", function(done){
	    // Navigate to the Advantage Online Shopping web site.
	    browser.navigate("https://www.advantageonlineshopping.com/");

	    // Identify the demo link using the XPath property.
	    var advantageDemoLink = browser.$(Web.Link({
	        xpath: "//HEADER[1]/NAV[1]/DIV[1]/A[@role=\"link\"][1]"
	    }));
	
	    // Verify that the found element is correct.
	    expect(advantageDemoLink.tagName()).toEqual("A");

	    LFT.whenDone(done);
	});

	it("should print products price from shopping cart table. ", function(done){
	    // Navigate to the Advantage Online Shopping web site.
	    browser.navigate("https://www.advantageonlineshopping.com/");
	
	    // Go to the "Speakers" link.
	    var speakersCategoryLink = browser.$(Web.Link({
	        innerText: "SPEAKERS Shop Now ",
	        tagName: "DIV"
	    }));
	    speakersCategoryLink.click();

	    // Go to the "Bose Soundlink Bluetooth Speaker" product page.
	    var boseSoundlinkBluetoothSpeaker = browser.$(Web.Element({
	        innerText: "SOLD OUT SHOP NOW Bose Soundlink Bluetooth Speaker III $269.99 ",
	        tagName: "LI"
	    }));
	    boseSoundlinkBluetoothSpeaker.click();

	    // Add the product to the cart.
	    var saveToCartButton = browser.$(Web.Button({
	        buttonType: "submit",
	        name: "ADD TO CART",
	        tagName: "BUTTON"
	    }));
	    saveToCartButton.click();

	    // Navigate back to the "Speakers" category.
	    var speakersLink = browser.$(Web.Link({
	        innerText: "SPEAKERS ",
		tagName: "A"
	    }));
	    speakersLink.click();
	
	    // Go to the "Bose Soundlink Wireless Speaker" product page.
	    var BoseSoundLinkWirelessSpeaker = browser.$(Web.Element({
	        innerText: "SOLD OUT SHOP NOW Bose SoundLink Wireless Speaker $129.00 ",
	        tagName: "LI"
	    }));
	    BoseSoundLinkWirelessSpeaker.click();

	    // Add the product to the cart.
	    saveToCartButton.click();

	    // Go to the shopping cart link.
	    var shoppingCartLink = browser.$(Web.Link({
               accessibilityName: "ShoppingCart",
	        role: "link",
	        tagName: "A"
	    }));
	    shoppingCartLink.click();

	    // Identify the shopping cart table element.
	    var shoppingCartTable = browser.$(Web.Table({
	        accessibilityName: "",
		index: 1,
		role: "",
		tagName: "TABLE"
	    }));
	
	    // Print the table contents to the console.
	    shoppingCartTable.cells().then(function(cells){
	        var numberOfRows = cells.length;
	        for (var i = 1; i < numberOfRows-1; i++){
                   shoppingCartTable.cells(i, 4).text().then(function(cellText){
	                console.log("cellText: " + cellText);
	                var cellTextString = "" + cellText;
	                console.log("char at 0: " + cellTextString.charAt(0));
	            });
               } 
	        console.log("Finished going over rows and cells");
	    }); 

	    LFT.whenDone(done);
	});

	it("should show how to retrieve products category from products listbox.  ", function(done){
	    // Navigate to the Advantage Online Shopping web site.
	    browser.navigate("https://www.advantageonlineshopping.com/");
	

	    // Go to the "CONTACT US" link
	    var contactLink = browser.$(Web.Link({
	        innerText: "CONTACT US",
	        tagName: "A"
	    }));
	    contactLink.click();
	
	    // Identify the categories list.
	    var categoryListbox = browser.$(Web.ListBox({
	        name: "categoryListboxContactUs",
	        tagName: "SELECT"
           }));

	    // Iterate through the list items.
	    categoryListbox.items().then(function(category){
	        var numberOfProductCategories = category.length-1;
	        console.log("number of category: " + numberOfProductCategories);
	        for (var i = 1; i < numberOfProductCategories; i++){
	            console.log("Category #" + i + ": " + category[i]);
	        }

	        // Verify that the "Laptops" category exists in the list.
	        expect(category[1]).toBe("Laptops");
	   });

           LFT.whenDone(done);
	});

	it("should verify down arrow menu is available and use VRI to locate objects", function(done){
	    // Navigate to the Advantage Online Shopping web site.
	    browser.navigate("https://www.advantageonlineshopping.com/");
	
	    // Wait for the browser to complete the navigation.
	    browser.sync();
	
	    // Set the browser to fullscreen to position the elements correctly.
	    browser.fullScreen();
	
	    // Identify the menu help element.
	    var menuHelpWebElement = browser.$(Web.Element({
	        accessibilityName: "",
		index: 5,
		innerText: "",
		tagName: "svg"
	    }));
	
	    // Identify the help arrow by using VRI.
	    var downArrowImage = browser.$(Web.Image({
	        vri: [{anchor: menuHelpWebElement, horizontal:LFT.Horizontal.right, hInline: true}]
	    }));
	
	    // Verify that the arrow is visible.
	    expect(downArrowImage.isVisible()).toBeTruthy();
	
	    LFT.whenDone(done);
	});

	
	it("should get the tagName attributes of all the images", function(done){
           // Navigate to the Advantage Online Shopping web site.
	    browser.navigate("https://www.advantageonlineshopping.com/");
		
	    // Go to the speakers category.
	    var speakersCategoryLink = browser.$(Web.Link({
	         innerText: "SPEAKERS Shop Now ",
	         tagName: "DIV"
           }));
	    speakersCategoryLink.click()

	    // Find all images inside the page and verify that their tagName attribute is not null.
	    browser.$$(Web.Image()).then(function(images){
	        images.forEach(function(image){
	            expect(image.getAttribute("tagName")).not.toBeNull();
	        });
	    });

	    LFT.whenDone(done);
	});
				
	afterEach(function(done){
		LFT.afterTest();
		if (browser){
			browser.close();
		}
		LFT.whenDone(done);
	});

	afterAll(function(done){
		LFT.cleanup();
		LFT.whenDone(done);
	});
});	

Back to top

Expect - one liners

The following code shows a few one-liner examples of how you can use expect.

var LFT = require("leanft");
var expect = require("leanft/verify");


// expect one liner examplesfunction showExpectUsages()
{
	var a=1;
	var b=2;
	var string1="SomeString";
	var trueValue = true;
	var falseValue = false;

	expect(a).toEqual(1);
	expect(b).toNotEqual(1);
	expect(string1).toBe("SomeString");
	expect(trueValue).toBeTruthy();
	expect(falseValue).toBeFalsey();

	expect(b).toBeGreaterThan(1);
	expect(a).toBeLessThan(2);
}

Back to top

Test on an emulated device using Google Chrome's Device mode

If the device on which you want test your app is not connected to UFT Mobile, or if UFT Mobile does not support the device, you can use Google Chrome's Device Mode to emulate the device.

The following example shows how to test on an emulated iPhone 6:

it("Chrome browser should open for predefined emulated device",function(done){
	Web.Browser.launchEmulated(Web.BrowserType.Chrome,EmulatedDevice.IPhone6).then(function(b){
		browser = b;
		browser.navigate("www.google.com");
	});

	LFT.whenDone(done);

});

The following example shows how test on a custom emulated device:

it("Chrome browser should open for custom emulated device",function(done){
	var env = {
		width: 375,
		height: 667,
		scaleFactor: 2,
		userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4",
		isTouchable: true,
	};
    
	Web.Browser.launchEmulated(Web.BrowserType.Chrome,env).then(function(b){ //EmulatedDevice.IPhone6
		browser = b;
		browser.navigate("www.google.com");
	});

	LFT.whenDone(done);
});

Back to top

See also: