-
-
Save jtbandes/039be5d61760aeb0eb12389228e643d1 to your computer and use it in GitHub Desktop.
Playwright Electron onHeadersReceived bug repro for https://github.com/microsoft/playwright/issues/30495
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https://example.com; script-src 'self'; style-src 'self' 'unsafe-inline'"> | |
</head> | |
<body> | |
Demo page | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("node:path"); | |
const electronPath = require("electron"); | |
const { _electron: electron } = require("playwright"); | |
async function main() { | |
const electronApp = await electron.launch({ | |
args: [path.resolve(__dirname, "main.js")], | |
// In node.js the electron import gives us the path to the electron binary | |
executablePath: electronPath, | |
}); | |
electronApp.process().stdout?.on("data", (data) => { | |
console.info(`[main stdout] ${data}`); | |
}); | |
electronApp.process().stderr?.on("data", (data) => { | |
console.info(`[main stderr] ${data}`); | |
}); | |
const electronWindow = await electronApp.firstWindow(); | |
await electronWindow.route("https://example.com/**", async (reqRoute, request) => { | |
console.log(`fulfilling mock request: ${request.url()}\n`); | |
await reqRoute.fulfill({ json: { foo: "bar" } }); | |
}); | |
electronWindow.on("console", (message) => { | |
console.info(`[renderer ${message.type()}]`, message.text()); | |
}); | |
await electronApp.evaluate(({ BrowserWindow }) => { | |
for (const win of BrowserWindow.getAllWindows()) { | |
win.webContents.openDevTools(); | |
} | |
}); | |
} | |
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app, BrowserWindow, session } = require("electron"); | |
function createWindow() { | |
const mainWindow = new BrowserWindow({ width: 800, height: 600 }); | |
mainWindow.webContents.openDevTools(); | |
mainWindow.loadFile("index.html"); | |
} | |
app.whenReady().then(() => { | |
createWindow(); | |
// Comment out this block to fix the request | |
session.defaultSession.webRequest.onHeadersReceived((details, callback) => { | |
const responseHeaders = { | |
...details.responseHeaders, | |
"x-example-header": "test", | |
}; | |
callback({ responseHeaders }); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"devDependencies": { | |
"electron": "30.0.1", | |
"playwright": "1.43.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("hi from renderer"); | |
(async function () { | |
// wait for the .route() mock to be installed | |
await new Promise((resolve) => setTimeout(resolve, 1000)); | |
try { | |
const response = await fetch("https://example.com/foo"); | |
console.log("got response:", response, response.status, response.ok, Object.fromEntries(response.headers.entries())); | |
console.log("json:", await response.json()); | |
} catch (err) { | |
console.log("failed:", err); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment