NativeObject code examples

 

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() throws GeneralLeanFtException {
	//Launch Internet Explorer and navigate to the New Tours web site.
	Browser browser = BrowserFactory.launch(BrowserType.INTERNET_EXPLORER);
	browser.navigate("http://newtours.demoaut.com/");

	//Describe the user name edit field on the page.
	EditField userName = browser.describe(EditField.class, new EditFieldDescription.Builder()
		.type("text").tagName("INPUT").name("userName").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 assert that a non-null value is returned.
	assertNotNull(nativeObject.getProperty("tagName", String.class));

	//Access the text value using the  native object's value property.
	String value = nativeObject.getProperty("value", String.class);;

	assertEquals("SomeName", value);

	//Change the text using the native object's value property.
	nativeObject.setProperty("value", "AnotherName");

	//Assert that the value was modified using the test object.
	assertEquals("AnotherName", userName.getValue());
}

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 http://newtours.demoaut.com/
	Browser browser = BrowserFactory.launch(BrowserType.CHROME);
	browser.navigate("http://newtours.demoaut.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.
	assertNotNull(arr);

	//Get value using invokeGetIndexer
	assertEquals(1, arr.getItem(Integer.class, 0).intValue());

	//Set value using invokeSetIndexer
	arr.setItem(1, 100);
	assertEquals(100, arr.getItem(Integer.class, 1).intValue());
}

Back to top