Merge pull request #18635 from calixteman/avoid_print_dialog

Don't show the print dialog when printing in some integration tests
This commit is contained in:
Tim van der Meij 2024-08-23 20:08:04 +02:00 committed by GitHub
commit 5f7637b9ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 41 deletions

View file

@ -439,38 +439,40 @@ describe("Interaction", () => {
let pages; let pages;
beforeAll(async () => { beforeAll(async () => {
pages = await loadAndWait("doc_actions.pdf", getSelector("47R")); pages = await loadAndWait("doc_actions.pdf", getSelector("47R"), null, {
earlySetup: () => {
// No need to trigger the print dialog.
window.print = () => {};
},
});
}); });
it("must execute WillPrint and DidPrint actions", async () => { it("must execute WillPrint and DidPrint actions", async () => {
// Run the tests sequentially to avoid to use the same printer at the same await Promise.all(
// time. pages.map(async ([browserName, page]) => {
// And to make sure that a printer isn't locked by a process we close the await waitForScripting(page);
// page before running the next test.
for (const [browserName, page] of pages) {
await waitForScripting(page);
await clearInput(page, getSelector("47R")); await clearInput(page, getSelector("47R"));
await page.evaluate(_ => { await page.evaluate(_ => {
window.document.activeElement.blur(); window.document.activeElement.blur();
}); });
await page.waitForFunction(`${getQuerySelector("47R")}.value === ""`); await page.waitForFunction(`${getQuerySelector("47R")}.value === ""`);
const text = await actAndWaitForInput( const text = await actAndWaitForInput(
page, page,
getSelector("47R"), getSelector("47R"),
async () => { async () => {
await page.click("#print"); await page.click("#print");
} }
); );
expect(text).withContext(`In ${browserName}`).toEqual("WillPrint"); expect(text).withContext(`In ${browserName}`).toEqual("WillPrint");
await page.keyboard.press("Escape");
await page.waitForFunction( await page.waitForFunction(
`${getQuerySelector("50R")}.value === "DidPrint"` `${getQuerySelector("50R")}.value === "DidPrint"`
); );
await closeSinglePage(page); await closeSinglePage(page);
} })
);
}); });
}); });
@ -1742,7 +1744,6 @@ describe("Interaction", () => {
describe("in autoprint.pdf", () => { describe("in autoprint.pdf", () => {
let pages; let pages;
const printHandles = new Map();
beforeAll(async () => { beforeAll(async () => {
// Autoprinting is triggered by the `Open` event, which is one of the // Autoprinting is triggered by the `Open` event, which is one of the
@ -1754,13 +1755,9 @@ describe("Interaction", () => {
// too late will cause it to never resolve because printing is already // too late will cause it to never resolve because printing is already
// done (and the printed page div removed) before we even get to it. // done (and the printed page div removed) before we even get to it.
pages = await loadAndWait("autoprint.pdf", "", null /* zoom = */, { pages = await loadAndWait("autoprint.pdf", "", null /* zoom = */, {
postPageSetup: async page => { earlySetup: () => {
printHandles.set( // No need to trigger the print dialog.
page, window.print = () => {};
page.evaluateHandle(() => [
window.PDFViewerApplication._testPrintResolver.promise,
])
);
}, },
appSetup: app => { appSetup: app => {
app._testPrintResolver = Promise.withResolvers(); app._testPrintResolver = Promise.withResolvers();
@ -1779,7 +1776,6 @@ describe("Interaction", () => {
afterAll(async () => { afterAll(async () => {
await closePages(pages); await closePages(pages);
printHandles.clear();
}); });
it("must check if printing is triggered when the document is open", async () => { it("must check if printing is triggered when the document is open", async () => {
@ -1787,7 +1783,11 @@ describe("Interaction", () => {
pages.map(async ([browserName, page]) => { pages.map(async ([browserName, page]) => {
await waitForScripting(page); await waitForScripting(page);
await awaitPromise(await printHandles.get(page)); await awaitPromise(
await page.evaluateHandle(() => [
window.PDFViewerApplication._testPrintResolver.promise,
])
);
}) })
); );
}); });

View file

@ -60,11 +60,15 @@ function loadAndWait(filename, selector, zoom, setups, options) {
// and EventBus, so we can inject some code to do whatever we want // and EventBus, so we can inject some code to do whatever we want
// soon enough especially before the first event in the eventBus is // soon enough especially before the first event in the eventBus is
// dispatched. // dispatched.
const { prePageSetup, appSetup, eventBusSetup } = setups; const { prePageSetup, appSetup, earlySetup, eventBusSetup } = setups;
await prePageSetup?.(page); await prePageSetup?.(page);
if (appSetup || eventBusSetup) { if (earlySetup || appSetup || eventBusSetup) {
await page.evaluateOnNewDocument( await page.evaluateOnNewDocument(
(aSetup, eSetup) => { (eaSetup, aSetup, evSetup) => {
if (eaSetup) {
// eslint-disable-next-line no-eval
eval(`(${eaSetup})`)();
}
let app; let app;
let eventBus; let eventBus;
Object.defineProperty(window, "PDFViewerApplication", { Object.defineProperty(window, "PDFViewerApplication", {
@ -83,13 +87,16 @@ function loadAndWait(filename, selector, zoom, setups, options) {
}, },
set(newV) { set(newV) {
eventBus = newV; eventBus = newV;
// eslint-disable-next-line no-eval if (evSetup) {
eval(`(${eSetup})`)(eventBus); // eslint-disable-next-line no-eval
eval(`(${evSetup})`)(eventBus);
}
}, },
}); });
}, },
}); });
}, },
earlySetup?.toString(),
appSetup?.toString(), appSetup?.toString(),
eventBusSetup?.toString() eventBusSetup?.toString()
); );