Skip to content

Commit

Permalink
Merge pull request #105 from /issues/104-nodelist
Browse files Browse the repository at this point in the history
fix: correct NodeList and HTMLCollection handling in execute script
  • Loading branch information
poftadeh authored Dec 18, 2019
2 parents eea2525 + 49a8045 commit b76747d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Session/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,15 @@ class Session {
this.handleSyncScriptError(error);
}

const { NodeList, HTMLCollection } = window;

if (
vmReturnValue instanceof NodeList ||
vmReturnValue instanceof HTMLCollection
) {
vmReturnValue = Array.from(vmReturnValue);
}

if (Array.isArray(vmReturnValue)) {
return vmReturnValue.map(value =>
value instanceof HTMLElement
Expand Down
25 changes: 19 additions & 6 deletions test/jest/e2e/execute-script-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Execute Script Sync', () => {
</head>
<body>
<input type="text" value='baz' />
<input type="password" />
<p id="foo">bar</p>
<button onclick="clickAction(this)">
not clicked
Expand All @@ -29,8 +30,8 @@ describe('Execute Script Sync', () => {
}
</script>
<script>
function getInputValue() {
return document.querySelector('input').value;
function getTextInputValue() {
return document.querySelector('input[type="text"]').value;
}
function getParagraphElement() {
Expand Down Expand Up @@ -155,11 +156,23 @@ describe('Execute Script Sync', () => {
});

it('handles global functions', async () => {
expect(await executeScript('return getInputValue()')).toBe(
'baz',
expect(await executeScript('return getTextInputValue()')).toBe('baz');
expect(await executeScript('return getParagraphElement();')).toHaveProperty(
[ELEMENT],
expect.any(String),
);
});

it('handles NodeList and HTMLCollection', async () => {
expect(
await executeScript('return document.querySelectorAll("input")'),
).toStrictEqual([
{ [ELEMENT]: expect.any(String) },
{ [ELEMENT]: expect.any(String) },
]);

expect(
await executeScript('return getParagraphElement();'),
).toHaveProperty([ELEMENT], expect.any(String));
await executeScript('return document.getElementsByTagName("body")'),
).toStrictEqual([{ [ELEMENT]: expect.any(String) }]);
});
});

0 comments on commit b76747d

Please sign in to comment.