Permalink
Browse files

added support for iframe/window switching in browsers, that support it (

closes #175)

For now, only 2 drivers support this functionality:

- SeleniumDriver
- Selenium2Driver

Sahi OS have huge problems with iframe and window management. Basically,
there is needed API, but i wasn't been able to make it work. Looks like it's
totally broken.

As of ZombieDriver - Zombie.js simply doesn't have iframe/window management
API.
  • Loading branch information...
1 parent 109af41 commit ab7ed3d9aeb9a1a5554a9582262881c8fc8531a0 @everzet everzet committed May 3, 2012
@@ -86,6 +86,20 @@ function back();
function setBasicAuth($user, $password);
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ */
+ function switchToWindow($name = null);
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ */
+ function switchToIFrame($name = null);
+
+ /**
* Sets specific request header on client.
*
* @param string $name
@@ -148,6 +148,30 @@ public function back()
}
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ *
+ * @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
+ */
+ public function switchToWindow($name = null)
+ {
+ throw new UnsupportedDriverActionException('Window management is not supported by %s', $this);
+ }
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ *
+ * @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
+ */
+ public function switchToIFrame($name = null)
+ {
+ throw new UnsupportedDriverActionException('iFrame management is not supported by %s', $this);
+ }
+
+ /**
* @see Behat\Mink\Driver\DriverInterface::setBasicAuth()
*/
public function setBasicAuth($user, $password)
@@ -29,6 +29,7 @@ class SahiDriver implements DriverInterface
private $browserName;
private $client;
private $session;
+ private $window;
/**
* Initializes Sahi driver.
@@ -153,6 +154,30 @@ public function setBasicAuth($user, $password)
}
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ *
+ * @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
+ */
+ public function switchToWindow($name = null)
+ {
+ throw new UnsupportedDriverActionException('Window management is broken in Sahi, so %s does not support switching into windows', $this);
+ }
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ *
+ * @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
+ */
+ public function switchToIFrame($name = null)
+ {
+ throw new UnsupportedDriverActionException('Sahi does not have ability to switch into iFrames, so %s does not support it too', $this);
+ }
+
+ /**
* @see Behat\Mink\Driver\DriverInterface::setRequestHeader()
*
* @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
@@ -231,6 +256,7 @@ public function find($xpath)
return count;
})()
JS;
+
$count = intval($this->evaluateScript($function));
$elements = array();
for ($i = 0; $i < $count; $i++) {
@@ -292,6 +292,26 @@ public function back()
}
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ */
+ public function switchToWindow($name = null)
+ {
+ $this->wdSession->focusWindow($name ? $name : '');
+ }
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ */
+ public function switchToIFrame($name = null)
+ {
+ $this->wdSession->frame(array('id' => $name));
+ }
+
+ /**
* Sets HTTP Basic authentication parameters
*
* @param string|false $user user name or false to disable authentication
@@ -175,6 +175,30 @@ public function back()
}
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ */
+ public function switchToWindow($name = null)
+ {
+ $this->browser->selectWindow($name ? $name : 'null');
+ }
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ */
+ public function switchToIFrame($name = null)
+ {
+ if ($name) {
+ $this->browser->selectFrame('dom=window.frames["'.$name.'"]');
+ } else {
+ $this->browser->selectFrame('relative=top');
+ }
+ }
+
+ /**
* @see Behat\Mink\Driver\DriverInterface::setBasicAuth()
*/
public function setBasicAuth($user, $password)
@@ -211,6 +211,30 @@ public function back()
}
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ *
+ * @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
+ */
+ public function switchToWindow($name = null)
+ {
+ throw new UnsupportedDriverActionException('Window management is not supported by %s', $this);
+ }
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ *
+ * @throws Behat\Mink\Exception\UnsupportedDriverActionException action is not supported by this driver
+ */
+ public function switchToIFrame($name = null)
+ {
+ throw new UnsupportedDriverActionException('iFrame management is not supported by %s', $this);
+ }
+
+ /**
* @see Behat\Mink\Driver\DriverInterface::setBasicAuth()
*/
public function setBasicAuth($user, $password)
View
@@ -227,6 +227,26 @@ public function forward()
}
/**
+ * Switches to specific browser window.
+ *
+ * @param string $name window name (null for switching back to main window)
+ */
+ public function switchToWindow($name = null)
+ {
+ $this->driver->switchToWindow($name);
+ }
+
+ /**
+ * Switches to specific iFrame.
+ *
+ * @param string $name iframe name (null for switching back)
+ */
+ public function switchToIFrame($name = null)
+ {
+ $this->driver->switchToIFrame($name);
+ }
+
+ /**
* Execute JS in browser.
*
* @param string $script javascript
@@ -6,6 +6,57 @@
abstract class JavascriptDriverTest extends GeneralDriverTest
{
+ public function testIFrame()
+ {
+ $this->getSession()->visit($this->pathTo('/iframe.php'));
+ $page = $this->getSession()->getPage();
+
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('Main window div text', $el->getText());
+
+ $this->getSession()->switchToIFrame('subframe');
+
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('iFrame div text', $el->getText());
+
+ $this->getSession()->switchToIFrame();
+
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('Main window div text', $el->getText());
+ }
+
+ public function testWindow()
+ {
+ $this->getSession()->visit($this->pathTo('/window.php'));
+ $session = $this->getSession();
+ $page = $session->getPage();
+
+ $page->clickLink('Popup #1');
+ $page->clickLink('Popup #2');
+
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('Main window div text', $el->getText());
+
+ $session->switchToWindow('popup_1');
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('Popup#1 div text', $el->getText());
+
+ $session->switchToWindow('popup_2');
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('Popup#2 div text', $el->getText());
+
+ $session->switchToWindow(null);
+ $el = $page->find('css', '#text');
+ $this->assertNotNull($el);
+ $this->assertSame('Main window div text', $el->getText());
+ }
+
public function testAriaRoles()
{
$this->getSession()->visit($this->pathTo('/aria_roles.php'));
@@ -48,4 +48,10 @@ public function testPrepareXPath()
$this->assertEquals("Single quote'", $method->invokeArgs($driver, array("Single quote'")));
$this->assertEquals('Double quote\"', $method->invokeArgs($driver, array('Double quote"')));
}
+
+ // Sahi doesn't support iFrames switching
+ public function testIFrame() {}
+
+ // Sahi doesn't support window switching
+ public function testWindow() {}
}
@@ -28,4 +28,10 @@ public function testWait() {}
* Zombie.js doesn't handle selects without values
*/
public function testIssue193() {}
+
+ // Zombie.js doesn't support iFrames switching
+ public function testIFrame() {}
+
+ // Zombie.js doesn't support window switching
+ public function testWindow() {}
}
@@ -0,0 +1,11 @@
+<html>
+<body>
+
+<iframe src="iframe_inner.php" name="subframe"></iframe>
+
+<div id="text">
+ Main window div text
+</div>
+
+</body>
+</html>
@@ -0,0 +1,9 @@
+<html>
+<body>
+
+ <div id="text">
+ iFrame div text
+ </div>
+
+</body>
+</html>
@@ -0,0 +1,12 @@
+<html>
+<head>
+ <title>popup_1</title>
+</head>
+<body>
+
+ <div id="text">
+ Popup#1 div text
+ </div>
+
+</body>
+</html>
@@ -0,0 +1,12 @@
+<html>
+<head>
+ <title>popup_2</title>
+</head>
+<body>
+
+ <div id="text">
+ Popup#2 div text
+ </div>
+
+</body>
+</html>
@@ -0,0 +1,25 @@
+<html>
+<body>
+
+<script>
+function newPopup(url, title) {
+ popupWindow = window.open(
+ url, title,'height=100,width=200,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes'
+ );
+}
+</script>
+
+<a href="javascript:newPopup('popup1.php', 'popup_1');">
+ Popup #1
+</a>
+
+<a href="javascript:newPopup('popup2.php', 'popup_2');">
+ Popup #2
+</a>
+
+<div id="text">
+ Main window div text
+</div>
+
+</body>
+</html>

0 comments on commit ab7ed3d

Please sign in to comment.