Re: [selenium-developers] Re: Handling alerts in HtmlUnit

1,259 views
Skip to first unread message

Luke Inman-Semerau

unread,
Nov 16, 2012, 11:42:11 AM11/16/12
to selenium-...@googlegroups.com
That would appear correct, that no one has attempted to implement alert handling with HtmlUnit.

Patches welcome :)

On Fri, Nov 16, 2012 at 5:16 AM, Ron Inbar <r...@evento.co.il> wrote:
Can you at least confirm that I understand the problem correctly, i.e. that Selenium doesn't call HtmlUnit's setAlertHandler?

Thanks,

Ron


On Thursday, November 15, 2012 1:46:42 PM UTC+2, Ron Inbar wrote:
Hi,

I'm not really a Selenium developer but I posted my question to the Selenium Users group and didn't get any replies so I thought maybe I'll have more luck here.
My problem is handling an alert using the WebDriver protocol and HtmlUnit. From the Selenium server's output (attached below) it appears that there is no way to do it because Selenium doesn't install an alert handler in HtmlUnit.
My code is in PHP so I communicate with the Selenium server via the WebDriver wire protocol, not the Java API.
What I do is send a /session/:sessionId/alert_text request to the Selenium server and check the response to see if it contains a NoAlertPresent error.
This works fine when I use the Chrome driver, but when I switch to HtmlUnit it breaks down.
I found that HtmlUnit has a setAlertHandler method. Is there a technical reason that Selenium doesn't use it?

Thanks,

Ron

Here is my code (it uses Nearsoft's PHP Selenium Client):

        $submitButton = $this->driver->findElement(By::id('btn_submit'));
        $submitButton->click();
        // Probe for a validation error.
        try {
            $alertText = $this->driver->getAlertText();
            $this->driver->dismissAlert();
        } catch (\SeleniumClient\Http\SeleniumNoAlertOpenErrorException $exc) {
            return new VerificationPage($this->driver);
        }
        if (false !== stripos($alertText, Resources::inputErrorMessage)) {
            throw new InputErrorException($alertText);
        }

When no alert is present I see the following output:

14:04:21.683 INFO - Executing: [get alert text] at URL: /session/1351512102002/alert_text)

14:04:21.687 WARN - Exception thrown

java.lang.UnsupportedOperationException: alert()

        at org.openqa.selenium.htmlunit.HtmlUnitDriver$HtmlUnitTargetLocator.alert(HtmlUnitDriver.java:978)

        at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringTargetLocator.alert(EventFiringWebDriver.java:592)

        at org.openqa.selenium.remote.server.handler.GetAlertText.call(GetAlertText.java:31)

        at org.openqa.selenium.remote.server.handler.GetAlertText.call(GetAlertText.java:1)

        at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

        at java.util.concurrent.FutureTask.run(Unknown Source)

        at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:150)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

        at java.lang.Thread.run(Unknown Source)

14:04:21.720 WARN - Exception: alert()

 

When there is an alert I see the following output:

14:14:39.502 WARN - window.alert("Input error - please correct the highlighted spaces:

 

- First name

- Last name

- Street, No.

- post code

- Location

- Telephone (only for further questions)

- E-mail

- Please choose payment method

- Please choose delivery method

") no alert handler installed

14:14:39.541 INFO - Done: /session/1351512102005/element/45/click

...and the rest looks exactly as in the first case, as if the alert never happened:

14:15:42.926 INFO - Executing: [get alert text] at URL: /session/1351512102005/alert_text)

14:15:42.926 WARN - Exception thrown

java.lang.UnsupportedOperationException: alert()

        at org.openqa.selenium.htmlunit.HtmlUnitDriver$HtmlUnitTargetLocator.alert(HtmlUnitDriver.java:978)

        at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringTargetLocator.alert(EventFiringWebDriver.java:592)

        at org.openqa.selenium.remote.server.handler.GetAlertText.call(GetAlertText.java:31)

        at org.openqa.selenium.remote.server.handler.GetAlertText.call(GetAlertText.java:1)

        at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

        at java.util.concurrent.FutureTask.run(Unknown Source)

        at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:150)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

        at java.lang.Thread.run(Unknown Source)

14:15:42.927 WARN - Exception: alert()


--
 
 

Message has been deleted

Marc Guillemot

unread,
Nov 19, 2012, 2:38:40 AM11/19/12
to selenium-...@googlegroups.com
Le 16/11/2012 17:42, Luke Inman-Semerau a �crit :
> That would appear correct, that no one has attempted to implement alert
> handling with HtmlUnit.
> http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/htmlunit/HtmlUnitDriver.java#977
>
> Patches welcome :)

I've proposed a patch, about 2 years ago:
http://code.google.com/p/selenium/issues/detail?id=1105

no idea how this patch behaves with current state of the sources.

The difficulty in implementing Webdriver alerts API in the
HtmlUnitDriver is that it forces to introduce a new thread in which the
WebClient has to be controlled.

Cheers,
Marc.
--
HtmlUnit support & consulting from the source
Blog: http://mguillem.wordpress.com

Ron Inbar

unread,
Nov 19, 2012, 6:30:11 AM11/19/12
to selenium-...@googlegroups.com
Thanks, Marc! I'll give it a try and let you know if it solved my problem.

Regards,

Ron

On Mon, Nov 19, 2012 at 9:38 AM, Marc Guillemot <mguil...@yahoo.fr> wrote:
--



Ron Inbar

unread,
Apr 3, 2013, 12:39:02 PM4/3/13
to selenium-...@googlegroups.com, ha...@evento.co.il
For the benefit of anyone who happens to stumble upon this thread:
With some effort, I managed to adapt Marc's code to Selenium 2.28.0. It works 99% of the time but seems to suffer from some elusive race condition which only manifests itself on one machine that has 12 CPUs.
I recently discovered a workaround suggested by Luke in the context of the GhostDriver project, so instead of trying to fix the race condition I reverted to the official server (2.31.0) and used Luke's workaround, which is simple and reliable.

Thank you, Marc and Luke, for your help.

Regards,
Reply all
Reply to author
Forward
0 new messages