fix: get the main renderer instead of the first one in testing as there are multiple windows now

This commit is contained in:
George He
2024-01-02 16:07:25 +08:00
parent fbb60f3c37
commit e2a92b17ea
2 changed files with 32 additions and 9 deletions

View File

@@ -3,12 +3,13 @@ import { test } from '../../playwright/test';
test('Clone from github', async ({ page }) => {
await page.getByLabel('Clone git repository').click();
await page.getByRole('tab', { name: ' Git' }).click();
await page.getByPlaceholder('https://github.com/org/repo.git').fill('https://github.com/gatzjames/insomnia-git-example.git');
await page.getByPlaceholder('https://github.com/org/repo.git').fill('https://github.com/ihexxa/insomnia-git-example.git');
await page.getByPlaceholder('Name').fill('J');
await page.getByPlaceholder('Email').fill('J');
await page.getByPlaceholder('MyUser').fill('J');
await page.getByPlaceholder('88e7ee63b254e4b0bf047559eafe86ba9dd49507').fill('J');
await page.getByTestId('git-repository-settings-modal__sync-btn').click();
await page.getByLabel('Toggle preview').click();
});
@@ -24,11 +25,21 @@ test('Sign in with GitHub', async ({ app, page }) => {
// and return the url that would be created by following the GitHub OAuth flow.
// https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#web-application-flow
const fakeGitHubOAuthWebFlow = app.evaluate(electron => {
return new Promise<{ redirectUrl: string }>(resolve => {
const webContents = electron.BrowserWindow.getAllWindows()[0].webContents;
return new Promise<{ redirectUrl: string }>((resolve, reject) => {
const wins = electron.BrowserWindow.getAllWindows();
let mainWin = undefined;
for (let i = 0; i < wins.length; i++) {
mainWin = wins[i].title === 'Insomnia' ? wins[i] : undefined;
}
if (!mainWin) {
reject('main window is not found, probably the title of the mainWindow is modified');
}
const webContents = mainWin?.webContents;
// Remove all navigation listeners so that only the one we inject will run
webContents.removeAllListeners('will-navigate');
webContents.on('will-navigate', (event: Event, url: string) => {
webContents?.removeAllListeners('will-navigate');
webContents?.on('will-navigate', (event: Event, url: string) => {
event.preventDefault();
const parsedUrl = new URL(url);
// We use the same state parameter that the app created to assert that we prevent CSRF

View File

@@ -1,3 +1,5 @@
import { expect } from '@playwright/test';
import { test } from '../../playwright/test';
test('Sign in with Gitlab', async ({ app, page }) => {
@@ -8,11 +10,21 @@ test('Sign in with Gitlab', async ({ app, page }) => {
await page.getByRole('tab', { name: 'GitLab' }).click();
const fakeGitLabOAuthWebFlow = app.evaluate(electron => {
return new Promise<{ redirectUrl: string }>(resolve => {
const webContents = electron.BrowserWindow.getAllWindows()[0].webContents;
return new Promise<{ redirectUrl: string }>((resolve, reject) => {
const wins = electron.BrowserWindow.getAllWindows();
let mainWin = undefined;
for (let i = 0; i < wins.length; i++) {
mainWin = wins[i].title === 'Insomnia' ? wins[i] : undefined;
}
if (!mainWin) {
reject('main window is not found, probably the title of the mainWindow is modified');
}
const webContents = mainWin?.webContents;
// Remove all navigation listeners so that only the one we inject will run
webContents.removeAllListeners('will-navigate');
webContents.on('will-navigate', (event: Event, url: string) => {
webContents?.removeAllListeners('will-navigate');
webContents?.on('will-navigate', (event: Event, url: string) => {
event.preventDefault();
const parsedUrl = new URL(url);
// We use the same state parameter that the app created to assert that we prevent CSRF