From 6b28a8c03feb0c4121d07299a18444dcbc34b35e Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 10 Jul 2024 16:08:47 +0530 Subject: [PATCH] [bidi] Add source type to log entry (#14244) --- .../selenium/bidi/log/BaseLogEntry.java | 11 ++++++- .../selenium/bidi/log/ConsoleLogEntry.java | 25 ++++++--------- .../selenium/bidi/log/GenericLogEntry.java | 17 ++++++++-- .../selenium/bidi/log/JavascriptLogEntry.java | 17 ++++++++-- .../selenium/bidi/log/LogInspectorTest.java | 14 ++++---- .../grid/router/RemoteWebDriverBiDiTest.java | 5 ++- .../selenium-webdriver/bidi/logEntries.js | 32 +++++++++---------- .../selenium-webdriver/bidi/logInspector.js | 8 +++-- .../test/bidi/log_inspector_test.js | 17 +++++++--- 9 files changed, 93 insertions(+), 53 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/log/BaseLogEntry.java b/java/src/org/openqa/selenium/bidi/log/BaseLogEntry.java index 9eac083468246..552b27134a447 100644 --- a/java/src/org/openqa/selenium/bidi/log/BaseLogEntry.java +++ b/java/src/org/openqa/selenium/bidi/log/BaseLogEntry.java @@ -17,11 +17,14 @@ package org.openqa.selenium.bidi.log; +import org.openqa.selenium.bidi.script.Source; + // @see https://w3c.github.io/webdriver-bidi/#types-log-logentry public class BaseLogEntry { private final LogLevel level; + private Source source; private final String text; private final long timestamp; private final StackTrace stackTrace; @@ -42,8 +45,14 @@ public StackTrace getStackTrace() { return stackTrace; } - public BaseLogEntry(LogLevel level, String text, long timestamp, StackTrace stackTrace) { + public Source getSource() { + return source; + } + + public BaseLogEntry( + LogLevel level, Source source, String text, long timestamp, StackTrace stackTrace) { this.level = level; + this.source = source; this.text = text; this.timestamp = timestamp; this.stackTrace = stackTrace; diff --git a/java/src/org/openqa/selenium/bidi/log/ConsoleLogEntry.java b/java/src/org/openqa/selenium/bidi/log/ConsoleLogEntry.java index 6f946576e4d29..62e95669b8ce9 100644 --- a/java/src/org/openqa/selenium/bidi/log/ConsoleLogEntry.java +++ b/java/src/org/openqa/selenium/bidi/log/ConsoleLogEntry.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.TreeMap; import org.openqa.selenium.bidi.script.RemoteValue; +import org.openqa.selenium.bidi.script.Source; import org.openqa.selenium.json.JsonInput; import org.openqa.selenium.json.TypeToken; @@ -31,21 +32,19 @@ public class ConsoleLogEntry extends GenericLogEntry { private final String method; - private final String realm; private final List args; public ConsoleLogEntry( LogLevel level, + Source source, String text, long timestamp, String type, String method, - String realm, List args, StackTrace stackTrace) { - super(level, text, timestamp, type, stackTrace); + super(level, source, text, timestamp, type, stackTrace); this.method = method; - this.realm = realm; this.args = args; } @@ -53,21 +52,17 @@ public String getMethod() { return method; } - public String getRealm() { - return realm; - } - public List getArgs() { return args; } public static ConsoleLogEntry fromJson(JsonInput input) { LogLevel level = null; + Source source = null; String text = null; long timestamp = 0; String type = null; String method = null; - String realm = null; List args = null; StackTrace stackTrace = null; @@ -78,6 +73,10 @@ public static ConsoleLogEntry fromJson(JsonInput input) { level = input.read(LogLevel.class); break; + case "source": + source = input.read(Source.class); + break; + case "text": text = input.read(String.class); break; @@ -94,10 +93,6 @@ public static ConsoleLogEntry fromJson(JsonInput input) { method = input.read(String.class); break; - case "realm": - realm = input.read(String.class); - break; - case "args": args = input.read(new TypeToken>() {}.getType()); break; @@ -114,18 +109,18 @@ public static ConsoleLogEntry fromJson(JsonInput input) { input.endObject(); - return new ConsoleLogEntry(level, text, timestamp, type, method, realm, args, stackTrace); + return new ConsoleLogEntry(level, source, text, timestamp, type, method, args, stackTrace); } private Map toJson() { Map toReturn = new TreeMap<>(); toReturn.put("type", super.getType()); + toReturn.put("source", super.getSource()); toReturn.put("level", super.getLevel()); toReturn.put("text", super.getText()); toReturn.put("timestamp", super.getTimestamp()); toReturn.put("method", method); - toReturn.put("realm", realm); toReturn.put("args", args); toReturn.put("stackTrace", super.getStackTrace()); diff --git a/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java b/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java index ad61b1d53bad8..a15a0af68bf1d 100644 --- a/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java +++ b/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.TreeMap; +import org.openqa.selenium.bidi.script.Source; import org.openqa.selenium.json.JsonInput; // @see toJson() { diff --git a/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java b/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java index ad64df37a8bfe..5a1ca42f07636 100644 --- a/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java +++ b/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.TreeMap; +import org.openqa.selenium.bidi.script.Source; import org.openqa.selenium.json.JsonInput; // @see toJson() { diff --git a/java/test/org/openqa/selenium/bidi/log/LogInspectorTest.java b/java/test/org/openqa/selenium/bidi/log/LogInspectorTest.java index bb36c5b4e6edc..be6e7a0f12f9c 100644 --- a/java/test/org/openqa/selenium/bidi/log/LogInspectorTest.java +++ b/java/test/org/openqa/selenium/bidi/log/LogInspectorTest.java @@ -36,6 +36,7 @@ import org.openqa.selenium.By; import org.openqa.selenium.WindowType; import org.openqa.selenium.bidi.module.LogInspector; +import org.openqa.selenium.bidi.script.Source; import org.openqa.selenium.environment.webserver.AppServer; import org.openqa.selenium.environment.webserver.NettyAppServer; import org.openqa.selenium.testing.JupiterTestBase; @@ -62,9 +63,10 @@ void canListenToConsoleLog() throws ExecutionException, InterruptedException, Ti driver.findElement(By.id("consoleLog")).click(); ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); - + Source source = logEntry.getSource(); + assertThat(source.getBrowsingContext().isPresent()).isTrue(); + assertThat(source.getRealm()).isNotNull(); assertThat(logEntry.getText()).isEqualTo("Hello, world!"); - assertThat(logEntry.getRealm()).isNull(); assertThat(logEntry.getArgs().size()).isEqualTo(1); assertThat(logEntry.getArgs().get(0).getType()).isEqualTo("string"); assertThat(logEntry.getType()).isEqualTo("console"); @@ -86,7 +88,6 @@ void canFilterConsoleLogs() throws ExecutionException, InterruptedException, Tim ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); assertThat(logEntry.getText()).isEqualTo("Hello, world!"); - assertThat(logEntry.getRealm()).isNull(); assertThat(logEntry.getArgs().size()).isEqualTo(1); assertThat(logEntry.getType()).isEqualTo("console"); assertThat(logEntry.getLevel()).isEqualTo(LogLevel.INFO); @@ -100,7 +101,6 @@ void canFilterConsoleLogs() throws ExecutionException, InterruptedException, Tim ConsoleLogEntry errorLogEntry = errorLogfuture.get(5, TimeUnit.SECONDS); assertThat(errorLogEntry.getText()).isEqualTo("I am console error"); - assertThat(errorLogEntry.getRealm()).isNull(); assertThat(errorLogEntry.getArgs().size()).isEqualTo(1); assertThat(errorLogEntry.getType()).isEqualTo("console"); assertThat(errorLogEntry.getLevel()).isEqualTo(LogLevel.ERROR); @@ -123,6 +123,10 @@ void canListenToJavascriptLog() JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS); + Source source = logEntry.getSource(); + assertThat(source.getBrowsingContext().isPresent()).isTrue(); + assertThat(source.getRealm()).isNotNull(); + assertThat(logEntry.getText()).isEqualTo("Error: Not working"); assertThat(logEntry.getType()).isEqualTo("javascript"); assertThat(logEntry.getLevel()).isEqualTo(LogLevel.ERROR); @@ -218,7 +222,6 @@ void canFilterLogs() throws ExecutionException, InterruptedException { ConsoleLogEntry consoleLogEntry = logEntry.getConsoleLogEntry().get(); assertThat(consoleLogEntry.getText()).isEqualTo("Hello, world!"); - assertThat(consoleLogEntry.getRealm()).isNull(); assertThat(consoleLogEntry.getArgs().size()).isEqualTo(1); assertThat(consoleLogEntry.getType()).isEqualTo("console"); assertThat(consoleLogEntry.getLevel()).isEqualTo(LogLevel.INFO); @@ -243,7 +246,6 @@ void canListenToConsoleLogForABrowsingContext() ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); assertThat(logEntry.getText()).isEqualTo("Hello, world!"); - assertThat(logEntry.getRealm()).isNull(); assertThat(logEntry.getArgs().size()).isEqualTo(1); assertThat(logEntry.getType()).isEqualTo("console"); assertThat(logEntry.getLevel()).isEqualTo(LogLevel.INFO); diff --git a/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java b/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java index 68c6486f64a71..aee7a084a388b 100644 --- a/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java +++ b/java/test/org/openqa/selenium/grid/router/RemoteWebDriverBiDiTest.java @@ -42,6 +42,7 @@ import org.openqa.selenium.bidi.log.ConsoleLogEntry; import org.openqa.selenium.bidi.log.LogLevel; import org.openqa.selenium.bidi.module.LogInspector; +import org.openqa.selenium.bidi.script.Source; import org.openqa.selenium.environment.webserver.AppServer; import org.openqa.selenium.environment.webserver.NettyAppServer; import org.openqa.selenium.grid.config.TomlConfig; @@ -108,8 +109,10 @@ void canListenToLogs() throws ExecutionException, InterruptedException, TimeoutE ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); + Source source = logEntry.getSource(); + assertThat(source.getBrowsingContext().isPresent()).isTrue(); + assertThat(source.getRealm()).isNotNull(); assertThat(logEntry.getText()).isEqualTo("Hello, world!"); - assertThat(logEntry.getRealm()).isNull(); assertThat(logEntry.getArgs().size()).isEqualTo(1); assertThat(logEntry.getType()).isEqualTo("console"); assertThat(logEntry.getLevel()).isEqualTo(LogLevel.INFO); diff --git a/javascript/node/selenium-webdriver/bidi/logEntries.js b/javascript/node/selenium-webdriver/bidi/logEntries.js index ed78d285a9f44..cba08adb77424 100644 --- a/javascript/node/selenium-webdriver/bidi/logEntries.js +++ b/javascript/node/selenium-webdriver/bidi/logEntries.js @@ -17,6 +17,8 @@ 'use strict' +const { Source } = require('./scriptTypes') + /** * Represents a base log entry. * Desribed in https://w3c.github.io/webdriver-bidi/#types-log-logentry. @@ -25,12 +27,14 @@ class BaseLogEntry { /** * Creates a new instance of BaseLogEntry. * @param {string} level - The log level. + * @param {string} text - The log source. * @param {string} text - The log text. * @param {number} timeStamp - The log timestamp. * @param {string} stackTrace - The log stack trace. */ - constructor(level, text, timeStamp, stackTrace) { + constructor(level, source, text, timeStamp, stackTrace) { this._level = level + this._source = new Source(source) this._text = text this._timeStamp = timeStamp this._stackTrace = stackTrace @@ -67,6 +71,10 @@ class BaseLogEntry { get stackTrace() { return this._stackTrace } + + get source() { + return this._source + } } /** @@ -83,8 +91,8 @@ class GenericLogEntry extends BaseLogEntry { * @param {string} type - The log type. * @param {string} stackTrace - The log stack trace. */ - constructor(level, text, timeStamp, type, stackTrace) { - super(level, text, timeStamp, stackTrace) + constructor(level, source, text, timeStamp, type, stackTrace) { + super(level, source, text, timeStamp, stackTrace) this._type = type } @@ -103,10 +111,9 @@ class GenericLogEntry extends BaseLogEntry { * @extends GenericLogEntry */ class ConsoleLogEntry extends GenericLogEntry { - constructor(level, text, timeStamp, type, method, realm, args, stackTrace) { - super(level, text, timeStamp, type, stackTrace) + constructor(level, source, text, timeStamp, type, method, args, stackTrace) { + super(level, source, text, timeStamp, type, stackTrace) this._method = method - this._realm = realm this._args = args } @@ -117,15 +124,6 @@ class ConsoleLogEntry extends GenericLogEntry { get method() { return this._method } - - /** - * Gets the realm associated with the log entry. - * @returns {string} The realm associated with the log entry. - */ - get realm() { - return this._realm - } - /** * Gets the arguments associated with the log entry. * @returns {Array} The arguments associated with the log entry. @@ -141,8 +139,8 @@ class ConsoleLogEntry extends GenericLogEntry { * @extends GenericLogEntry */ class JavascriptLogEntry extends GenericLogEntry { - constructor(level, text, timeStamp, type, stackTrace) { - super(level, text, timeStamp, type, stackTrace) + constructor(level, source, text, timeStamp, type, stackTrace) { + super(level, source, text, timeStamp, type, stackTrace) } } diff --git a/javascript/node/selenium-webdriver/bidi/logInspector.js b/javascript/node/selenium-webdriver/bidi/logInspector.js index e1861f5c2f2ed..9eb51f5015bcf 100644 --- a/javascript/node/selenium-webdriver/bidi/logInspector.js +++ b/javascript/node/selenium-webdriver/bidi/logInspector.js @@ -128,11 +128,11 @@ class LogInspector { if (params?.type === LOG.TYPE_CONSOLE) { let consoleEntry = new ConsoleLogEntry( params.level, + params.source, params.text, params.timestamp, params.type, params.method, - params.realm, params.args, params.stackTrace, ) @@ -178,6 +178,7 @@ class LogInspector { if (params?.type === LOG.TYPE_JS_LOGS) { let jsEntry = new JavascriptLogEntry( params.level, + params.source, params.text, params.timestamp, params.type, @@ -212,6 +213,7 @@ class LogInspector { if (params?.type === 'javascript' && params?.level === 'error') { let jsErrorEntry = new JavascriptLogEntry( params.level, + params.source, params.text, params.timestamp, params.type, @@ -250,6 +252,7 @@ class LogInspector { if (params?.type === 'javascript') { let jsEntry = new JavascriptLogEntry( params.level, + params.source, params.text, params.timestamp, params.type, @@ -279,11 +282,11 @@ class LogInspector { if (params?.type === 'console') { let consoleEntry = new ConsoleLogEntry( params.level, + params.source, params.text, params.timestamp, params.type, params.method, - params.realm, params.args, params.stackTrace, ) @@ -302,6 +305,7 @@ class LogInspector { if (params !== undefined && !['console', 'javascript'].includes(params?.type)) { let genericEntry = new GenericLogEntry( params.level, + params.source, params.text, params.timestamp, params.type, diff --git a/javascript/node/selenium-webdriver/test/bidi/log_inspector_test.js b/javascript/node/selenium-webdriver/test/bidi/log_inspector_test.js index c23c9d8b46629..c8967a4f333fc 100644 --- a/javascript/node/selenium-webdriver/test/bidi/log_inspector_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/log_inspector_test.js @@ -44,7 +44,8 @@ suite( const inspector = await logInspector(driver) await inspector.onConsoleEntry(function (log) { assert.equal(log.text, 'Hello, world!') - assert.equal(log.realm, null) + assert.notEqual(log.source.realmId, null) + assert.notEqual(log.source.browsingContextId, null) assert.equal(log.type, 'console') assert.equal(log.level, 'info') assert.equal(log.method, 'log') @@ -63,7 +64,8 @@ suite( await inspector.onConsoleEntry(function (log) { logEntry = log assert.equal(logEntry.text, 'Hello, world!') - assert.equal(logEntry.realm, null) + assert.notEqual(log.source.realmId, null) + assert.notEqual(log.source.browsingContextId, null) assert.equal(logEntry.type, 'console') assert.equal(logEntry.level, 'info') assert.equal(logEntry.method, 'log') @@ -88,7 +90,8 @@ suite( await inspector.onConsoleEntry(function (log) { logEntry = log assert.equal(logEntry.text, 'Hello, world!') - assert.equal(logEntry.realm, null) + assert.notEqual(log.source.realmId, null) + assert.notEqual(log.source.browsingContextId, null) assert.equal(logEntry.type, 'console') assert.equal(logEntry.level, 'info') assert.equal(logEntry.method, 'log') @@ -235,6 +238,8 @@ suite( await inspector.onJavascriptException(function (log) { logEntry = log assert.equal(logEntry.text, 'Error: Not working') + assert.notEqual(log.source.realmId, null) + assert.notEqual(log.source.browsingContextId, null) assert.equal(logEntry.type, 'javascript') assert.equal(logEntry.level, 'error') }) @@ -267,7 +272,8 @@ suite( await inspector.onLog(function (log) { logEntry = log assert.equal(logEntry.text, 'Hello, world!') - assert.equal(logEntry.realm, null) + assert.notEqual(log.source.realmId, null) + assert.notEqual(log.source.browsingContextId, null) assert.equal(logEntry.type, 'console') assert.equal(logEntry.level, 'info') assert.equal(logEntry.method, 'log') @@ -286,7 +292,8 @@ suite( await inspector.onLog(function (log) { logEntry = log assert.equal(logEntry.text, 'Hello, world!') - assert.equal(logEntry.realm, null) + assert.notEqual(log.source.realmId, null) + assert.notEqual(log.source.browsingContextId, null) assert.equal(logEntry.type, 'console') assert.equal(logEntry.level, 'info') assert.equal(logEntry.method, 'log')