Skip to content

Commit

Permalink
Merge pull request #11 from alexandresalome/alert-messages
Browse files Browse the repository at this point in the history
add acceptAlert() and dismissAlert()
  • Loading branch information
Alexandre Salomé committed May 7, 2014
2 parents 57a740c + c62c200 commit acec1b1
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ Add the library to your **composer.json**:
Changelog
---------

**v0.9**

* Alert messages

**v0.8**

* *Behat* new steps for form assertions
Expand Down
10 changes: 10 additions & 0 deletions doc/behat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ Available steps
| xpath=//input | foobar |
| A select field | The option label |
**Alert messages**

When I confirm alert message

When I dismiss alert message

Then alert text should be "**some text**"

I answer alert with "**some text**"

**Mouse**

When I move mouse to "**css=#field**"
Expand Down
30 changes: 30 additions & 0 deletions doc/browser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ If your script takes too much time, a **ScriptTimeoutException** will be thrown.
* `Reference of execute method <https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute>`_
* `Reference of execute_async method <https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute_async>`_

Alert messages
--------------

You can use the ``acceptAlert()`` method to simulate the click on "OK" button of the currently displayed alert:

.. code-block:: php
$browser->acceptAlert();
This method also work for ``confirm()`` dialogs.

If you want to click "Cancel" in the confirmation dialog, use method ``dismissAlert()``:

.. code-block:: php
$browser->dismissAlert();
To get the message of the alert, use ``getAlertText()``:

.. code-block:: php
$browser->getAlertText();
And to give an answer to prompt messages, use ``answerAlert()``:

.. code-block:: php
$browser->answerAlert("my answer");
$browser->acceptAlert();
Handling windows
----------------

Expand Down
45 changes: 45 additions & 0 deletions src/WebDriver/Behat/WebDriverContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,51 @@ public function iShouldNotSee($text)
$this->iShouldSee(0, $text);
}

/**
* @Then /^I accept alert$/
*/
public function iAcceptAlert()
{
$this->tryRepeating(function ($browser) {
$browser->acceptAlert();
});
}

/**
* @Then /^I dismiss alert$/
*/
public function iDismissAlert()
{
$this->tryRepeating(function ($browser) {
$browser->dismissAlert();
});
}

/**
* @Then /^alert text should be "((?:[^"]|"")*)"$/
*/
public function alertTextShouldBe($text)
{
$text = $this->unescape($text);
$this->tryRepeating(function ($browser) use ($text) {
$actual = $browser->getAlertText();
if (false === strpos($actual, $text)) {
throw new \RuntimeException(sprintf('Unable to find text "%" in alert "%s".', $text, $actual));
}
});
}

/**
* @Then /^I answer alert with "((?:[^"]|"")*)"$/
*/
public function iAnswerAlertWith($text)
{
$text = $this->unescape($text);
$this->tryRepeating(function ($browser) use ($text) {
$actual = $browser->answerAlert($text);
});
}

/**
* @When /^I fill:$/
*/
Expand Down
52 changes: 52 additions & 0 deletions src/WebDriver/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,58 @@ public function open($url)
return $this;
}

/**
* Accepts the currently displayed alert dialog. Usually, this is
* equivalent to clicking on the 'OK' button in the dialog.
*
* @return Browser fluid interface
*/
public function acceptAlert()
{
$this->request('POST', 'accept_alert');

return $this;
}

/**
* Gets the text of the currently displayed JavaScript alert(), confirm(),
* or prompt() dialog.
*
* @return string the alert text
*/
public function getAlertText()
{
return $this->requestValue('alert_text');
}

/**
* Sends keystrokes to a JavaScript prompt() dialog.
*
* @param string $text
*
* @return Browser fluid interface
*/
public function answerAlert($text)
{
$this->request('POST', 'alert_text', json_encode(array('text' => $text)));

return $this;
}

/**
* Dismisses the currently displayed alert dialog. For confirm() and
* prompt() dialogs, this is equivalent to clicking the 'Cancel' button.
* For alert() dialogs, this is equivalent to clicking the 'OK' button.
*
* @return Browser fluid interface
*/
public function dismissAlert()
{
$this->request('POST', 'dismiss_alert');

return $this;
}

/**
* Run a Javascript snippet on browser.
*
Expand Down
2 changes: 2 additions & 0 deletions src/WebDriver/Exception/ExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ static public function createExceptionFromArray(array $array)
return new InvalidCookieDomainException($message);
} elseif ($status == self::STATUS_UNEXPECTED_ALERT_OPEN) {
return new UnexpectedAlertOpenException($message);
} elseif ($status == self::STATUS_NO_ALERT_OPEN_ERROR) {
return new NoAlertOpenErrorException($message);
} elseif ($status == self::STATUS_SCRIPT_TIMEOUT) {
return new ScriptTimeoutException($message);
}
Expand Down
18 changes: 18 additions & 0 deletions src/WebDriver/Exception/NoAlertOpenErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of PHP WebDriver Library.
* (c) Alexandre Salomé <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WebDriver\Exception;

/**
* @author Alexandre Salomé <[email protected]>
*/
class NoAlertOpenErrorException extends \RuntimeException implements ExceptionInterface
{
}
51 changes: 51 additions & 0 deletions tests/WebDriver/Tests/Behat/WebDriverContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,57 @@ public function testIDeleteCookie()

}

