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

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

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

Comments

Projects
None yet
@detro
Owner

detro commented May 16, 2012

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

@lukeis

This comment has been minimized.

Show comment
Hide comment
@lukeis

lukeis Oct 12, 2012

Contributor

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.

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.

@detro

This comment has been minimized.

Show comment
Hide comment
@detro

detro Oct 12, 2012

Owner

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".

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

This comment has been minimized.

Show comment
Hide comment
@nemoo

nemoo Feb 15, 2013

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

nemoo commented Feb 15, 2013

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

@lukeis

This comment has been minimized.

Show comment
Hide comment
@lukeis

lukeis Feb 15, 2013

Contributor

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;}");

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

This comment has been minimized.

Show comment
Hide comment
@nemoo

nemoo Feb 15, 2013

that helped. thanks a lot!!

nemoo commented Feb 15, 2013

that helped. thanks a lot!!

@ron-inbar

This comment has been minimized.

Show comment
Hide comment
@ron-inbar

ron-inbar Mar 29, 2013

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.

ron-inbar commented Mar 29, 2013

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.

@trillianC

This comment has been minimized.

Show comment
Hide comment
@trillianC

trillianC Apr 17, 2013

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

trillianC commented Apr 17, 2013

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

This comment has been minimized.

Show comment
Hide comment
@khreez

khreez 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!

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

This comment has been minimized.

Show comment
Hide comment
@Jenissi

Jenissi 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

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

This comment has been minimized.

Show comment
Hide comment
@Jenissi

Jenissi 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.

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.

@ron-inbar

This comment has been minimized.

Show comment
Hide comment
@ron-inbar

ron-inbar May 2, 2013

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.

ron-inbar commented May 2, 2013

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.

@asciidisco

This comment has been minimized.

Show comment
Hide comment
@asciidisco

asciidisco Jul 18, 2013

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

// cc @Jenissi @khreez

asciidisco commented Jul 18, 2013

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

// cc @Jenissi @khreez

@detro

This comment has been minimized.

Show comment
Hide comment
@detro

detro Jul 18, 2013

Owner

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//issues/20#issuecomment-21179563
.

Ivan De Marino
Coder, Technologist, Cook, Italian

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

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//issues/20#issuecomment-21179563
.

Ivan De Marino
Coder, Technologist, Cook, Italian

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

@emassip

This comment has been minimized.

Show comment
Hide comment
@emassip

emassip 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?

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

This comment has been minimized.

Show comment
Hide comment
@ajaxsys

ajaxsys 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)

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)

@bettiolo

This comment has been minimized.

Show comment
Hide comment
@bettiolo

bettiolo Oct 14, 2013

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

bettiolo commented Oct 14, 2013

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

@mbp

This comment has been minimized.

Show comment
Hide comment
@mbp

mbp Oct 25, 2013

+1 for this issue.

mbp commented Oct 25, 2013

+1 for this issue.

@exstan

This comment has been minimized.

Show comment
Hide comment
@exstan

exstan commented Nov 15, 2013

+1

@ghguy

This comment has been minimized.

Show comment
Hide comment
@ghguy

ghguy commented Apr 23, 2014

+1

@pavelpp

This comment has been minimized.

Show comment
Hide comment
@pavelpp

pavelpp commented May 5, 2014

+1

@acorse

This comment has been minimized.

Show comment
Hide comment
@acorse

acorse May 19, 2014

+1 on this one too

acorse commented May 19, 2014

+1 on this one too

@ptrouillard

This comment has been minimized.

Show comment
Hide comment
@ptrouillard

ptrouillard May 23, 2014

+1 still not possible to get alert confirmation in 1.9.7

ptrouillard commented May 23, 2014

+1 still not possible to get alert confirmation in 1.9.7

@stefanteixeira

This comment has been minimized.

Show comment
Hide comment
@stefanteixeira

stefanteixeira May 23, 2014

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;" +
             "};");

stefanteixeira commented May 23, 2014

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;" +
             "};");
@ptrouillard

This comment has been minimized.

Show comment
Hide comment
@ptrouillard

ptrouillard May 26, 2014

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

ptrouillard commented May 26, 2014

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

@detro detro added the enhancement label May 27, 2014

@detro detro added the help wanted label May 27, 2014

@harobed

This comment has been minimized.

Show comment
Hide comment
@harobed

harobed commented Aug 8, 2014

+1

@ant-howell

This comment has been minimized.

Show comment
Hide comment
@ant-howell

ant-howell Aug 29, 2014

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?

ant-howell commented Aug 29, 2014

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

This comment has been minimized.

Show comment
Hide comment
@juzwani

juzwani 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?

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

This comment has been minimized.

Show comment
Hide comment
@jerckov

jerckov 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);"

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);"

@myselfsufiyan

This comment has been minimized.

Show comment
Hide comment
@myselfsufiyan

myselfsufiyan Jul 6, 2015

@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();

myselfsufiyan commented Jul 6, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@leftty

leftty 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?

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?

@sooyoung32

This comment has been minimized.

Show comment
Hide comment
@sooyoung32

sooyoung32 commented Dec 24, 2015

+1

@davesave

This comment has been minimized.

Show comment
Hide comment
@davesave

davesave Aug 22, 2016

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

davesave commented Aug 22, 2016

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

@nysyzp

This comment has been minimized.

Show comment
Hide comment
@nysyzp

nysyzp Sep 21, 2016

+1 Any news?

nysyzp commented Sep 21, 2016

+1 Any news?

@ColinJackson22

This comment has been minimized.

Show comment
Hide comment
@ColinJackson22

ColinJackson22 commented Sep 27, 2016

+1

@betelgeuse

This comment has been minimized.

Show comment
Hide comment
@betelgeuse

betelgeuse commented Sep 28, 2016

@davesave @nysyzp @ColinJackson22 github supports reactions nowadays so please stop spamming +1 comments.

https://github.com/blog/2119-add-reactions-to-pull-requests-issues-and-comments

@alexellis

This comment has been minimized.

Show comment
Hide comment
@alexellis

alexellis Jan 26, 2017

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

alexellis commented Jan 26, 2017

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

@jesg

This comment has been minimized.

Show comment
Hide comment
@jesg

jesg Jan 27, 2017

Collaborator

@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 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

@jesg

This comment has been minimized.

Show comment
Hide comment
@jesg

jesg Feb 15, 2017

Collaborator

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 15, 2017

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

this wouldn't be very hard to implement.

@jesg

This comment has been minimized.

Show comment
Hide comment
@jesg

jesg Feb 21, 2017

Collaborator

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

Collaborator

jesg commented Feb 21, 2017

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

@flashflight

This comment has been minimized.

Show comment
Hide comment
@flashflight

flashflight commented May 18, 2017

+1

@Danondso

This comment has been minimized.

Show comment
Hide comment
@Danondso

Danondso Jan 3, 2018

Does this happen to be on anyone's roadmap by chance?

Danondso commented Jan 3, 2018

Does this happen to be on anyone's roadmap by chance?

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