Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced <obj> instanceof Array with Array.isArray() #452

Merged
merged 1 commit into from
Dec 13, 2016

Conversation

iainbeeston
Copy link
Contributor

I've been seeing a bug where at certain points in my tests, code to
retrieve elements like this:

driver.elementsByAccessibilityId("foo bar")

would throw an error:

[elementsByAccessibilityId("foo bar")] Response value field is not an Array.

After looking into the for wd code, I could see this was happening in
callbacks.js:133, in code that checked whether the return value was an
array, using instanceof Array. When I inspected what this code was
doing at runtime the value really was an array, but instanceof Array
was still returning false. Strangely I could reproduce the original
error when I ran my tests via Jest but not when I ran the same code in
the wd repl (where instanceof Array would return true at the same place).

It turns out that instanceof Array can return false for an array
created in another window or frame (or process?). See
http://web.mit.edu/jwalden/www/isArray.html for a full
explanation of this. Array.isArray() was added to ES5 for exactly this
reason, so I have replaced instanceof Array with Array.isArray()
throughout the codebase.

With this change I wd behaves as I would expect both in the repl and in
Jest.

I've been seeing a bug where at certain points in my tests, code to
retrieve elements like this:

```
driver.elementsByAccessibilityId("foo bar")
```

would throw an error:

```
[elementsByAccessibilityId("foo bar")] Response value field is not an Array.
```

After looking into the for wd code, I could see this was happening in
`callbacks.js:133`, in code that checked whether the return value was an
array, using `instanceof Array`. When I inspected what this code was
doing at runtime the value really was an array, but `instanceof Array`
was still returning false. Strangely I could reproduce the original
error when I ran my tests via Jest but not when I ran the same code in
the wd repl (where `instanceof Array` would return true at the same place).

It turns out that `instanceof Array` can return false for an array
created in another window or frame (or process?). See
http://web.mit.edu/jwalden/www/isArray.html for a full
explanation of this. `Array.isArray()` was added to ES5 for exactly this
reason, so I have replaced `instanceof  Array` with `Array.isArray()`
throughout the codebase.

With this change I wd behaves as I would expect both in the repl and in
Jest.
@sebv sebv merged commit 25c1340 into admc:master Dec 13, 2016
@iainbeeston
Copy link
Contributor Author

Thanks @sebv !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants