Skip to content

Interacting with Alerts

StageZero follows a similar approach to interacting with alerts to the likes of Playwright and Puppeteer. You can subscribe to the OnAlert event listener available in the IDriverWeb object. This allows you to have clear, concise alert handling and reduces the requirement for having to wait for an alert to appear in the browser.

var driver = DriverBuilder.Create(new WebDriverOptions());
driver.OnAlert += async (_, alert) =>
{
// Handle any alert based actions here.
};

⚠️ Precautions

You must ensure that you handle the alert directly in the OnAlert event handler. If you do not call either Dismiss or Confirm your tests will hang.

driver.OnAlert += async (_, alert) =>
{
await alert.Confirm();
};
driver.OnAlert += async (_, alert) =>
{
await alert.Dismiss();
};
driver.OnAlert += async (_, alert) =>
{
Console.WriteLine(alert.Message);
await alert.Dismiss();
};