Skip to content

Commit

Permalink
Merge pull request #132 from /issues/107-newline-error
Browse files Browse the repository at this point in the history
fix: correct returning newlines in execute script sync
  • Loading branch information
poftadeh authored Jan 15, 2020
2 parents 023a19f + da01d16 commit 6a77936
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Session/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,12 @@ class Session {
});

const { window } = this.browser.dom;
const scriptWithEscapedNewlines = script
.replace(/\r\n/g, '\\r\\n')
.replace(/\n/g, '\\n');

const func = window
.eval(`(function() {${script}})`)
.eval(`(function() {${scriptWithEscapedNewlines}})`)
.bind(null, ...argumentList);

const vm = new VM({
Expand Down
6 changes: 6 additions & 0 deletions test/jest/e2e/execute-script-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,10 @@ describe('Execute Script Sync', () => {
await executeScript('return document.getElementsByTagName("body")'),
).toStrictEqual([{ [ELEMENT]: expect.any(String) }]);
});

it('handles returning newlines', async () => {
expect(await executeScript('return "\n"')).toBe('\n');
expect(await executeScript('return "\r\n"')).toBe('\r\n');
expect(await executeScript('return "\n\r\n\r\n";')).toBe('\n\r\n\r\n');
});
});

0 comments on commit 6a77936

Please sign in to comment.