Skip to content

Commit

Permalink
alerts can be their own object now
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgoucher committed Aug 21, 2012
1 parent 72f6386 commit 1acf4de
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
48 changes: 48 additions & 0 deletions PHPWebDriver/WebDriverAlert.php
@@ -0,0 +1,48 @@
<?php
// Copyright 2012-present Element 34
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

require_once(dirname(__FILE__) . '/WebDriverSession.php');

class PHPWebDriver_WebDriverAlert extends PHPWebDriver_WebDriverSession {
public function __construct($session) {
$this->session = $session;
return $this;
}

function __get($property) {
switch($property) {
// text
case "text":
$result = $this->session->curl('GET', '/alert_text', '', array());
return $result['value'];
default:
if (isset($property)) {
return $this->$property;
}
}
}

public function accept() {

}

public function dismiss() {

}

public function sendKeys($keys) {

}
}
6 changes: 6 additions & 0 deletions PHPWebDriver/WebDriverSession.php
Expand Up @@ -156,6 +156,11 @@ public function deleteWindow($curl_opts = array()) {
return $this;
}

public function switch_to_alert() {
require_once('WebDriverAlert.php');
return new PHPWebDriver_WebDriverAlert($this);
}

public function window($window_handle = 'current') {
$item = new PHPWebDriver_WebDriverSimpleItem($this->url . '/window/' . $window_handle);
return $item->setMethods(array(
Expand Down Expand Up @@ -211,3 +216,4 @@ protected function getElementPath($element_id) {
return sprintf('%s/element/%s', $this->url, $element_id);
}
}
?>
1 change: 1 addition & 0 deletions PHPWebDriver/__init__.php
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
require_once('WebDriver.php');
require_once('WebDriverActionChains.php');
require_once('WebDriverAlert.php');
require_once('WebDriverBase.php');
require_once('WebDriverContainer.php');
require_once('WebDriverDesiredCapabilities.php');
Expand Down
97 changes: 97 additions & 0 deletions test/AlertTest.php
@@ -0,0 +1,97 @@
<?php
// Copyright 2012-present Element 34
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

require_once(dirname(__FILE__) . '/../PHPWebDriver/WebDriver.php');
require_once('LocalWebServer.php');

class AlertTest extends PHPUnit_Framework_TestCase {
protected static $driver;
protected static $pid;
protected static $port;

public static function setUpBeforeClass() {
if (function_exists('pcntl_fork') && function_exists('posix_kill')) {
// get an empty socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, '127.0.0.1', 0);
socket_getsockname($socket, $socket_address, $port);
socket_close($socket);
// set it somewhere
self::$port = $port;
// fork the webserver
self::$pid = pcntl_fork();
if (self::$pid == -1) {
return $this->raiseError('Could not fork child process.');
} elseif (self::$pid == 0) {
$webserver = &new LocalWebServer('127.0.0.1', $port);
$webserver->_driver->setDebugMode(false);
$webserver->documentRoot = dirname(__FILE__) . '/../www';
$webserver->start();
} else {
// the parent process does not have to do anything
}
}
}

public function setUp() {
self::$driver = new PHPWebDriver_WebDriver();
}

public function tearDown() {
$this->session->close();
}

public static function tearDownAfterClass() {
// kill the server
posix_kill(self::$pid, SIGKILL);
}

/**
* @group alert
*/
public function testAlertAccept() {
$this->session = self::$driver->session();
$this->session->open("http://127.0.0.1:" . self::$port . "/alerts.html");
$e = $this->session->element("id", "alert");
$e->click();
$a = $this->session->switch_to_alert();
$this->assertEquals($a->text, "cheese");
}

/**
* @group alert
*/
public function testAlertDecline() {
$this->session = self::$driver->session();
$this->session->open("https://github.com/element-34/php-webdriver");
}

/**
* @group alert
*/
public function testAlertText() {
$this->session = self::$driver->session();
$this->session->open("https://github.com/element-34/php-webdriver");
}

/**
* @group alert
*/
public function testAlertSendKeys() {
$this->session = self::$driver->session();
$this->session->open("https://github.com/element-34/php-webdriver");
}
}

0 comments on commit 1acf4de

Please sign in to comment.