Share this page

# tagName NativeObject code examples (Java SDK)

In this topic:

## Basic usage of NativeObject on a Web application

This example shows basic use 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 Advantage Online Shopping web site.
        Browser browser = BrowserFactory.launch(BrowserType.INTERNET_EXPLORER);
        browser.navigate("https://www.advantageonlineshopping.com/");

        //open login dialog
        Link userMenuLink = browser.describe(Link.class, new LinkDescription.Builder()
                            .innerText("My account My orders Sign out ")
                            .tagName("A").build());
                            userMenuLink.click();

        //Describe the user name edit field on the page.
        EditField userName = browser.describe(EditField.class, new EditFieldDescription.Builder()
                            .name("username")
                            .tagName("INPUT")
                            .type("text").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 Property", "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);

        System.out.print(styleNativeObject);

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

        //verify that length is 0.
        Verify.isTrue(length==0);
    }

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 https://www.advantageonlineshopping.com/
        Browser browser = BrowserFactory.launch(BrowserType.CHROME);
        browser.navigate("https://www.advantageonlineshopping.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.
        Assert.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

Tips to improve your search results... UFT Developer Java SDK Reference Version 15.0-15.0.2 UFT Developer SDK Reference All View Local Help Center View Online Help Center back Go back in browser history forward Go forward in browser history Remove search highlights next topic Go to next topic previous topic Go to previous topic Print the current topic Version: