Skip to content

[🐛 Bug]: DevTools.createSession() picks a wrong window #10810

@asolntsev

Description

@asolntsev

What happened?

I have a browser with 2+ open windows (tabs).
When I execute driver.getDevTools().createSession() it connects to a wrong tab.
See the code sample below.

The problematic code is https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/devtools/DevTools.java#L99:

    // Grab the first "page" type, and glom on to that.
    // TODO: Find out which one might be the current one
    TargetID targetId = infos.stream()
      .filter(info -> "page".equals(info.getType()))
      .map(TargetInfo::getTargetId)
      .findAny()
      .orElseThrow(() -> new DevToolsException("Unable to find target id of a page"));

Actually, we can easily pick the right TargetID by matching it with WebDriver.getWindowHandle().

How can we reproduce the issue?

@Test
  void useDevToolsInWrongWindow() {
    WebDriverManager.chromedriver().setup();
    ChromeDriver driver = new ChromeDriver();
    driver.navigate().to("https://duckduckgo.com");
    driver.executeScript("window.open()");

    DevTools devTools = driver.getDevTools();
    devTools.createSessionIfThereIsNotOne();
    devTools.send(Emulation.setAutoDarkModeOverride(Optional.of(true)));

    // Expected: the first window (duckduckgo.com) becomes dark
    // Actual: the second window (empty) becomes dark
    driver.quit();
  }

The fix

Here is my pull request: #10811

Operating System

MacOS (no matter)

Selenium version

4.3.0 Java

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

Chrome 102 (no matter)

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

ChromeDriver 102 (no matter)

Are you using Selenium Grid?

no matter

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-javaJava BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions