toHaveURL.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_utils2 = require("playwright-core/lib/utils");
  26. var import_expect = require("./expect");
  27. var import_matcherHint = require("./matcherHint");
  28. var import_expectBundle = require("../common/expectBundle");
  29. async function toHaveURLWithPredicate(page, expected, options) {
  30. const matcherName = "toHaveURL";
  31. const expression = "page";
  32. const matcherOptions = {
  33. isNot: this.isNot,
  34. promise: this.promise
  35. };
  36. if (typeof expected !== "function") {
  37. throw new Error(
  38. [
  39. // Always display `expected` in expectation place
  40. (0, import_matcherHint.matcherHint)(this, void 0, matcherName, expression, void 0, matcherOptions, void 0, void 0, true),
  41. `${import_utils2.colors.bold("Matcher error")}: ${(0, import_expectBundle.EXPECTED_COLOR)("expected")} value must be a string, regular expression, or predicate`,
  42. this.utils.printWithType("Expected", expected, this.utils.printExpected)
  43. ].join("\n\n")
  44. );
  45. }
  46. const timeout = options?.timeout ?? this.timeout;
  47. const baseURL = page.context()._options.baseURL;
  48. let conditionSucceeded = false;
  49. let lastCheckedURLString = void 0;
  50. try {
  51. await page.mainFrame().waitForURL(
  52. (url) => {
  53. lastCheckedURLString = url.toString();
  54. if (options?.ignoreCase) {
  55. return !this.isNot === (0, import_utils.urlMatches)(
  56. baseURL?.toLocaleLowerCase(),
  57. lastCheckedURLString.toLocaleLowerCase(),
  58. expected
  59. );
  60. }
  61. return !this.isNot === (0, import_utils.urlMatches)(baseURL, lastCheckedURLString, expected);
  62. },
  63. { timeout }
  64. );
  65. conditionSucceeded = true;
  66. } catch (e) {
  67. conditionSucceeded = false;
  68. }
  69. if (conditionSucceeded)
  70. return { name: matcherName, pass: !this.isNot, message: () => "" };
  71. return {
  72. name: matcherName,
  73. pass: this.isNot,
  74. message: () => toHaveURLMessage(
  75. this,
  76. matcherName,
  77. expression,
  78. expected,
  79. lastCheckedURLString,
  80. this.isNot,
  81. true,
  82. timeout
  83. ),
  84. actual: lastCheckedURLString,
  85. timeout
  86. };
  87. }
  88. function toHaveURLMessage(state, matcherName, expression, expected, received, pass, didTimeout, timeout) {
  89. const matcherOptions = {
  90. isNot: state.isNot,
  91. promise: state.promise
  92. };
  93. const receivedString = received || "";
  94. const messagePrefix = (0, import_matcherHint.matcherHint)(state, void 0, matcherName, expression, void 0, matcherOptions, didTimeout ? timeout : void 0, void 0, true);
  95. let printedReceived;
  96. let printedExpected;
  97. let printedDiff;
  98. if (typeof expected === "function") {
  99. printedExpected = `Expected predicate to ${!state.isNot ? "succeed" : "fail"}`;
  100. printedReceived = `Received string: ${(0, import_expectBundle.printReceived)(receivedString)}`;
  101. } else {
  102. if (pass) {
  103. printedExpected = `Expected pattern: not ${state.utils.printExpected(expected)}`;
  104. const formattedReceived = (0, import_expect.printReceivedStringContainExpectedResult)(receivedString, null);
  105. printedReceived = `Received string: ${formattedReceived}`;
  106. } else {
  107. const labelExpected = `Expected ${typeof expected === "string" ? "string" : "pattern"}`;
  108. printedDiff = state.utils.printDiffOrStringify(expected, receivedString, labelExpected, "Received string", false);
  109. }
  110. }
  111. const resultDetails = printedDiff ? printedDiff : printedExpected + "\n" + printedReceived;
  112. return messagePrefix + resultDetails;
  113. }
  114. // Annotate the CommonJS export names for ESM import in node:
  115. 0 && (module.exports = {
  116. toHaveURLWithPredicate
  117. });