Skip to content

Commit 1352f06

Browse files
author
LuisRBarreras
committed
Magic call Switchto and Test Magic SwitchTo
1 parent b3e9f19 commit 1352f06

File tree

3 files changed

+73
-20
lines changed

3 files changed

+73
-20
lines changed

SeleniumClient/Options.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,14 @@ public function timeouts()
4545
*/
4646
public function window()
4747
{
48-
if(!$this->_window)
49-
{
50-
$this->setWindow(new Window($this->_driver));
51-
}
48+
isset($this->_window) ? : $this->setWindow(new Window($this->_driver));
5249
return $this->_window;
5350
}
5451

5552
/** Sets Window
5653
* @param $window
5754
*/
58-
public function setWindow($window)
59-
{
60-
$this->_window = $window;
61-
}
55+
public function setWindow($window) { $this->_window = $window; }
6256

6357
/**
6458
* Sets cookie

SeleniumClient/WebDriver.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function __construct(DesiredCapabilities $desiredCapabilities = null, $ho
5757
* Example: navigationRefresh, navigationBack.
5858
* Enables window's methods be invoked
5959
* Example: windowMaximize, windowGetPosition.
60+
* Enables TargetLocator's methods
61+
* Examples: switchToWindow, switchToFrame
6062
* Enables findElement and findElements methods be invoked through method missing.
6163
* The methods should be invoked with the format 'findElementBy<strategy>'.
6264
* Arguments should match those required by findElement and findElements methods.
@@ -78,6 +80,11 @@ public function __call( $name, array $args )
7880
return $values;
7981
}
8082

83+
else if ( strpos($name, "switchTo") === 0 ) {
84+
$values = $this->callSwitchTo($name, $args);
85+
return $values;
86+
}
87+
8188
else if( strpos($name, 'findElement') === 0 ){
8289
return $this->callFindElement($name, $args);
8390
}
@@ -136,6 +143,26 @@ private function callWindowMethods($name, $args)
136143
return $values;
137144
}
138145

146+
/**
147+
* Call Target Locator Methods
148+
* @param $name
149+
* @param array $args
150+
* @return mixed
151+
* @throws \Exception
152+
*/
153+
private function callSwitchTo($name, array $args)
154+
{
155+
$method = lcfirst(substr($name, 8));
156+
switch($method){
157+
case 'window':
158+
case 'frame':
159+
$values = call_user_func( array($this->switchTo(), $method),$args[0]);
160+
break;
161+
default: throw new \Exception ('Invalid magic call:'. $name);
162+
}
163+
return $values;
164+
}
165+
139166
/**Call findElement and findElement methods
140167
* @param $name
141168
* @param array $args
@@ -238,19 +265,14 @@ public function getHubUrl() { return $this->_hubUrl; }
238265
*/
239266
public function navigate()
240267
{
241-
if(!$this->_navigate) {
242-
$this->setNavigate(new Navigation($this));
243-
}
268+
isset($this->_navigate) ? : $this->setNavigate(new Navigation($this));
244269
return $this->_navigate;
245270
}
246271

247272
/**Set Navigation
248273
* @param $navigate
249274
*/
250-
public function setNavigate($navigate)
251-
{
252-
$this->_navigate = $navigate;
253-
}
275+
public function setNavigate($navigate) { $this->_navigate = $navigate; }
254276

255277
/**
256278
* Get assigned session id
@@ -277,10 +299,6 @@ public function getScreenShotsDirectory() { return $this->_screenshotsDirectory;
277299
*/
278300
public function manage()
279301
{
280-
281-
if(!$this->_options) {
282-
$this->_options = new Options($this);
283-
}
284302
isset($this->_options) ? : $this->_options = new Options($this);
285303
return $this->_options;
286304
}
@@ -291,10 +309,18 @@ public function manage()
291309
*/
292310
public function switchTo()
293311
{
294-
isset($this->_targetLocator) ? : $this->_targetLocator = new TargetLocator($this);
312+
isset($this->_targetLocator) ? : $this->setSwitchTo(new TargetLocator($this));
295313
return $this->_targetLocator;
296314
}
297315

316+
/**
317+
* Set Target Locator
318+
* @param $targetLocator
319+
*/
320+
public function setSwitchTo($targetLocator) { $this->_targetLocator = $targetLocator; }
321+
322+
323+
298324
/**
299325
* Starts new Selenium session
300326
* @param DesiredCapabilities $desiredCapabilities

SeleniumClientTest/Tests/WebDriverTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ class WebDriverTest extends AbstractTest
1111
{
1212
private $_navigationMock = null;
1313
private $_windowMock = null;
14+
private $_targetLocatorMock = null;
15+
1416
private $_originalNavigate = null;
1517
private $_originalWindow = null;
18+
private $_originalTargetLocator = null;
1619

1720
public function setUp()
1821
{
1922
parent::setUp();
2023
$this->_navigationMock = $this->getMock('Navigation', array('refresh','to','back','forward'));
2124
$this->_windowMock = $this->getMock('Window', array('maximize','close' ,'setSize', 'getSize', 'setPosition', 'getPosition'));
25+
$this->_targetLocatorMock = $this->getMock('TargetLocator', array('window','frame'));
2226
$this->_originalNavigate = $this->_driver->navigate();
2327
$this->_originalWindow = $this->_driver->manage()->window();
28+
$this->_originalTargetLocator = $this->_driver->switchTo();
2429

2530
}
2631

@@ -398,4 +403,32 @@ public function testMagicWindowSetPositionShouldCallMethodSetPosition()
398403
$this->_driver->manage()->setWindow($this->_originalWindow);
399404

400405
}
406+
407+
public function testMagicSwitchToWindowShouldCallMethodWindow()
408+
{
409+
$this->_targetLocatorMock->expects($this->exactly(1))
410+
->method('window');
411+
412+
$this->_driver->setSwitchTo($this->_targetLocatorMock);
413+
414+
$this->_driver->switchToWindow('popup1');
415+
416+
$this->_driver->setSwitchTo($this->_originalTargetLocator);
417+
418+
419+
}
420+
421+
public function testMagicSwitchToFrameShouldCallMethodFrame()
422+
{
423+
$this->_targetLocatorMock->expects($this->exactly(1))
424+
->method('frame');
425+
426+
$this->_driver->setSwitchTo($this->_targetLocatorMock);
427+
428+
$this->_driver->switchToFrame(null);
429+
430+
$this->_driver->setSwitchTo($this->_originalTargetLocator);
431+
432+
}
433+
401434
}

0 commit comments

Comments
 (0)