SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.78k stars 8.02k forks source link

[🐛 Bug]: Can't capture full Object structures in console log #14043

Closed zerosdev closed 3 weeks ago

zerosdev commented 1 month ago

What happened?

I'm trying to get the full log messages in console (Chrome) but the log captured is only "Object" when in real browser we can expand object structures. Is there any possibility to capture the full object values?

How can we reproduce the issue?

  1. Add logger to the webdriver instance to print log message in Terminal

    const logs: logging.Entry[] = await driver.manage().logs().get(logging.Type.BROWSER);
    console.log(logs);
  2. Create a webpage than print an object in the browser's console

    <script>console.warn(JSON.parse(`{"key":"value"}`));</script>
  3. Navigate to that webpage

  4. Check terminal messages

Relevant log output

[WARNING] [2024-05-27T08:20:34.139Z] http://localhost:8000/console.html 6:16 Object

Operating System

MacOS X

Selenium version

Javascript 4.7.1

What are the browser(s) and version(s) where you see this issue?

Chrome 125

What are the browser driver(s) and version(s) where you see this issue?

Chromedriver 125

Are you using Selenium Grid?

No

github-actions[bot] commented 1 month ago

@zerosdev, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

diemol commented 1 month ago

Those APIs will be using BiDi in the future, you can see an examples here: https://www.selenium.dev/documentation/webdriver/bidirectional/webdriver_bidi/log/

You also need to upgrade to the latest Selenium version to use them.

zerosdev commented 1 month ago

Those APIs will be using BiDi in the future, you can see an examples here: https://www.selenium.dev/documentation/webdriver/bidirectional/webdriver_bidi/log/

You also need to upgrade to the latest Selenium version to use them.

Thanks, i'm trying to implement BiDi in the latest version of Selenium but i got this error :

TypeError: Cannot read properties of undefined (reading 'replace') at thenableWebDriverProxy.getBidi (node_modules/selenium-webdriver/lib/webdriver.js:1261:34) at LogInspector.init (node_modules/selenium-webdriver/bidi/logInspector.js:41:17) at Object.getLogInspectorInstance [as LogInspector] (node_modules/selenium-webdriver/bidi/logInspector.js:259:3)

Here is the piece of code where the error produced:

async getBidi() {
    const caps = await this.getCapabilities()
    let WebSocketUrl = caps['map_'].get('webSocketUrl')
    return new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1'))
  }
diemol commented 1 month ago

Can you share your code?

zerosdev commented 1 month ago

Here are the main functions:

public constructor(browserConfig?: BrowserConfig) {
  // default values
  this.browserConfig = {
    downloadPath: environment.DOWNLOADS_PATH ?? `../builds/qa/automation/downloads`,
    headless: environment.headless ?? false,
    incognito: environment.incognito ?? true,
    ...browserConfig,
  };

  this.driver = Browser.createBrowser(this.browserConfig);
  // add ability to upload from local machine to remote webdriver
  this.driver.setFileDetector(new FileDetector());
}

public async enableBidiLogging(): Promise<void> {
  const inspector = await LogInspector(this.driver);
  await inspector.onConsoleEntry((log: ConsoleLogEntry) => this.consoleLogs.push(log));
  await inspector.onJavascriptLog((log: JavascriptLogEntry) => this.javascriptLogs.push(log));
  await inspector.onJavascriptException((log: JavascriptLogEntry) => this.javascriptLogs.push(log));
}

private static createBrowser(browserConfig: BrowserConfig): ThenableWebDriver {
    const options: Options = new Options();
        options.addArguments(
          '--no-sandbox',
          `--window-size=${environment.screen}`,
          '--disable-dev-shm-usage',
          '--disable-web-security',
          '--allow-running-insecure-content',
          '--disable-cache',
          'disable-popup-blocking',
          // `--proxy-server=${environment.PROXY_SERVER}`,
        );
        if (browserConfig.incognito) {
          options.addArguments('--incognito');
        }
        if (browserConfig.headless) {
          options.addArguments('--headless');
        }
        const prefs: any = {
          'directory_upgrade': true,
          'disable-popup-blocking': true,
          'download.directory_upgrade': true,
          'download.prompt_for_download': false,
          'plugins.always_open_pdf_externally': true, // force download pdf file
          'profile.content_settings.exceptions.automatic_downloads.*.setting': 1,
          'profile.default_content_settings.popups': 0,
          'safebrowsing.enabled': false,
        };
        options.setUserPreferences(prefs);

        return new Builder().forBrowser('chrome').setChromeOptions(options).build();
}

So, i call enableBidiLogging() before opening an url and that error occurs

diemol commented 1 month ago

Did you check the code examples I linked above?

Check how the browser is started: https://github.com/SeleniumHQ/seleniumhq.github.io/blob/trunk//examples/javascript/test/bidirectional/logInspector.spec.js#L10-L15

pujagani commented 1 month ago

+1 to Diego's comment. You need to enableBiDi() for the code to work.

github-actions[bot] commented 3 weeks ago

This issue was closed because we did not receive any additional information after 14 days.