@@ -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
0 commit comments