Implement all the Session Commands related to JS Alert, Prompt and Confirm #20

Open
detro opened this Issue May 16, 2012 · 40 comments

Comments

Projects
None yet
Owner

detro commented May 16, 2012

/session/:sessionId/alert_text
/session/:sessionId/alert_text
/session/:sessionId/accept_alert
/session/:sessionId/dismiss_alert

Contributor

lukeis commented Oct 12, 2012

So, I've started thinking about this... and have a rough implementation that basically just captures the alert message, but auto dismisses the alert.

The WebDriver API has a gap here... I've run into the same thing with iPhoneDriver. It assumes the underlying driver can actually capture the alert/confirm/prompt and hold it until a wire command comes across the wire to take action. This is simply not possible in iOS (I have yet to have a solution there) and doesn't appear possible for phantomjs either... unless phantomjs itself was reworked in some way to accommodate this.

Anyways, I think I have the rough one ready where it will handle alerts, but not allow you to choose how it handles them. Let me know if you think this is good enough for 1.0 and I can send a pull request.

Owner

detro commented Oct 12, 2012

No, I think it's doable to work on PhantomJS to make it behave "as we need".
But this will be done after I deal with all the current issues (major ones are related to mouse interaction) and have a first version released in Selenium and integrated 1.0 in phantomjs.

After that I'll come back to this and work on PhantomJS to enable the option to "keep the alert open".

nemoo commented Feb 15, 2013

is there a workaround that i can use right now to handle confirmation dialogs before this is implemented?

Contributor

lukeis commented Feb 15, 2013

inject javascript to handle the alert / confirms yourself (although you'll need to do this after every page refresh before the alert shows up and you won't be able to handle alerts that are onload)
driver.execute_script("window.alert = function(){}");
or
driver.execute_script("window.confirm = function(){return true;}");

nemoo commented Feb 15, 2013

that helped. thanks a lot!!

I use @lukeis's workaround to retrieve the last alert message. To inject the alert handler, I execute this script:

(function () {
    var lastAlert = undefined;
    window.alert = function (message) {
        lastAlert = message;
    };
    window.getLastAlert = function () {
        var result = lastAlert;
        lastAlert = undefined;
        return result;
    };
}());

and then, to retrieve and reset lastAlert, I execute this script:

return window.getLastAlert && window.getLastAlert();

The only problem with this workaround is that it's necessary to call getLastAlert after every WebDriver command in order to know which command raised the alert. It would be nice if there was a way, using only injected JavaScript, to cause WebDriver to return a500 Internal Server Error whenever lastAlert isn't undefined.

Is alert handling still planned to be added in GhostDriver?
Will this be possible in the future:
Alert alert = driver.switchTo().alert()
alert.accept()

khreez commented Apr 24, 2013

I started to work on a solution for this issue following @detro comments, but my main limitation is with C++ and how to keep open the alert on the PhantomJS side, could any one provide me any hint I can follow.
Thanks in advance!

Jenissi commented May 2, 2013

I was able to capture the alert text using the windows.alert and window.getLastAlert JS functions but how do I actually confirm/cancel the alert?

Jenissi commented May 2, 2013

nvm -- I think I figured out what I was doing wrong, I was executing the window.confirm after the click that generated the alert as supposed to before. Don't completely understand how it's working but it did confirm the alert. Thanks.

There is no need to dismiss the alert because there is no actual alert.
What happens is that whoever tries to call alert() ends up calling our injected function instead of popping up a modal alert.

ariya referenced this issue in ariya/phantomjs May 20, 2013

Closed

Ghostdriver missing alert commands #11327

