NativeObject code samples

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 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 assert that a non-null value is returned.
	Assert.NotNull(nativeObject.tagName);

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

	Assert.AreEqual("SomeName", value);

	//Change the text using the native object's value property.
	nativeObject.value = "AnotherName";

	//Assert that the value successfully modified the text in the edit box.
	Assert.AreEqual("AnotherName", userName.Value);
}

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.

[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;

	//Assert that the returned array is not null.
	Assert.NotNull(arr);

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

	//Set a value using the array index.
	arr[1] = 100;
	Assert.AreEqual(100, arr[1]);
}

Back to top