test_utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. exports.loadAndWait = (filename, selector) =>
  16. Promise.all(
  17. global.integrationSessions.map(async session => {
  18. const page = await session.browser.newPage();
  19. // In order to avoid errors because of checks which depend on
  20. // a locale.
  21. await page.evaluateOnNewDocument(() => {
  22. Object.defineProperty(navigator, "language", {
  23. get() {
  24. return "en-US";
  25. },
  26. });
  27. Object.defineProperty(navigator, "languages", {
  28. get() {
  29. return ["en-US", "en"];
  30. },
  31. });
  32. });
  33. await page.goto(
  34. `${global.integrationBaseUrl}?file=/test/pdfs/${filename}`
  35. );
  36. await page.bringToFront();
  37. await page.waitForSelector(selector, {
  38. timeout: 0,
  39. });
  40. return [session.name, page];
  41. })
  42. );
  43. exports.closePages = pages =>
  44. Promise.all(
  45. pages.map(async ([_, page]) => {
  46. await page.close();
  47. })
  48. );
  49. exports.clearInput = async (page, selector) => {
  50. await page.click(selector);
  51. await page.keyboard.down("Control");
  52. await page.keyboard.press("A");
  53. await page.keyboard.up("Control");
  54. await page.keyboard.press("Backspace");
  55. };
  56. function getSelector(id) {
  57. return `[data-element-id="${id}"]`;
  58. }
  59. exports.getSelector = getSelector;
  60. function getQuerySelector(id) {
  61. return `document.querySelector('${getSelector(id)}')`;
  62. }
  63. exports.getQuerySelector = getQuerySelector;
  64. function getComputedStyleSelector(id) {
  65. return `getComputedStyle(${getQuerySelector(id)})`;
  66. }
  67. exports.getComputedStyleSelector = getComputedStyleSelector;
  68. exports.getEditorSelector = n => `#pdfjs_internal_editor_${n}`;
  69. function getSelectedEditors(page) {
  70. return page.evaluate(() => {
  71. const elements = document.querySelectorAll(".selectedEditor");
  72. const results = [];
  73. for (const { id } of elements) {
  74. results.push(parseInt(id.split("_").at(-1)));
  75. }
  76. results.sort();
  77. return results;
  78. });
  79. }
  80. exports.getSelectedEditors = getSelectedEditors;
  81. async function waitForEvent(page, eventName, timeout = 30000) {
  82. await Promise.race([
  83. // add event listener and wait for event to fire before returning
  84. page.evaluate(name => {
  85. return new Promise(resolve => {
  86. document.addEventListener(name, resolve, { once: true });
  87. });
  88. }, eventName),
  89. page.waitForTimeout(timeout),
  90. ]);
  91. }
  92. exports.waitForEvent = waitForEvent;
  93. const waitForStorageEntries = async (page, nEntries) => {
  94. await page.waitForFunction(
  95. n => window.PDFViewerApplication.pdfDocument.annotationStorage.size === n,
  96. {},
  97. nEntries
  98. );
  99. };
  100. exports.waitForStorageEntries = waitForStorageEntries;
  101. const waitForSelectedEditor = async (page, selector) => {
  102. await page.waitForFunction(
  103. sel => document.querySelector(sel).classList.contains("selectedEditor"),
  104. {},
  105. selector
  106. );
  107. };
  108. exports.waitForSelectedEditor = waitForSelectedEditor;