Any news on this?
I ran into that accept_alert dismisses an alert/prompt today :(

// cc @Jenissi @khreez

Owner

detro commented Jul 18, 2013

Nope.
But you are welcome to contribute to this issue with a patch.

On 18 July 2013 13:24, Sebastian Golasch notifications@github.com wrote:

Any news on this?
I ran into that accept_alert dismisses an alert/prompt today :(

// cc @Jenissi https://github.com/Jenissi @khreezhttps://github.com/khreez


Reply to this email directly or view it on GitHubhttps://github.com/detro/ghostdriver/issues/20#issuecomment-21179563
.

Ivan De Marino
Coder, Technologist, Cook, Italian

blog.ivandemarino.me | www.linkedin.com/in/ivandemarino |
twitter.com/detronizator

emassip commented Aug 4, 2013

@detro You need to handle some "a system modal dialog box is open" status handle to throw UnexpectedAlertOpen and handle the 4 WebDriver requests but is it possible to implement fully this kind of thing without updating PhantomJS code?

I mean, if you simply set a var then any "background" javascript using setTimeout() will continue to run and you don't want that since it's not what really happen with a true browser, and if you set e.g. an "onAlert" blocking handler in GD then you'll have PhantomJS blocked waiting (great ?) but GD part will be also, except if there's a way to manage threads in GD?

Please, what were you thinking at exactly earlier when you said "work on PhantomJS to enable the option to "keep the alert open"?

Thank you

Edit: FWIW, I think that PhantomJS should handle some very basic modal window management: on alert(), confirm() and prompt(), it could just block and render a white page with nothing else than the alert message and options?

ajaxsys commented Aug 30, 2013

It seams that it doesn't support with alert(), confirm() and prompt() yet.
It throws UnsupportedCommandException when trying to accept/dismiss.

Exception:
org.openqa.selenium.UnsupportedCommandException: Invalid Command Method - Request => {"headers":{...},"httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":{...},"urlOriginal":"/session/13ddee20-110f-11e3-af52-a75f21929065/alert_text"}

Env:
(selenium-java.version 2.33.0 + PhantomJS 1.9.1 + GhostDriver1.0.4)

This issue is still present in PhantomJS 1.9.2, the switchTo alert command + accept does not work.

mbp commented Oct 25, 2013

+1 for this issue.

cmiles74 referenced this issue in semperos/clj-webdriver Oct 28, 2013

Merged

Support for PhantomJS #112

exstan commented Nov 15, 2013

+1

detro was assigned Jan 1, 2014

abotalov referenced this issue in teamcapybara/capybara Mar 9, 2014

Closed

Add API for operation with modal (simple) dialogs #1037

ghguy commented Apr 23, 2014

+1

pavelpp commented May 5, 2014

+1

acorse commented May 19, 2014

+1 on this one too

+1 still not possible to get alert confirmation in 1.9.7

There are two ways to do this, and they work really well:

1- Using JavascriptExecutor

((JavascriptExecutor) driver).executeScript("window.alert = function(msg){};");
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg){return true;};");

2- Using PhantomJS callbacks (you need the 1.1.0 version of GhostDriver to do this)

PhantomJSDriver phantom = (PhantomJSDriver) driver;

phantom.executePhantomJS("var page = this;" +
             "page.onAlert = function(msg) {" +
                    "console.log('ALERT: ' + msg);" +
             "};");
phantom.executePhantomJS("var page = this;" +
             "page.onConfirm = function(msg) {" +
                    "console.log('CONFIRM: ' + msg);"
                    "return true;" +
             "};");

Thanks Stefan, it works well on my side. I preferred the first solution as I needed to handle several browsers (Chrome, PhantomJS...).

harobed commented Aug 8, 2014

+1

So do we know if we can resolve the onLoad() JavaScript Alerts? For example, if a page is using a SAML page? The above solutions do not work with alerts triggered from an onLoad() which I've read is a known issue. However, can this ever be resolved?

juzwani commented Oct 1, 2014

I just need the text of the Alert for my Java application. How can i get the Alert text in Java?
Does anybody know this or know when this is issue will be fixed?

jerckov commented Feb 5, 2015

still not possible to get alert confirmation in 1.9.8. also Stefans solution doesn't work for me. I have no log on this place

"console.log('CONFIRM: ' + msg);"

@Jenissi thanks a lot your comment solve my issue too..
i am using selenium phantomjs latest version for java, seriously i was struggling with it from last 5 hours now get it worked using your comment, so thanks a lot again man.

i was doing :
driver.findElement(By.id("clickme")).click();
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg){return true;};");

and finally get it worked using your comment:
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg){return true;};");
driver.findElement(By.id("clickme")).click();

leftty commented Nov 26, 2015

Can someone tell me how can I differentiate in my test the values for which I get an alert when inserting them in an input and pressing a button and the values that trigger the opening of a modal with some info I want to retrieve, if I use the presented solution of avoiding the alerts altogether?

+1 (really?! why no 'accept_alert')

nysyzp commented Sep 21, 2016

+1 Any news?

alexellis commented Jan 26, 2017 edited

@detro is this still on your roadmap since it's been open > 4 years now with no sign of progress? cc/ @jesg

Collaborator

jesg commented Jan 27, 2017

@alexellis not at the moment. there are no plans that i know of to fix this in phantomjs 2.5.

webdriver assumes that user prompts are blocking while phantomjs assumes that they are async.

https://www.w3.org/TR/webdriver/#user-prompts

Collaborator

jesg commented Feb 15, 2017 edited

w3c added a user prompt handler. you would set the unhandledPromptBehavior capability to dismiss or accept.

this wouldn't be very hard to implement.

Collaborator

jesg commented Feb 21, 2017

i've implemented unhandledPromptBehavior capability in commit jesg/ghostdriver@38c39ca

padde referenced this issue in keathley/wallaby Mar 25, 2017

Closed

Handling JS alert/confirm/prompt #169

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