Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
asolntsev opened this issue Jun 25, 2022 · 3 comments
Closed

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

asolntsev opened this issue Jun 25, 2022 · 3 comments

Comments

@asolntsev
Copy link
Contributor

asolntsev commented Jun 25, 2022

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

@github-actions
Copy link

@asolntsev, 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 added a commit that referenced this issue Jun 29, 2022
* #10810 pick the right target using given window handle

* #10810 leave parameterless methods createSession() and createSessionIfThereIsNotOne() for backward compatibility

* #10810 log the exception properly

* #10810 split code to smaller methods

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
@diemol
Copy link
Member

diemol commented Jun 29, 2022

PR has been merged, thank you, @asolntsev!

@diemol diemol closed this as completed Jun 29, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants