NativeObject code samples (.NET SDK)

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.
public void NativeObjectBasicTest()
    //Launch Internet Explorer and navigate to the Advantage Online Shopping web site.
    IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer);

    //open login dialogvar userMenuLink = browser.Describe<ILink>(new LinkDescription
        InnerText = @"My account My orders Sign out ",
        TagName = @"A"

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

    //Set the text using the text object

    //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 Property", "Verify the TagName property is not null.");

    //Access the text value using the  native object's value property.
    String 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 was modified using the test object.
    Verify.AreEqual("AnotherName", userName.Value, "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.
    var styleNativeObject =;

    //The styleNativeObject is of type NativeObject so we can get its properties the same way as we did for the nativeObject.
    int length = styleNativeObject.length;

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

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.

public void NativeObject_JavaScriptArray_Chrome()
    //Launch Chrome browser and navigate to
    IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer);

    //Describe the Body web element.
    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;

    //assert the returned array is not null.

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

    //set value using invokeSetIndexer
     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