Open topic with navigation

# NativeObject code examples (Java SDK)

In this topic:

## Basic usage of NativeObject on a Web application

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

- It accesses a native object, checks native property values, and activates a native method.
- It then ensures that the method was performed as expected.

    @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.");

    	//An example of how to access a 2nd level property:
    	//The style property of the user name edit field native object is in itself a NativeObject.
    	//Therefore, the value of the returnType parameter for getProperty needs to be set to NativeObject.class.
    	NativeObject styleNativeObject = nativeObject.getProperty("style", NativeObject.class);

    	//The styleNativeObject is of type NativeObject so we can get its properties the same way as we did for the nativeObject.
    	String color = styleNativeObject.getProperty("backgroundColor", String.class);

    	//verify that background color is white.
    	Verify.areEqual("white", color, "Verify the background color of the user name edit box", "Verify that the background color of the edit box is white");
    }

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