Testing 'prompt' popups

246 views
Skip to first unread message

Mike

unread,
Jun 7, 2013, 8:17:57 AM6/7/13
to be...@googlegroups.com
Hi, I'm trying to test a page that has a javascript prompt popup. I've read the topic
https://groups.google.com/forum/#!topic/behat/Cc6OcObVWnE and I'm utilising the code by benjamin lazarecki (https://gist.github.com/2888851)
The scenario is as follows
 features/behat/behat-test-window-alert.feature

Feature: For other tests to work, Behat must be able to type into a popup
@behat
  Background:                                                                   
    Given I am logged in as the test user

@javascript
  Scenario: Open and switch to a new window                       
    When I go to "/utils/subwindowwithpassword"

    When I press "Enter"
    And I wait 5 seconds
    Then I should see "Please enter your password" in popup
    When I fill "1234" in popup
    And I confirm the popup
    Then I should see "You're in!"

The javascript is as follows:
    var message = "no message";
    function enter() {
        var pass = prompt("Please enter your password","type here");
        if ( pass == "1234") {
            message = "You're in!";
        } else {
            message = "Go away!";
        }
        var elem = document.getElementById("message");
        elem.innerHTML = message;
    }
   

All the steps 'work' (show in green) except the last one. The popup flashes up and disappears, too quick to see if the "1234" goes in. Any attempt to $this->getSession()->wait(5000); will complain about a modal dialog ad fail straight away. If in the javascript I say if ( pass == "type here") then the last step passes.

Is anyone getting this sort of scenario to work?

Mike

unread,
Jun 10, 2013, 5:46:31 AM6/10/13
to be...@googlegroups.com
Some more information which may be useful: Looking at the output of Selenium2 standalone server:
11:24:21.409 INFO - Executing: [get alert text] at URL: /session/fc53eaa6-0f7c-4891-8752-65c1756d54f3/alert_text)
11:24:21.504 INFO - Done: /session/fc53eaa6-0f7c-4891-8752-65c1756d54f3/alert_text
11:24:21.517 INFO - Executing: [accept alert] at URL: /session/fc53eaa6-0f7c-4891-8752-65c1756d54f3/accept_alert)
11:24:21.541 INFO - Done: /session/fc53eaa6-0f7c-4891-8752-65c1756d54f3/accept_alert

There is no representation of post_Alert in there, so I guess this webdriver doesn't support it? According to the Selenium docs it should. So, I'm asking if anyone has used this sucessfully?

Vanille

unread,
Jun 13, 2013, 7:23:53 AM6/13/13
to be...@googlegroups.com
 You can use this function taken from the Behatch extension to insert a breakpoint so you can inspect what happens at a given point:

   /**
     * Pauses the scenario until the user presses a key. Useful when debugging a scenario.
     *
     * @Then /^(?:|I )put a breakpoint$/
     */
    public function iPutABreakpoint()
    {
        fwrite(STDOUT, "\033[s    \033[93m[Breakpoint] Press \033[1;93m[RETURN]\033[0;93m to continue...\033[0m");
        while (fgets(STDIN, 1024) == '') {}
        fwrite(STDOUT, "\033[u");

        return;
    }

or you can print out debugging information using:

$this->printDebug($str);

Mike

unread,
Oct 16, 2013, 10:26:58 AM10/16/13
to be...@googlegroups.com
Cracked it!

OK, so I had written a java program linked directly to Selenium and used "Alert alert = driver.switchTo().alert(); alert.sendKeys(text.toString());" to at least show that it wasn't a limitation of Selenium itself or of it's support for the browser.

Vanille's breakpoint led me to the solution: I put one just before my "When I fill "1234" in popup" and was able to "type" into the popup with the following from the terminal:

curl -X POST -d "1234" http://localhost:4444/wd/hub/session/cbac2503-65c9-4f20-ab15-2483e372a3b6/alert_text/

But by the error messages this command to Selenium was expecting a parameter in JSON format; documentation confirms this (http://code.google.com/p/selenium/wiki/JsonWireProtocol#Commands)

OK then, let's try this:

curl -X POST -d "{\"text\": \"1234\"}" http://localhost:4444/wd/hub/session/cbac2503-65c9-4f20-ab15-2483e372a3b6/alert_text

Success!

After some looking around in the code, the thing is that if HTTP method is POST and the arguments/parameters are an array then it will JSON-encode them. So if I change Bejamin's original setPopupText to this:

    public function setPopupText($message)
    {
        $this->getSession()->getDriver()->getWebDriverSession()->postAlert_text(array("text" => $message));
    }

Then it works as expected!


Reply all
Reply to author
Forward
0 new messages