Skip to content

Executing JavaScript in the Browser

When UI-licious tests run, they execute in the context of the test runner, which is a separate process from the browser itself. Because of this separation, test scripts cannot directly access browser APIs such as window or document, nor can they access global objects or functions exposed by the application.

To run JavaScript in the context of the browser page, UI-licious provides UI.execute().

Usage

You can pass a script to execute as either a string or a function.

js
UI.execute("document.querySelector('#username').value = 'john'")

// or

UI.execute(() => {
	document.querySelector('#username').value = 'john'
})

Passing arguments

The browser context does not have access to values defined in the test runner. If you need to use values from the test script, pass them as arguments to UI.execute().

UI.execute() accepts an array of arguments as its second parameter. Each item in the array is passed to the function in order.

js
// executes in the test runner
var user = {
	username : "john",
	password : "supersecretpassword"
}

UI.execute((value) => {
	// executes in the browser
	document.querySelector('#username').value = value
}, [user.username])

In this example, user exists only in the test runner. The value of user.username is passed explicitly into the browser context as an argument.

Returning values

You can return values from the browser context back to the test runner by returning them from the function.

js
var pageInfo = UI.execute(() => {
	return {
		url : document.location.href,
		title : document.title
	}
})