toHaveURL.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var toHaveURL_exports = {};
  20. __export(toHaveURL_exports, {
  21. toHaveURLWithPredicate: () => toHaveURLWithPredicate
  22. });
  23. module.exports = __toCommonJS(toHaveURL_exports);
  24. var import_utils = require("playwright-core/lib/utils");
  25. var import_expect = require("./expect");
  26. var import_matcherHint = require("./matcherHint");
  27. var import_expectBundle = require("../common/expectBundle");
  28. async function toHaveURLWithPredicate(page, expected, options) {
  29. const matcherName = "toHaveURL";
  30. const timeout = options?.timeout ?? this.timeout;
  31. const baseURL = page.context()._options.baseURL;
  32. let conditionSucceeded = false;
  33. let lastCheckedURLString = void 0;
  34. try {
  35. await page.mainFrame().waitForURL(
  36. (url) => {
  37. lastCheckedURLString = url.toString();
  38. if (options?.ignoreCase) {
  39. return !this.isNot === (0, import_utils.urlMatches)(
  40. baseURL?.toLocaleLowerCase(),
  41. lastCheckedURLString.toLocaleLowerCase(),
  42. expected
  43. );
  44. }
  45. return !this.isNot === (0, import_utils.urlMatches)(baseURL, lastCheckedURLString, expected);
  46. },
  47. { timeout }
  48. );
  49. conditionSucceeded = true;
  50. } catch (e) {
  51. conditionSucceeded = false;
  52. }
  53. if (conditionSucceeded)
  54. return { name: matcherName, pass: !this.isNot, message: () => "" };
  55. return {
  56. name: matcherName,
  57. pass: this.isNot,
  58. message: () => toHaveURLMessage(
  59. this,
  60. matcherName,
  61. expected,
  62. lastCheckedURLString,
  63. this.isNot,
  64. true,
  65. timeout
  66. ),
  67. actual: lastCheckedURLString,
  68. timeout
  69. };
  70. }
  71. function toHaveURLMessage(state, matcherName, expected, received, pass, timedOut, timeout) {
  72. const receivedString = received || "";
  73. let printedReceived;
  74. let printedExpected;
  75. let printedDiff;
  76. if (typeof expected === "function") {
  77. printedExpected = `Expected: predicate to ${!state.isNot ? "succeed" : "fail"}`;
  78. printedReceived = `Received: ${(0, import_expectBundle.printReceived)(receivedString)}`;
  79. } else {
  80. if (pass) {
  81. printedExpected = `Expected pattern: not ${state.utils.printExpected(expected)}`;
  82. const formattedReceived = (0, import_expect.printReceivedStringContainExpectedResult)(receivedString, null);
  83. printedReceived = `Received string: ${formattedReceived}`;
  84. } else {
  85. const labelExpected = `Expected ${typeof expected === "string" ? "string" : "pattern"}`;
  86. printedDiff = state.utils.printDiffOrStringify(expected, receivedString, labelExpected, "Received string", false);
  87. }
  88. }
  89. return (0, import_matcherHint.formatMatcherMessage)(state, {
  90. matcherName,
  91. expectation: "expected",
  92. timeout,
  93. timedOut,
  94. printedExpected,
  95. printedReceived,
  96. printedDiff
  97. });
  98. }
  99. // Annotate the CommonJS export names for ESM import in node:
  100. 0 && (module.exports = {
  101. toHaveURLWithPredicate
  102. });