How to programmatically dismiss an alert in WebExtensions?

Hi,

I’m porting a Chrome/FirefoxLegacy add-on to WebExtensions.
The add-on is used to do automation test. It needs to be able to dismiss an alert/confirm/prompt.

In Chrome, this is done using chrome.debugger API, by sending “Page.handleJavaScriptDialog” command.
In Firefox JSCTypes add-on, this is done using view sdk.

Now in WebExtensions, chrome.debugger API is not implemented yet, and we can’t use JSCTypes sdk in it, too.
Is it possible to do this in WebExtensions?

Thanks!

what is the “dismiss an alert/confirm/prompt”?

alert=()=>{}; is acceptable?

or, use https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging to native app.

By “dismiss an alert/confirm/prompt”, I mean for example, when an alert pops out by user operation, I want to make it disappear by executing some code in extension.

As a test program, I prefer not to change web page behavior. So rewrite alert() function may be the last option.
For native messaging, can this be done in a native app? How?

Thanks for advise!

You can send a message to an native program that simulate mouse click or send window message.

If your purpose is simply test, rewriting functions may be a simple solution, for example:
var ok;alert=(str)=>{if(str==‘good’){ok=1}};

There are some disadvantages of simulate mouse click, for example:

  • The alert seems not native Windows dialog, I can’t find it using Spy++. So it is hard to know where to click, and I can’t SendMessage to it either.
  • When there are multiple Firefox window, it can be hard to find the correct one.

Is there a more reliable way to do this?

So, what alert you want to dismiss, why not use console, variables or other things to testing?

I’m developing an extension for web automation test. So I can’t decide what to use in web pages. The web pages are not developed by me.

Okay, I understand it now.

I found this https://bugzilla.mozilla.org/show_bug.cgi?id=1351938 by you, it is wontfix.

So, you have to consider using rewriting functions, native message to native app, or another solution (such as a test framework instead of a WebExtensions, or await the chrome.debugger in Bug 1316741) to implement it, and I don‘t think it’s a necessary APIs.

I can’t find a better way now. Maybe wait for chrome.debugger or new features in WebExtensions which allow me to do this.

Thank you for the help! :slightly_smiling_face: