Skip to content

Commit ae6a399

Browse files
committed
fixes #26 - element() should throw WebDriver\Exception\NoSuchElement instead of null
1 parent 5f23164 commit ae6a399

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

lib/WebDriver/AbstractWebDriver.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ public function getURL()
103103
protected function curl($requestMethod, $command, $parameters = null, $extraOptions = array())
104104
{
105105
if ($parameters && is_array($parameters) && $requestMethod !== 'POST') {
106-
throw WebDriverException::factory(WebDriverException::NO_PARAMETERS_EXPECTED, sprintf(
107-
'The http request method called for %s is %s but it has to be POST' .
108-
' if you want to pass the JSON parameters %s',
109-
$command,
110-
$requestMethod,
111-
json_encode($parameters)
112-
));
106+
throw WebDriverException::factory(
107+
WebDriverException::NO_PARAMETERS_EXPECTED,
108+
sprintf(
109+
'The http request method called for %s is %s but it has to be POST if you want to pass the JSON parameters %s',
110+
$command,
111+
$requestMethod,
112+
json_encode($parameters)
113+
)
114+
);
113115
}
114116

115117
$url = sprintf('%s%s', $this->url, $command);
@@ -154,9 +156,9 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
154156
public function __call($name, $arguments)
155157
{
156158
if (count($arguments) > 1) {
157-
throw WebDriverException::factory(WebDriverException::JSON_PARAMETERS_EXPECTED,
158-
'Commands should have at most only one parameter,' .
159-
' which should be the JSON Parameter object'
159+
throw WebDriverException::factory(
160+
WebDriverException::JSON_PARAMETERS_EXPECTED,
161+
'Commands should have at most only one parameter, which should be the JSON Parameter object'
160162
);
161163
}
162164

@@ -170,11 +172,14 @@ public function __call($name, $arguments)
170172

171173
$methods = $this->methods();
172174
if (!in_array($requestMethod, (array) $methods[$webdriverCommand])) {
173-
throw WebDriverException::factory(WebDriverException::INVALID_REQUEST, sprintf(
174-
'%s is not an available http request method for the command %s.',
175-
$requestMethod,
176-
$webdriverCommand
177-
));
175+
throw WebDriverException::factory(
176+
WebDriverException::INVALID_REQUEST,
177+
sprintf(
178+
'%s is not an available http request method for the command %s.',
179+
$requestMethod,
180+
$webdriverCommand
181+
)
182+
);
178183
}
179184

180185
$results = $this->curl(
@@ -198,7 +203,8 @@ public function __call($name, $arguments)
198203
private function getRequestMethod($webdriverCommand)
199204
{
200205
if (!array_key_exists($webdriverCommand, $this->methods())) {
201-
throw WebDriverException::factory(array_key_exists($webdriverCommand, $this->obsoleteMethods())
206+
throw WebDriverException::factory(
207+
array_key_exists($webdriverCommand, $this->obsoleteMethods())
202208
? WebDriverException::OBSOLETE_COMMAND : WebDriverException::UNKNOWN_COMMAND,
203209
sprintf('%s is not a valid WebDriver command.', $webdriverCommand)
204210
);

lib/WebDriver/Container.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,32 @@ public function element($using = null, $value = null)
6666
$locatorJson
6767
);
6868
} catch (WebDriverException\NoSuchElement $e) {
69+
throw WebDriverException::factory(
70+
WebDriverException::NO_SUCH_ELEMENT,
71+
sprintf(
72+
"Element not found with %s, %s\n\n%s",
73+
$locatorJson['using'],
74+
$locatorJson['value'],
75+
$e->getMessage()
76+
),
77+
$e
78+
);
79+
}
80+
81+
$element = $this->webDriverElement($results['value']);
82+
83+
if ($element === null) {
6984
throw WebDriverException::factory(WebDriverException::NO_SUCH_ELEMENT,
7085
sprintf(
71-
'Element not found with %s, %s',
86+
"Element not found with %s, %s\n\n%s",
7287
$locatorJson['using'],
73-
$locatorJson['value']) . "\n\n" . $e->getMessage(), $e
88+
$locatorJson['value'],
89+
$e->getMessage()
90+
)
7491
);
7592
}
7693

77-
return $this->webDriverElement($results['value']);
94+
return $element;
7895
}
7996

8097
/**
@@ -138,7 +155,8 @@ private function parseArgs($method, $argv)
138155
}
139156

140157
default:
141-
throw WebDriverException::factory(WebDriverException::JSON_PARAMETERS_EXPECTED,
158+
throw WebDriverException::factory(
159+
WebDriverException::JSON_PARAMETERS_EXPECTED,
142160
sprintf('Invalid arguments to %s method: %s', $method, print_r($argv, true))
143161
);
144162
}
@@ -159,7 +177,8 @@ private function parseArgs($method, $argv)
159177
public function locate($using, $value)
160178
{
161179
if (!in_array($using, $this->strategies)) {
162-
throw WebDriverException::factory(WebDriverException::UNKNOWN_LOCATOR_STRATEGY,
180+
throw WebDriverException::factory(
181+
WebDriverException::UNKNOWN_LOCATOR_STRATEGY,
163182
sprintf('Invalid locator strategy %s', $using)
164183
);
165184
}

0 commit comments

Comments
 (0)