Skip to content

Commit d8b3f82

Browse files
committed
add chainable() support
1 parent f85d466 commit d8b3f82

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

lib/WebDriver/AbstractWebDriver.php

+30
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ protected function obsoleteMethods()
6262
return [];
6363
}
6464

65+
/**
66+
* Return array of chainable method/property names
67+
*
68+
* @return array
69+
*/
70+
protected function chainable()
71+
{
72+
return [];
73+
}
74+
6575
/**
6676
* Constructor
6777
*
@@ -172,6 +182,10 @@ public function __call($name, $arguments)
172182
return new $className($this->url . '/' . $this->extensions[$name][1]);
173183
}
174184

185+
if (count($arguments) === 0 && array_key_exists($name, $this->chainable())) {
186+
return call_user_func([$this, $name]);
187+
}
188+
175189
if (preg_match('/^(get|post|delete)/', $name, $matches)) {
176190
$requestMethod = strtoupper($matches[0]);
177191
$webdriverCommand = strtolower(substr($name, strlen($requestMethod)));
@@ -204,6 +218,22 @@ public function __call($name, $arguments)
204218
return $result['value'];
205219
}
206220

221+
/**
222+
* Magic method that maps property names to chainable methods
223+
*
224+
* @param string $name Property name
225+
*
226+
* @return mixed
227+
*/
228+
public function __get($name)
229+
{
230+
if (array_key_exists($name, $this->chainable())) {
231+
return call_user_func([$this, $name]);
232+
}
233+
234+
trigger_error('Undefined property: ' . __CLASS__ . '::$' . $name, E_USER_WARNING);
235+
}
236+
207237
/**
208238
* Serialize script arguments (containing web elements and/or shadow roots)
209239
*

lib/WebDriver/Element.php

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ protected function obsoleteMethods()
8282
];
8383
}
8484

85+
/**
86+
* {@inheritdoc}
87+
*/
88+
protected function chainable()
89+
{
90+
return [
91+
'shadow' => 'shadow',
92+
];
93+
}
94+
8595
/**
8696
* Constructor
8797
*

lib/WebDriver/Session.php

+28
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ protected function obsoleteMethods()
112112
];
113113
}
114114

115+
/**
116+
* {@inheritdoc}
117+
*/
118+
protected function chainable()
119+
{
120+
return [
121+
'actions' => 'actions',
122+
'alert' => 'alert',
123+
'execute' => 'execute',
124+
'frame' => 'frame',
125+
'timeouts' => 'timeouts',
126+
'window' => 'window',
127+
];
128+
}
129+
115130
/**
116131
* Constructor
117132
*
@@ -416,6 +431,19 @@ public function activeElement()
416431
return $this->makeElement($result['value']);
417432
}
418433

434+
/**
435+
* actions method chaining, e.g.,
436+
* - $session->actions()->method() - chaining
437+
* - $session->actions->method() - chaining
438+
*
439+
* @return mixed
440+
*/
441+
protected function actions()
442+
{
443+
return Actions::getInstance($this->url . '/actions');
444+
}
445+
446+
419447
/**
420448
* touch method chaining, e.g.,
421449
* - $session->touch()->method()

0 commit comments

Comments
 (0)