cypress_api.spec.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /// <reference types="cypress" />
  2. context('Cypress.Commands', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/cypress-api')
  5. })
  6. // https://on.cypress.io/custom-commands
  7. it('.add() - create a custom command', () => {
  8. Cypress.Commands.add('console', {
  9. prevSubject: true,
  10. }, (subject, method) => {
  11. // the previous subject is automatically received
  12. // and the commands arguments are shifted
  13. // allow us to change the console method used
  14. method = method || 'log'
  15. // log the subject to the console
  16. // @ts-ignore TS7017
  17. console[method]('The subject is', subject)
  18. // whatever we return becomes the new subject
  19. // we don't want to change the subject so
  20. // we return whatever was passed in
  21. return subject
  22. })
  23. // @ts-ignore TS2339
  24. cy.get('button').console('info').then(($button) => {
  25. // subject is still $button
  26. })
  27. })
  28. })
  29. context('Cypress.Cookies', () => {
  30. beforeEach(() => {
  31. cy.visit('https://example.cypress.io/cypress-api')
  32. })
  33. // https://on.cypress.io/cookies
  34. it('.debug() - enable or disable debugging', () => {
  35. Cypress.Cookies.debug(true)
  36. // Cypress will now log in the console when
  37. // cookies are set or cleared
  38. cy.setCookie('fakeCookie', '123ABC')
  39. cy.clearCookie('fakeCookie')
  40. cy.setCookie('fakeCookie', '123ABC')
  41. cy.clearCookie('fakeCookie')
  42. cy.setCookie('fakeCookie', '123ABC')
  43. })
  44. it('.preserveOnce() - preserve cookies by key', () => {
  45. // normally cookies are reset after each test
  46. cy.getCookie('fakeCookie').should('not.be.ok')
  47. // preserving a cookie will not clear it when
  48. // the next test starts
  49. cy.setCookie('lastCookie', '789XYZ')
  50. Cypress.Cookies.preserveOnce('lastCookie')
  51. })
  52. it('.defaults() - set defaults for all cookies', () => {
  53. // now any cookie with the name 'session_id' will
  54. // not be cleared before each new test runs
  55. Cypress.Cookies.defaults({
  56. preserve: 'session_id',
  57. })
  58. })
  59. })
  60. context('Cypress.arch', () => {
  61. beforeEach(() => {
  62. cy.visit('https://example.cypress.io/cypress-api')
  63. })
  64. it('Get CPU architecture name of underlying OS', () => {
  65. // https://on.cypress.io/arch
  66. expect(Cypress.arch).to.exist
  67. })
  68. })
  69. context('Cypress.config()', () => {
  70. beforeEach(() => {
  71. cy.visit('https://example.cypress.io/cypress-api')
  72. })
  73. it('Get and set configuration options', () => {
  74. // https://on.cypress.io/config
  75. let myConfig = Cypress.config()
  76. expect(myConfig).to.have.property('animationDistanceThreshold', 5)
  77. expect(myConfig).to.have.property('baseUrl', null)
  78. expect(myConfig).to.have.property('defaultCommandTimeout', 4000)
  79. expect(myConfig).to.have.property('requestTimeout', 5000)
  80. expect(myConfig).to.have.property('responseTimeout', 30000)
  81. expect(myConfig).to.have.property('viewportHeight', 660)
  82. expect(myConfig).to.have.property('viewportWidth', 1000)
  83. expect(myConfig).to.have.property('pageLoadTimeout', 60000)
  84. expect(myConfig).to.have.property('waitForAnimations', true)
  85. expect(Cypress.config('pageLoadTimeout')).to.eq(60000)
  86. // this will change the config for the rest of your tests!
  87. Cypress.config('pageLoadTimeout', 20000)
  88. expect(Cypress.config('pageLoadTimeout')).to.eq(20000)
  89. Cypress.config('pageLoadTimeout', 60000)
  90. })
  91. })
  92. context('Cypress.dom', () => {
  93. beforeEach(() => {
  94. cy.visit('https://example.cypress.io/cypress-api')
  95. })
  96. // https://on.cypress.io/dom
  97. it('.isHidden() - determine if a DOM element is hidden', () => {
  98. let hiddenP = Cypress.$('.dom-p p.hidden').get(0)
  99. let visibleP = Cypress.$('.dom-p p.visible').get(0)
  100. // our first paragraph has css class 'hidden'
  101. expect(Cypress.dom.isHidden(hiddenP)).to.be.true
  102. expect(Cypress.dom.isHidden(visibleP)).to.be.false
  103. })
  104. })
  105. context('Cypress.env()', () => {
  106. beforeEach(() => {
  107. cy.visit('https://example.cypress.io/cypress-api')
  108. })
  109. // We can set environment variables for highly dynamic values
  110. // https://on.cypress.io/environment-variables
  111. it('Get environment variables', () => {
  112. // https://on.cypress.io/env
  113. // set multiple environment variables
  114. Cypress.env({
  115. host: 'veronica.dev.local',
  116. api_server: 'http://localhost:8888/v1/',
  117. })
  118. // get environment variable
  119. expect(Cypress.env('host')).to.eq('veronica.dev.local')
  120. // set environment variable
  121. Cypress.env('api_server', 'http://localhost:8888/v2/')
  122. expect(Cypress.env('api_server')).to.eq('http://localhost:8888/v2/')
  123. // get all environment variable
  124. expect(Cypress.env()).to.have.property('host', 'veronica.dev.local')
  125. expect(Cypress.env()).to.have.property('api_server', 'http://localhost:8888/v2/')
  126. })
  127. })
  128. context('Cypress.log', () => {
  129. beforeEach(() => {
  130. cy.visit('https://example.cypress.io/cypress-api')
  131. })
  132. it('Control what is printed to the Command Log', () => {
  133. // https://on.cypress.io/cypress-log
  134. })
  135. })
  136. context('Cypress.platform', () => {
  137. beforeEach(() => {
  138. cy.visit('https://example.cypress.io/cypress-api')
  139. })
  140. it('Get underlying OS name', () => {
  141. // https://on.cypress.io/platform
  142. expect(Cypress.platform).to.be.exist
  143. })
  144. })
  145. context('Cypress.version', () => {
  146. beforeEach(() => {
  147. cy.visit('https://example.cypress.io/cypress-api')
  148. })
  149. it('Get current version of Cypress being run', () => {
  150. // https://on.cypress.io/version
  151. expect(Cypress.version).to.be.exist
  152. })
  153. })
  154. context('Cypress.spec', () => {
  155. beforeEach(() => {
  156. cy.visit('https://example.cypress.io/cypress-api')
  157. })
  158. it('Get current spec information', () => {
  159. // https://on.cypress.io/spec
  160. // wrap the object so we can inspect it easily by clicking in the command log
  161. cy.wrap(Cypress.spec).should('include.keys', ['name', 'relative', 'absolute'])
  162. })
  163. })