actions.spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /// <reference types="cypress" />
  2. context('Actions', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/commands/actions')
  5. })
  6. // https://on.cypress.io/interacting-with-elements
  7. it('.type() - type into a DOM element', () => {
  8. // https://on.cypress.io/type
  9. cy.get('.action-email')
  10. .type('fake@email.com').should('have.value', 'fake@email.com')
  11. // .type() with special character sequences
  12. .type('{leftarrow}{rightarrow}{uparrow}{downarrow}')
  13. .type('{del}{selectall}{backspace}')
  14. // .type() with key modifiers
  15. .type('{alt}{option}') //these are equivalent
  16. .type('{ctrl}{control}') //these are equivalent
  17. .type('{meta}{command}{cmd}') //these are equivalent
  18. .type('{shift}')
  19. // Delay each keypress by 0.1 sec
  20. .type('slow.typing@email.com', { delay: 100 })
  21. .should('have.value', 'slow.typing@email.com')
  22. cy.get('.action-disabled')
  23. // Ignore error checking prior to type
  24. // like whether the input is visible or disabled
  25. .type('disabled error checking', { force: true })
  26. .should('have.value', 'disabled error checking')
  27. })
  28. it('.focus() - focus on a DOM element', () => {
  29. // https://on.cypress.io/focus
  30. cy.get('.action-focus').focus()
  31. .should('have.class', 'focus')
  32. .prev().should('have.attr', 'style', 'color: orange;')
  33. })
  34. it('.blur() - blur off a DOM element', () => {
  35. // https://on.cypress.io/blur
  36. cy.get('.action-blur').type('About to blur').blur()
  37. .should('have.class', 'error')
  38. .prev().should('have.attr', 'style', 'color: red;')
  39. })
  40. it('.clear() - clears an input or textarea element', () => {
  41. // https://on.cypress.io/clear
  42. cy.get('.action-clear').type('Clear this text')
  43. .should('have.value', 'Clear this text')
  44. .clear()
  45. .should('have.value', '')
  46. })
  47. it('.submit() - submit a form', () => {
  48. // https://on.cypress.io/submit
  49. cy.get('.action-form')
  50. .find('[type="text"]').type('HALFOFF')
  51. cy.get('.action-form').submit()
  52. .next().should('contain', 'Your form has been submitted!')
  53. })
  54. it('.click() - click on a DOM element', () => {
  55. // https://on.cypress.io/click
  56. cy.get('.action-btn').click()
  57. // You can click on 9 specific positions of an element:
  58. // -----------------------------------
  59. // | topLeft top topRight |
  60. // | |
  61. // | |
  62. // | |
  63. // | left center right |
  64. // | |
  65. // | |
  66. // | |
  67. // | bottomLeft bottom bottomRight |
  68. // -----------------------------------
  69. // clicking in the center of the element is the default
  70. cy.get('#action-canvas').click()
  71. cy.get('#action-canvas').click('topLeft')
  72. cy.get('#action-canvas').click('top')
  73. cy.get('#action-canvas').click('topRight')
  74. cy.get('#action-canvas').click('left')
  75. cy.get('#action-canvas').click('right')
  76. cy.get('#action-canvas').click('bottomLeft')
  77. cy.get('#action-canvas').click('bottom')
  78. cy.get('#action-canvas').click('bottomRight')
  79. // .click() accepts an x and y coordinate
  80. // that controls where the click occurs :)
  81. cy.get('#action-canvas')
  82. .click(80, 75) // click 80px on x coord and 75px on y coord
  83. .click(170, 75)
  84. .click(80, 165)
  85. .click(100, 185)
  86. .click(125, 190)
  87. .click(150, 185)
  88. .click(170, 165)
  89. // click multiple elements by passing multiple: true
  90. cy.get('.action-labels>.label').click({ multiple: true })
  91. // Ignore error checking prior to clicking
  92. cy.get('.action-opacity>.btn').click({ force: true })
  93. })
  94. it('.dblclick() - double click on a DOM element', () => {
  95. // https://on.cypress.io/dblclick
  96. // Our app has a listener on 'dblclick' event in our 'scripts.js'
  97. // that hides the div and shows an input on double click
  98. cy.get('.action-div').dblclick().should('not.be.visible')
  99. cy.get('.action-input-hidden').should('be.visible')
  100. })
  101. it('.rightclick() - right click on a DOM element', () => {
  102. // https://on.cypress.io/rightclick
  103. // Our app has a listener on 'contextmenu' event in our 'scripts.js'
  104. // that hides the div and shows an input on right click
  105. cy.get('.rightclick-action-div').rightclick().should('not.be.visible')
  106. cy.get('.rightclick-action-input-hidden').should('be.visible')
  107. })
  108. it('.check() - check a checkbox or radio element', () => {
  109. // https://on.cypress.io/check
  110. // By default, .check() will check all
  111. // matching checkbox or radio elements in succession, one after another
  112. cy.get('.action-checkboxes [type="checkbox"]').not('[disabled]')
  113. .check().should('be.checked')
  114. cy.get('.action-radios [type="radio"]').not('[disabled]')
  115. .check().should('be.checked')
  116. // .check() accepts a value argument
  117. cy.get('.action-radios [type="radio"]')
  118. .check('radio1').should('be.checked')
  119. // .check() accepts an array of values
  120. cy.get('.action-multiple-checkboxes [type="checkbox"]')
  121. .check(['checkbox1', 'checkbox2']).should('be.checked')
  122. // Ignore error checking prior to checking
  123. cy.get('.action-checkboxes [disabled]')
  124. .check({ force: true }).should('be.checked')
  125. cy.get('.action-radios [type="radio"]')
  126. .check('radio3', { force: true }).should('be.checked')
  127. })
  128. it('.uncheck() - uncheck a checkbox element', () => {
  129. // https://on.cypress.io/uncheck
  130. // By default, .uncheck() will uncheck all matching
  131. // checkbox elements in succession, one after another
  132. cy.get('.action-check [type="checkbox"]')
  133. .not('[disabled]')
  134. .uncheck().should('not.be.checked')
  135. // .uncheck() accepts a value argument
  136. cy.get('.action-check [type="checkbox"]')
  137. .check('checkbox1')
  138. .uncheck('checkbox1').should('not.be.checked')
  139. // .uncheck() accepts an array of values
  140. cy.get('.action-check [type="checkbox"]')
  141. .check(['checkbox1', 'checkbox3'])
  142. .uncheck(['checkbox1', 'checkbox3']).should('not.be.checked')
  143. // Ignore error checking prior to unchecking
  144. cy.get('.action-check [disabled]')
  145. .uncheck({ force: true }).should('not.be.checked')
  146. })
  147. it('.select() - select an option in a <select> element', () => {
  148. // https://on.cypress.io/select
  149. // at first, no option should be selected
  150. cy.get('.action-select')
  151. .should('have.value', '--Select a fruit--')
  152. // Select option(s) with matching text content
  153. cy.get('.action-select').select('apples')
  154. // confirm the apples were selected
  155. // note that each value starts with "fr-" in our HTML
  156. cy.get('.action-select').should('have.value', 'fr-apples')
  157. cy.get('.action-select-multiple')
  158. .select(['apples', 'oranges', 'bananas'])
  159. // when getting multiple values, invoke "val" method first
  160. .invoke('val')
  161. .should('deep.equal', ['fr-apples', 'fr-oranges', 'fr-bananas'])
  162. // Select option(s) with matching value
  163. cy.get('.action-select').select('fr-bananas')
  164. // can attach an assertion right away to the element
  165. .should('have.value', 'fr-bananas')
  166. cy.get('.action-select-multiple')
  167. .select(['fr-apples', 'fr-oranges', 'fr-bananas'])
  168. .invoke('val')
  169. .should('deep.equal', ['fr-apples', 'fr-oranges', 'fr-bananas'])
  170. // assert the selected values include oranges
  171. cy.get('.action-select-multiple')
  172. .invoke('val').should('include', 'fr-oranges')
  173. })
  174. it('.scrollIntoView() - scroll an element into view', () => {
  175. // https://on.cypress.io/scrollintoview
  176. // normally all of these buttons are hidden,
  177. // because they're not within
  178. // the viewable area of their parent
  179. // (we need to scroll to see them)
  180. cy.get('#scroll-horizontal button')
  181. .should('not.be.visible')
  182. // scroll the button into view, as if the user had scrolled
  183. cy.get('#scroll-horizontal button').scrollIntoView()
  184. .should('be.visible')
  185. cy.get('#scroll-vertical button')
  186. .should('not.be.visible')
  187. // Cypress handles the scroll direction needed
  188. cy.get('#scroll-vertical button').scrollIntoView()
  189. .should('be.visible')
  190. cy.get('#scroll-both button')
  191. .should('not.be.visible')
  192. // Cypress knows to scroll to the right and down
  193. cy.get('#scroll-both button').scrollIntoView()
  194. .should('be.visible')
  195. })
  196. it('.trigger() - trigger an event on a DOM element', () => {
  197. // https://on.cypress.io/trigger
  198. // To interact with a range input (slider)
  199. // we need to set its value & trigger the
  200. // event to signal it changed
  201. // Here, we invoke jQuery's val() method to set
  202. // the value and trigger the 'change' event
  203. cy.get('.trigger-input-range')
  204. .invoke('val', 25)
  205. .trigger('change')
  206. .get('input[type=range]').siblings('p')
  207. .should('have.text', '25')
  208. })
  209. it('cy.scrollTo() - scroll the window or element to a position', () => {
  210. // https://on.cypress.io/scrollto
  211. // You can scroll to 9 specific positions of an element:
  212. // -----------------------------------
  213. // | topLeft top topRight |
  214. // | |
  215. // | |
  216. // | |
  217. // | left center right |
  218. // | |
  219. // | |
  220. // | |
  221. // | bottomLeft bottom bottomRight |
  222. // -----------------------------------
  223. // if you chain .scrollTo() off of cy, we will
  224. // scroll the entire window
  225. cy.scrollTo('bottom')
  226. cy.get('#scrollable-horizontal').scrollTo('right')
  227. // or you can scroll to a specific coordinate:
  228. // (x axis, y axis) in pixels
  229. cy.get('#scrollable-vertical').scrollTo(250, 250)
  230. // or you can scroll to a specific percentage
  231. // of the (width, height) of the element
  232. cy.get('#scrollable-both').scrollTo('75%', '25%')
  233. // control the easing of the scroll (default is 'swing')
  234. cy.get('#scrollable-vertical').scrollTo('center', { easing: 'linear' })
  235. // control the duration of the scroll (in ms)
  236. cy.get('#scrollable-both').scrollTo('center', { duration: 2000 })
  237. })
  238. })