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

[chore] Apply context isolation and rearrange execution.ts #376

Merged
merged 14 commits into from
Feb 9, 2023
Merged
15 changes: 0 additions & 15 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



---
This product relies on electron-better-ipc

MIT License

Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



---
This product relies on electron-debug

Expand Down
15 changes: 15 additions & 0 deletions common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,18 @@ export type GenerateCodeOptions = {
actions: Steps;
isProject: boolean;
};

export type ActionGeneratedListener = (event: IpcRendererEvent, actions: ActionContext[]) => void;
export type TestEventListener = (event: IpcRendererEvent, data: TestEvent) => void;
export interface IElectronAPI {
exportScript: (string) => Promise<boolean>;
recordJourney: (url: string) => Promise<void>;
stopRecording: () => void;
pauseRecording: () => Promise<void>;
resumeRecording: () => Promise<void>;
addActionGeneratedListener(listener: ActionGeneratedListener): () => void;
generateCode: (params: GenerateCodeOptions) => Promise<string>;
openExternalLink: (url: string) => Promise<void>;
runTest: (params: RunJourneyOptions, listener: TestEventListener) => Promise<void>;
removeOnTestListener: () => void;
}
32 changes: 19 additions & 13 deletions jest.unit.setup.js → electron/api/exportScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { BrowserWindow, dialog, IpcMainInvokeEvent } from 'electron';
import { writeFile } from 'fs/promises';

/**
* We do this because there are numerous places in the frontend code that
* need to call `window.require("electron-better-ipc"). Because the tests
* are not configured to run with node env features, `window.require` will
* not be defined.
*
* We rely on e2e testing to ensure that our communications via IPC to the
* electron process are working, so for the most part we can ignore this
* feature, and mock its responses when testing frontend functionality in
* unit tests. This setup will preclude the need to include the declaration
* of `require` on the `window` object before any test that depends on this.
*/
export async function onExportScript(_event: IpcMainInvokeEvent, code: string) {
const window = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
const { filePath, canceled } = await dialog.showSaveDialog(window, {
filters: [
{
name: 'JavaScript',
extensions: ['js'],
},
],
defaultPath: 'recorded.journey.js',
});

window.require = require;
if (!canceled && filePath) {
await writeFile(filePath, code);
return true;
}
return false;
}
34 changes: 34 additions & 0 deletions electron/api/generateCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
MIT License

Copyright (c) 2021-present, Elastic NV

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { IpcMainInvokeEvent } from 'electron';
import { RecorderSteps } from '../../common/types';
import { SyntheticsGenerator } from '../syntheticsGenerator';

export async function onGenerateCode(
_event: IpcMainInvokeEvent,
data: { isProject: boolean; actions: RecorderSteps }
) {
const generator = new SyntheticsGenerator(data.isProject);
return generator.generateFromSteps(data.actions);
}
29 changes: 29 additions & 0 deletions electron/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
MIT License

Copyright (c) 2021-present, Elastic NV

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
export * from './recordJourney';
export * from './setMode';
export * from './exportScript';
export * from './runJourney';
export * from './openExternalLink';
export * from './generateCode';
33 changes: 33 additions & 0 deletions electron/api/openExternalLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
MIT License

Copyright (c) 2021-present, Elastic NV

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { IpcMainInvokeEvent, shell } from 'electron';
import logger from 'electron-log';

export async function onOpenExternalLink(_event: IpcMainInvokeEvent, url: string) {
try {
await shell.openExternal(url);
} catch (e) {
logger.error(e);
}
}
80 changes: 80 additions & 0 deletions electron/api/recordJourney.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
MIT License

Copyright (c) 2021-present, Elastic NV

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import path from 'path';
import { EventEmitter, once } from 'events';
import { existsSync } from 'fs';
import { BrowserWindow, ipcMain, IpcMainInvokeEvent } from 'electron';
import type { BrowserContext } from 'playwright-core';
import logger from 'electron-log';
import { ActionInContext } from '../../common/types';
import { BrowserManager } from '../browserManager';

export async function recordJourney(
_event: IpcMainInvokeEvent,
url: string,
browserManager: BrowserManager
) {
const browserWindow = BrowserWindow.getFocusedWindow()!;
try {
const { browser, context } = await browserManager.launchBrowser();
const actionListener = new EventEmitter();

ipcMain.handleOnce('stop-recording', async () => {
actionListener.removeListener('actions', actionsHandler);
await browserManager.closeBrowser();
});

// Listen to actions from Playwright recording session
const actionsHandler = (actions: ActionInContext[]) => {
browserWindow.webContents.send('actions-generated', actions);
};
actionListener.on('actions', actionsHandler);

// _enableRecorder is private method, not defined in BrowserContext type
await (context as any)._enableRecorder({
launchOptions: {},
contextOptions: {},
mode: 'recording',
showRecorder: false,
actionListener,
});
await openPage(context, url);
await once(browser, 'disconnected');
} catch (e) {
logger.error(e);
} finally {
ipcMain.removeHandler('stop-recording');
}
}

async function openPage(context: BrowserContext, url: string) {
const page = await context.newPage();
if (url) {
if (existsSync(url)) url = 'file://' + path.resolve(url);
else if (!url.startsWith('http') && !url.startsWith('file://') && !url.startsWith('about:'))
url = 'http://' + url;
await page.goto(url);
}
return page;
}
Loading