commands.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // cypress/support/commands.js
  2. // ***********************************************
  3. // This example commands.js shows you how to
  4. // create various custom commands and overwrite
  5. // existing commands.
  6. //
  7. // For more comprehensive examples of custom
  8. // commands please read more here:
  9. // https://on.cypress.io/custom-commands
  10. // ***********************************************
  11. //
  12. //
  13. // -- This is a parent command --
  14. // Cypress.Commands.add('login', (email, password) => { ... })
  15. //
  16. //
  17. // -- This is a child command --
  18. // Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
  19. //
  20. //
  21. // -- This is a dual command --
  22. // Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
  23. //
  24. //
  25. // -- This will overwrite an existing command --
  26. // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
  27. Cypress.Commands.add('logWithDate', (message, ...args) => {
  28. const timestamp = new Date().toISOString();
  29. const formattedMessage = `[${timestamp}] ${message}`;
  30. // 输出到控制台
  31. console.log(formattedMessage, ...args);
  32. // 也可以输出到 Cypress 的命令日志(可选)
  33. cy.log(formattedMessage);
  34. return null; // Cypress 命令需要返回 null 或 Promise
  35. });
  36. // Cypress.on('window:before:load', (win) => {
  37. // // 每打开/刷新一页都会触发
  38. // cy.then(() => {
  39. // console.log('[全局] 内存 href:', win.location.href);
  40. // const [, stamp] = win.location.href.match(/[?&]stamp=([^&]*)/) || [];
  41. // if (stamp) console.log('[全局] 当前 stamp:', stamp);
  42. // });
  43. // });