v8.34.0
This release adds an experimental ability to use our internal CDP implementation in your tests.
π Improvements
We've added a new command β unstable_getCdp. It lets you send raw CDP commands to the browser as well as prebuilt methods. You can check what domain and CDP methods are implemented in a type-safe manner here: https://github.com/gemini-testing/testplane/tree/master/src/browser/cdp/domains.
The main reason to use this API over Puppeteer is that Puppeteer in practice is proven to have issues with browsers that don't match its expected version range or with android emulators and browsers inside them. You can expect this API to be more stable than that of puppeteer instead of browser compatibility and connection stability.
Warning
This command is unstable for now, so changes in its API are not considered breaking changes by us.
Example usage
describe('CDP', () => {
it('Demo', async ({ browser }) => {
const cdp = await browser.unstable_getCdp();
const {targetInfos} = await cdp.request('Target.getTargets');
const pageTarget = targetInfos.find(t => t.type === 'page');
const { sessionId } = await cdp.request('Target.attachToTarget', {
targetId: pageTarget.targetId,
flatten: true
});
// Enabling Page domain is required for addScriptToEvaluateOnNewDocument
await cdp.request('Page.enable', null, sessionId);
const scriptToInject = `
window.__testValue = Math.random();
`;
await cdp.request('Page.addScriptToEvaluateOnNewDocument', {
source: scriptToInject
}, sessionId);
await browser.url('https://ya.ru');
const result = await browser.execute(() => {
return window.__testValue;
});
console.log('Injected value:', result);
});
});Note that most requests require you to pass cdp session ID.
Also note that CDP API is available only in chromium browsers. Check what is considered a chromium browser here. In practice, this means that if you are using Chrome on Android emulator, you must pass goog:chromeOptions in capabilities, an empty object is sufficient.