public function testIAcceptAlert()
{
$ctx = $this->getContext($browser = $this->getBrowser());
$browser->open($this->getUrl('alert.php'));

$ctx->iClickOn('Alert');
$ctx->iAcceptAlert();
$ctx->iShouldSee('', 'alerted');
}

public function testAlertTextShouldBe()
{
$ctx = $this->getContext($browser = $this->getBrowser());
$browser->open($this->getUrl('alert.php'));

$ctx->iClickOn('Alert');
$ctx->alertTextShouldBe('ALERT!');
$ctx->iAcceptAlert();

$ctx->iClickOn('Confirm');
$ctx->alertTextShouldBe('CONFIRM?');
$ctx->iAcceptAlert();
$ctx->iShouldSee('', 'confirmed');
}

public function testIAnswerAlertWith()
{
$ctx = $this->getContext($browser = $this->getBrowser());
$browser->open($this->getUrl('alert.php'));

$ctx->iClickOn('Prompt');
$ctx->alertTextShouldBe('PROMPT?');
$ctx->iAnswerAlertWith("foobar");
$ctx->iAcceptAlert();
$ctx->iShouldSee('', 'answered: foobar');

$ctx->iClickOn('Prompt');
$ctx->iDismissAlert();
$ctx->iShouldSee('', 'not answered');
}

public function testIDismissAlert()
{
$ctx = $this->getContext($browser = $this->getBrowser());
$browser->open($this->getUrl('alert.php'));

$ctx->iClickOn('Confirm');
$ctx->iDismissAlert();
$ctx->iShouldSee('', 'dismissed');
}

// Abstract method tests

public function testGetUrl()
Expand Down
55 changes: 55 additions & 0 deletions tests/WebDriver/Tests/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,59 @@ public function testGetSource()
$this->assertContains('<head>', $source);
$this->assertContains('<body>', $source);
}

public function testGetAlertText()
{
$browser = $this->getBrowser();
$browser->open($this->getUrl('alert.php'));

$browser->element(By::id('alert'))->click();
$this->assertEquals('ALERT!', $browser->getAlertText());
$browser->acceptAlert();
}

public function testAnswerAlert()
{
$browser = $this->getBrowser();
$browser->open($this->getUrl('alert.php'));

$browser->element(By::id('prompt'))->click();
$this->assertEquals('PROMPT?', $browser->getAlertText());
$browser->answerAlert('foobar');
$browser->acceptAlert();
$this->assertContains('answered: foobar', $browser->getText());

$browser->element(By::id('prompt'))->click();
$this->assertEquals('PROMPT?', $browser->getAlertText());
$browser->dismissAlert();
$this->assertContains('not answered', $browser->getText());
}

public function testAcceptAlert()
{
$browser = $this->getBrowser();
$browser->open($this->getUrl('alert.php'));

$browser->element(By::id('alert'))->click();
$browser->acceptAlert();
$this->assertContains('alerted', $browser->getText());

$browser->element(By::id('confirm'))->click();
$browser->acceptAlert();
$this->assertContains('confirmed', $browser->getText());
}

public function testDismissAlert()
{
$browser = $this->getBrowser();
$browser->open($this->getUrl('alert.php'));

$browser->element(By::id('alert'))->click();
$browser->dismissAlert();
$this->assertContains('alerted', $browser->getText());

$browser->element(By::id('confirm'))->click();
$browser->dismissAlert();
$this->assertContains('dismissed', $browser->getText());
}
}
54 changes: 54 additions & 0 deletions website/alert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<title>Alert page</title>
</head>
<body>
<h1>Alert tests</h1>
<p >Feedback: <span id="alert-feedback">nothing</span></p>
<div class="hover-wrapper">
<h2 class="title">Hover me</h2>
<p class="content">You see this text because of hover</p>
<p>
<button id="reset" onclick="test_reset()">Reset</button>
<button id="alert" onclick="test_alert()">Alert</button>
<button id="confirm" onclick="test_confirm()">Confirm</button>
<button id="prompt" onclick="test_prompt()">Prompt</button>
</p>
</div>
<script type="text/javascript">
function test_reset()
{
document.getElementById('alert-feedback').innerHTML = 'nothing';
}
function test_alert()
{
alert("ALERT!");
document.getElementById('alert-feedback').innerHTML = 'alerted';
}
function test_prompt()
{
val = prompt("PROMPT?");
console.log(val);
if (null === val) {
document.getElementById('alert-feedback').innerHTML = 'not answered';
} else {
document.getElementById('alert-feedback').innerHTML = 'answered: ' + val;
}
}
function test_confirm()
{
if (confirm("CONFIRM?")) {
document.getElementById('alert-feedback').innerHTML = 'confirmed';
} else {
document.getElementById('alert-feedback').innerHTML = 'dismissed';
}
}
</script>
</body>
</html>
1 change: 1 addition & 0 deletions website/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<li><a href="other.php">Another page</a></li>
<li><a href="form.php">Page to test form stuff</a></li>
<li><a href="cookies.php">Cookies page</a></li>
<li><a href="alert.php">Alerts page</a></li>
<li><a href="mouse.php">Mouse tests</a></li>
<li><a href="request.php">What is your request?</a></li>
<li><a href="tree.php">A tree with lot of informations, for node processing</a></li>
Expand Down

0 comments on commit acec1b1

Please sign in to comment.