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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: NoAlertPresentException with Selenium Java 4.24.0 and Chrome under enableBiDi #14468

Closed
alaahong opened this issue Sep 2, 2024 · 3 comments

Comments

@alaahong
Copy link
Member

alaahong commented Sep 2, 2024

What happened?

If disable BiDi, both Chrome and Firefox are fine to trigger alert.
If enableBiDi, Selenium can't trigger the Alert in Chrome with Java 4.24.0 , but work with Firefox.
more detail can be refer to below code.

How can we reproduce the issue?

package cn.ianzhang;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class AlertTest {
  @Test
  void noBidiChromeTest() {
    ChromeOptions options = new ChromeOptions();
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertEquals(driver.switchTo().alert().getText(), "I am an alert");
    } finally {
      if (driver != null) driver.quit();
    }
  }


  @Test
  void bidiTest() {
    ChromeOptions options = new ChromeOptions();
    options.enableBiDi();
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertThrows(NoAlertPresentException.class, () -> driver.switchTo().alert());
    } finally {
      if (driver != null) driver.quit();
    }
  }

  @Test
  void noBidiFirefoxTest() {
    FirefoxOptions options = new FirefoxOptions();
    WebDriver driver = new FirefoxDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertEquals(driver.switchTo().alert().getText(), "I am an alert");
    } finally {
      if (driver != null) driver.quit();
    }
  }

  @Test
  void bidiFirefoxTest() {
    FirefoxOptions options = new FirefoxOptions();
    options.enableBiDi();
    WebDriver driver = new FirefoxDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertEquals(driver.switchTo().alert().getText(), "I am an alert");
    } finally {
      if (driver != null) driver.quit();
    }
  }
}

Relevant log output

N/A

Operating System

Windows 11

Selenium version

Java 4.24.0

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

Chrome 128.0.6613.114

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

ChromeDriver 128.0.6613.86

Are you using Selenium Grid?

N/A

Copy link

github-actions bot commented Sep 2, 2024

@alaahong, 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!

@alaahong
Copy link
Member Author

alaahong commented Sep 2, 2024

Just inspired by serenity-bdd/serenity-core#2470 (comment)
Seems the exception is from UnhandledPromptBehaviour, only INGORE will no exception for the alert test, and meanwhile ,the default behavior is DISMISS_AND_NOTIFY.

As this is the BiDi relatives, @pujagani , is the issue from Selenium or Chrome Driver? It's coming since enabledBiDi.


  @Test
  void bidiIgnoreTest() {
    ChromeOptions options = new ChromeOptions();
    options.enableBiDi();
    options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertEquals(driver.switchTo().alert().getText(), "I am an alert");
    } finally {
      if (driver != null) driver.quit();
    }
  }

  @Test
  void bidiAcceptTest() {
    ChromeOptions options = new ChromeOptions();
    options.enableBiDi();
    options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertThrows(NoAlertPresentException.class, () -> driver.switchTo().alert());
    } finally {
      if (driver != null) driver.quit();
    }
  }

  @Test
  void bidiDismissTest() {
    ChromeOptions options = new ChromeOptions();
    options.enableBiDi();
    options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS);
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertThrows(NoAlertPresentException.class, () -> driver.switchTo().alert());
    } finally {
      if (driver != null) driver.quit();
    }
  }

  @Test
  void bidiDismissAndNotifyTest() {
    ChromeOptions options = new ChromeOptions();
    options.enableBiDi();
    options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertThrows(NoAlertPresentException.class, () -> driver.switchTo().alert());
    } finally {
      if (driver != null) driver.quit();
    }
  }

  @Test
  void bidiAcceptAndNotifyTest() {
    ChromeOptions options = new ChromeOptions();
    options.enableBiDi();
    options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT_AND_NOTIFY);
    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://www.ianzhang.cn/s4ia/03-webdriver-advanced/interaction.html");
      driver.findElement(By.id("alert-btn")).click();
      Assertions.assertThrows(NoAlertPresentException.class, () -> driver.switchTo().alert());
    } finally {
      if (driver != null) driver.quit();
    }
  }

@pujagani
Copy link
Contributor

pujagani commented Sep 4, 2024

It is a known breaking change in Chrome. If BiDi is enabled then alert handling is done via BiDi. Refer to GoogleChromeLabs/chromium-bidi#2351. Setting the "unhandledPromptBehavior" to "ignore" will help solve it. We added this line to the Selenium test setup https://github.com/SeleniumHQ/selenium/blob/trunk/java/test/org/openqa/selenium/testing/drivers/Browser.java#L70.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants