Open topic with navigation

# NativeObject code samples (.NET 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.

    /*
     * 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()
    {
           //Launch Internet Explorer and navigate to the New Tours web site.
           var browser = BrowserFactory.Launch(BrowserType.InternetExplorer);
           browser.Navigate("http://newtours.demoaut.com/");

           //Describe the user name edit field on the page.
           var userName = browser.Describe<IEditField>(new EditFieldDescription
           {
    		Type = @"text",
    		TagName = @"INPUT",
    		Name = @"userName"
           });

           //Set the text using a test object method.
           userName.SetValue("SomeName");

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

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

           //Access the text value using the  native object's value property.
           var value = nativeObject.value;

           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.value = "AnotherName";

           //Verify that the value successfully modified the text in the edit box.
           Verify.AreEqual("AnotherName", userName.Value, "Verify NativeObject Updated Value", "Verify that the NativeObject's updated value is correct.");
    }

Back to top

## Use native object index to get or modify values from a JavaScript array

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

    /*
     * This test demonstrates how to get\modify values from a JavaScript array using the native object index.
     */
    [Test]
    public void NativeObject_JavaScriptArray_Chrome()
    {
           ////Launch Chrome and navigate to the New Tours web site.
           var browser = BrowserFactory.Launch(BrowserType.Chrome);
           browser.Navigate("http://newtours.demoaut.com/");

           //Describe the Body web element on the page.
           var webElement = browser.Describe<IWebElement>(new WebElementDescription
           {
    		TagName = "Body"
           });

           //Dynamically add a JavaScript array to the body element
           browser.Page.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.
           var arr = webElement.NativeObject.array;

           //Verify that the returned array is not null.
           Verify.IsNotNullOrEmpty(arr, "Verify Returned Array", "Verify that the returned array is not null.");

           //Get one of the values value using the array index.
           Verify.AreEqual(1, arr[0], "Verify Some Value", "Verify one of the values using the array index.");

           //Set a value using the array index.
           arr[1] = 100;
           Verify.AreEqual(100, arr[1], "Set A Value", "Verify setting a value in the array using the array index.");
    }

Back to top