NativeObject code examples (Java SDK)

Basic usage of NativeObject on a Web application

This example shows basic usage of NativeObject on a Web application.

@Test
public void nativeObjectBasicTest() throws GeneralLeanFtException {
	//Launch Internet Explorer and navigate to the New Tours web site.
	Browser browser = BrowserFactory.launch(BrowserType.INTERNET_EXPLORER);
	browser.navigate("http://newtours.demoaut.com/");

	//Describe the user name edit field on the page.
	EditField userName = browser.describe(EditField.class, new EditFieldDescription.Builder()
		.type("text").tagName("INPUT").name("userName").build());

	//set the text using the text object
	userName.setValue("SomeName");

	//Get the native object of the user name edit field.
	NativeObject nativeObject = userName.getNativeObject();

	//Access the native object's tagName property and verify that a non-null value is returned.
	Verify.isNotNullOrEmpty(nativeObject.getProperty("tagName", String.class), "Verify TagName Proeprty", "Verify the TagName property is not null.");

	//Access the text value using the  native object's value property.
	String value = nativeObject.getProperty("value", String.class);;

	Verify.areEqual("SomeName", value, "Verify NativeObject Value", "Verify that the NativeObject's value is correct.");

	//change the text using the native object's value property.
	nativeObject.setProperty("value", "AnotherName");

	//Verify that the value was modified using the test object.
	Verify.areEqual("AnotherName", userName.getValue(), "Verify NativeObject Updated Value", "Verify that the NativeObject's updated value is correct.");
}

Back to top

Use native object indexer to get/modify values from JavaScript array

This test demonstrates how to get or modify values from a JavaScript array using the native object indexer.

@Test
public void nativeObject_JavaScriptArray_Chrome() throws GeneralLeanFtException{
	//Launch Chrome browser and navigate to http://newtours.demoaut.com/
	Browser browser = BrowserFactory.launch(BrowserType.CHROME);
	browser.navigate("http://newtours.demoaut.com/");

	//describe the Body web element.
	WebElement webElement = browser.describe(WebElement.class, new WebElementDescription.Builder().tagName("Body").build());

	//dynamically add a JavaScript array to the body element
	browser.getPage().runJavaScript("(function(){ document.body.array = [1,2,3,4];})();");

	//get the array from the Body native object - the array itself is also returned as a native object.
	NativeObject arr = webElement.getNativeObject().getProperty("array", NativeObject.class);

	//assert the returned array is not null.
	assertNotNull(arr);

	//get value using invokeGetIndexer
	Verify.areEqual(1, arr.getItem(Integer.class, 0).intValue(), "Verify Some Value", "Verify one of the values using the array index.");

	//set value using invokeSetIndexer
	arr.setItem(1, 100);
	Verify.areEqual(100, arr.getItem(Integer.class, 1).intValue(), "Set A Value", "Verify setting a value in the array using the array index.");
}

Back to top