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.
[Test] public void NativeObjectBasicTest() { //Launch Internet Explorer and navigate to the Advantage Online Shopping web site. IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer); browser.Navigate("https://www.advantageonlineshopping.com/"); //open login dialogvar userMenuLink = browser.Describe<ILink>(new LinkDescription { InnerText = @"My account My orders Sign out ", TagName = @"A" }); userMenuLink.Click(); //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 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 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 = nativeObject.style; //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); }
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.
[Test] public void NativeObject_JavaScriptArray_Chrome() { //Launch Chrome browser and navigate to https://www.advantageonlineshopping.com/ IBrowser browser = BrowserFactory.Launch(BrowserType.InternetExplorer); browser.Navigate("https://www.advantageonlineshopping.com/"); //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. Assert.NotNull(arr); //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."); }