Script
Commands
This section contains the APIs related to script commands.
Call function in a browsing context
try (Script script = new Script(id, driver)) {
List<LocalValue> arguments = new ArrayList<>();
arguments.add(PrimitiveProtocolValue.numberValue(22));
Map<Object, LocalValue> value = new HashMap<>();
value.put("some_property", LocalValue.numberValue(42));
LocalValue thisParameter = LocalValue.objectValue(value);
arguments.add(thisParameter);
EvaluateResult result =
script.callFunctionInBrowsingContext(
id,
"function processWithPromise(argument) {\n"
+ " return new Promise((resolve, reject) => {\n"
+ " setTimeout(() => {\n"
+ " resolve(argument + this.some_property);\n"
+ " }, 1000)\n"
+ " })\n"
+ "}",
true,
Optional.of(arguments),
Optional.of(thisParameter),
Optional.of(ResultOwnership.ROOT));
const manager = await ScriptManager(id, driver)
let argumentValues = []
let value = new ArgumentValue(LocalValue.createNumberValue(22))
argumentValues.push(value)
let mapValue = {some_property: LocalValue.createNumberValue(42)}
let thisParameter = new ArgumentValue(LocalValue.createObjectValue(mapValue)).asMap()
const result = await manager.callFunctionInBrowsingContext(
id,
'function processWithPromise(argument) {' +
'return new Promise((resolve, reject) => {' +
'setTimeout(() => {' +
'resolve(argument + this.some_property);' +
'}, 1000)' +
'})' +
'}',
true,
argumentValues,
thisParameter,
ResultOwnership.ROOT)
Call function in a sandbox
try (Script script = new Script(id, driver)) {
EvaluateResult result =
script.callFunctionInBrowsingContext(
id,
"sandbox",
"() => window.foo",
true,
Optional.empty(),
Optional.empty(),
Optional.empty());
const manager = await ScriptManager(id, driver)
await manager.callFunctionInBrowsingContext(id, '() => { window.foo = 2; }', true, null, null, null, 'sandbox')
Call function in a realm
try (Script script = new Script(tab, driver)) {
List<RealmInfo> realms = script.getAllRealms();
String realmId = realms.get(0).getRealmId();
EvaluateResult result = script.callFunctionInRealm(
realmId,
"() => { window.foo = 3; }",
true,
Optional.empty(),
Optional.empty(),
Optional.empty());
const manager = await ScriptManager(firstTab, driver)
const realms = await manager.getAllRealms()
const realmId = realms[0].realmId
await manager.callFunctionInRealm(realmId, '() => { window.foo = 3; }', true)
Evaluate script in a browsing context
try (Script script = new Script(id, driver)) {
EvaluateResult result =
script.evaluateFunctionInBrowsingContext(id, "1 + 2", true, Optional.empty());
const manager = await ScriptManager(id, driver)
const result = await manager.evaluateFunctionInBrowsingContext(id, '1 + 2', true)
Evaluate script in a sandbox
try (Script script = new Script(id, driver)) {
EvaluateResult result =
script.evaluateFunctionInBrowsingContext(
id, "sandbox", "window.foo", true, Optional.empty());
const manager = await ScriptManager(id, driver)
await manager.evaluateFunctionInBrowsingContext(id, 'window.foo = 2', true, null, 'sandbox')
const resultInSandbox = await manager.evaluateFunctionInBrowsingContext(id, 'window.foo', true, null, 'sandbox')
Evaluate script in a realm
try (Script script = new Script(tab, driver)) {
List<RealmInfo> realms = script.getAllRealms();
String realmId = realms.get(0).getRealmId();
EvaluateResult result =
script.evaluateFunctionInRealm(
realmId, "window.foo", true, Optional.empty());
const manager = await ScriptManager(firstTab, driver)
const realms = await manager.getAllRealms()
const realmId = realms[0].realmId
await manager.evaluateFunctionInRealm(realmId, 'window.foo = 3', true)
const result = await manager.evaluateFunctionInRealm(realmId, 'window.foo', true)
Disown handles in a browsing context
Disown handles in a realm
Get all realms
try (Script script = new Script(firstWindow, driver)) {
List<RealmInfo> realms = script.getAllRealms();
const manager = await ScriptManager(firstWindow, driver)
const realms = await manager.getAllRealms()
Get realm by type
try (Script script = new Script(firstWindow, driver)) {
List<RealmInfo> realms = script.getRealmsByType(RealmType.WINDOW);
const manager = await ScriptManager(firstWindow, driver)
const realms = await manager.getRealmsByType(RealmType.WINDOW)
Get browsing context realms
try (Script script = new Script(windowId, driver)) {
List<RealmInfo> realms = script.getRealmsInBrowsingContext(tabId);
const manager = await ScriptManager(windowId, driver)
const realms = await manager.getRealmsInBrowsingContext(tabId)
Get browsing context realms by type
List<RealmInfo> windowRealms =
script.getRealmsInBrowsingContextByType(windowId, RealmType.WINDOW);
const realms = await manager.getRealmsInBrowsingContextByType(windowId, RealmType.WINDOW)
Preload a script
String id = script.addPreloadScript("() => { window.bar=2; }", "sandbox");
const manager = await ScriptManager(id, driver)
const scriptId = await manager.addPreloadScript('() => {{ console.log(\'{preload_script_console_text}\') }}')
Remove a preloaded script
Events
This section contains the APIs related to script events.
Message
try (Script script = new Script(driver)) {
CompletableFuture<Message> future = new CompletableFuture<>();
script.onMessage(future::complete);
script.callFunctionInBrowsingContext(
driver.getWindowHandle(),
"(channel) => channel('foo')",
false,
Optional.of(List.of(LocalValue.channelValue("channel_name"))),
Optional.empty(),
Optional.empty());
Message message = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals("channel_name", message.getChannel());
}
const manager = await ScriptManager(undefined, driver)
let message = null
await manager.onMessage((m) => {
message = m
})
let argumentValues = []
let value = new ArgumentValue(LocalValue.createChannelValue(new ChannelValue('channel_name')))
argumentValues.push(value)
const result = await manager.callFunctionInBrowsingContext(
await driver.getWindowHandle(),
'(channel) => channel("foo")',
false,
argumentValues,
)
Realm Created
try (Script script = new Script(driver)) {
CompletableFuture<RealmInfo> future = new CompletableFuture<>();
script.onRealmCreated(future::complete);
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate("https://www.selenium.dev/selenium/blankPage");
RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS);
Assertions.assertNotNull(realmInfo.getRealmId());
Assertions.assertEquals(RealmType.WINDOW, realmInfo.getRealmType());
}
const manager = await ScriptManager(undefined, driver)
let realmInfo = null
await manager.onRealmCreated((result) => {
realmInfo = result
})
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})
await browsingContext.navigate('https://www.selenium.dev/selenium/web/blank', 'complete')
Realm Destroyed
try (Script script = new Script(driver)) {
CompletableFuture<RealmInfo> future = new CompletableFuture<>();
script.onRealmDestroyed(future::complete);
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.close();
RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS);
Assertions.assertNotNull(realmInfo.getRealmId());
Assertions.assertEquals(RealmType.WINDOW, realmInfo.getRealmType());
}
const manager = await ScriptManager(undefined, driver)
let realmInfo = null
await manager.onRealmDestroyed((result) => {
realmInfo = result
})
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})
await browsingContext.close()