Skip to content

Commit bb4085b

Browse files
committed
refactor(electron): simplify script handling in execute function
- Updated the `execute` function to pass scripts as-is instead of using `JSON.stringify`, improving handling of various script formats. - Adjusted related test cases to reflect this change, ensuring accurate expectations for string arguments passed to the plugin.
1 parent d7d53e9 commit bb4085b

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

packages/electron-service/src/commands/execute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function execute<ReturnValue, InnerArguments extends unknown[]>(
1414
throw new Error('WDIO browser is not yet initialised');
1515
}
1616

17-
const scriptString = typeof script === 'function' ? script.toString() : JSON.stringify(script);
17+
const scriptString = typeof script === 'function' ? script.toString() : script;
1818

1919
const returnValue = await browser.execute(
2020
function executeWithinElectron(script: string, ...args) {

packages/electron-service/test/commands/execute.spec.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,37 @@ describe('execute Command', () => {
4444

4545
it('should execute a stringified function', async () => {
4646
await execute(globalThis.browser, '() => 1 + 2 + 3');
47-
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), JSON.stringify('() => 1 + 2 + 3'));
48-
expect(globalThis.wdioElectron.execute).toHaveBeenCalledWith(JSON.stringify('() => 1 + 2 + 3'), []);
47+
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), '() => 1 + 2 + 3');
48+
expect(globalThis.wdioElectron.execute).toHaveBeenCalledWith('() => 1 + 2 + 3', []);
4949
});
5050

5151
it('should handle scripts with quotes', async () => {
5252
const scriptWithQuotes = '() => "He said \\"hello\\""';
5353
await execute(globalThis.browser, scriptWithQuotes);
54-
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), JSON.stringify(scriptWithQuotes));
54+
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), scriptWithQuotes);
5555
});
5656

5757
it('should handle scripts with newlines', async () => {
5858
const scriptWithNewlines = '() => "line1\\nline2"';
5959
await execute(globalThis.browser, scriptWithNewlines);
60-
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), JSON.stringify(scriptWithNewlines));
60+
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), scriptWithNewlines);
6161
});
6262

6363
it('should handle scripts with unicode', async () => {
6464
const scriptWithUnicode = '() => "Hello 世界"';
6565
await execute(globalThis.browser, scriptWithUnicode);
66-
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), JSON.stringify(scriptWithUnicode));
66+
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), scriptWithUnicode);
6767
});
6868

6969
it('should handle scripts with backslashes', async () => {
7070
const scriptWithBackslashes = '() => "C:\\\\path\\\\file"';
7171
await execute(globalThis.browser, scriptWithBackslashes);
72-
expect(globalThis.browser.execute).toHaveBeenCalledWith(
73-
expect.any(Function),
74-
JSON.stringify(scriptWithBackslashes),
75-
);
72+
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), scriptWithBackslashes);
7673
});
7774

7875
it('should handle mixed special characters', async () => {
7976
const script = '() => "Test \\n \\t \\u001b and \\\\ backslash"';
8077
await execute(globalThis.browser, script);
81-
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), JSON.stringify(script));
78+
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), script);
8279
});
8380
});

0 commit comments

Comments
 (0)