commands.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // ***********************************************
  2. // This example commands.js shows you how to
  3. // create various custom commands and overwrite
  4. // existing commands.
  5. //
  6. // For more comprehensive examples of custom
  7. // commands please read more here:
  8. // https://on.cypress.io/custom-commands
  9. // ***********************************************
  10. //
  11. import 'cypress-wait-until';
  12. Cypress.Commands.add('randomString', (length) => {
  13. var result = '';
  14. var characters = 'ABCDEFGHIJK LMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  15. var charactersLength = characters.length;
  16. for (var i = 0; i < length; i++) {
  17. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  18. }
  19. return result;
  20. });
  21. /**
  22. * Check the swagger schema:
  23. *
  24. * @param {string} method API Method in swagger doc, "get", "put", "post", "delete"
  25. * @param {integer} code Swagger doc endpoint response code, exactly as defined in swagger doc
  26. * @param {string} path Swagger doc endpoint path, exactly as defined in swagger doc
  27. * @param {*} data The API response data to check against the swagger schema
  28. */
  29. Cypress.Commands.add('validateSwaggerSchema', (method, code, path, data) => {
  30. cy.task('validateSwaggerSchema', {
  31. file: Cypress.env('swaggerBase'),
  32. endpoint: path,
  33. method: method,
  34. statusCode: code,
  35. responseSchema: data,
  36. verbose: true
  37. }).should('equal', null);
  38. });
  39. Cypress.Commands.add('getToken', () => {
  40. // login with existing user
  41. cy.task('backendApiPost', {
  42. path: '/api/tokens',
  43. data: {
  44. identity: '[email protected]',
  45. secret: 'changeme'
  46. }
  47. }).then(res => {
  48. cy.wrap(res.token);
  49. });
  50. });
  51. // TODO: copied from v3, is this usable?
  52. Cypress.Commands.add('waitForCertificateStatus', (token, certID, expected, timeout = 60) => {
  53. cy.log(`Waiting for certificate (${certID}) status (${expected}) timeout (${timeout})`);
  54. cy.waitUntil(() => cy.task('backendApiGet', {
  55. token: token,
  56. path: `/api/certificates/${certID}`
  57. }).then((data) => {
  58. return data.result.status === expected;
  59. }), {
  60. errorMsg: 'Waiting for certificate status failed',
  61. timeout: timeout * 1000,
  62. interval: 5000
  63. });
  64. });