example_spec.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. //
  2. // **** Kitchen Sink Tests ****
  3. //
  4. // This app was developed to demonstrate
  5. // how to write tests in Cypress utilizing
  6. // all of the available commands
  7. //
  8. // Feel free to modify this spec in your
  9. // own application as a jumping off point
  10. // Please read our "Introduction to Cypress"
  11. // https://on.cypress.io/introduction-to-cypress
  12. /* eslint-disable */
  13. describe('Kitchen Sink', function () {
  14. it('.should() - assert that <title> is correct', function () {
  15. // https://on.cypress.io/visit
  16. cy.visit('https://example.cypress.io')
  17. // Here we've made our first assertion using a '.should()' command.
  18. // An assertion is comprised of a chainer, subject, and optional value.
  19. // https://on.cypress.io/should
  20. // https://on.cypress.io/and
  21. // https://on.cypress.io/title
  22. cy.title().should('include', 'Kitchen Sink')
  23. // ↲ ↲ ↲
  24. // subject chainer value
  25. })
  26. context('Querying', function () {
  27. beforeEach(function () {
  28. // Visiting our app before each test removes any state build up from
  29. // previous tests. Visiting acts as if we closed a tab and opened a fresh one
  30. cy.visit('https://example.cypress.io/commands/querying')
  31. })
  32. // Let's query for some DOM elements and make assertions
  33. // The most commonly used query is 'cy.get()', you can
  34. // think of this like the '$' in jQuery
  35. it('cy.get() - query DOM elements', function () {
  36. // https://on.cypress.io/get
  37. // Get DOM elements by id
  38. cy.get('#query-btn').should('contain', 'Button')
  39. // Get DOM elements by class
  40. cy.get('.query-btn').should('contain', 'Button')
  41. cy.get('#querying .well>button:first').should('contain', 'Button')
  42. // ↲
  43. // Use CSS selectors just like jQuery
  44. })
  45. it('cy.contains() - query DOM elements with matching content', function () {
  46. // https://on.cypress.io/contains
  47. cy.get('.query-list')
  48. .contains('bananas').should('have.class', 'third')
  49. // we can pass a regexp to `.contains()`
  50. cy.get('.query-list')
  51. .contains(/^b\w+/).should('have.class', 'third')
  52. cy.get('.query-list')
  53. .contains('apples').should('have.class', 'first')
  54. // passing a selector to contains will yield the selector containing the text
  55. cy.get('#querying')
  56. .contains('ul', 'oranges').should('have.class', 'query-list')
  57. // `.contains()` will favor input[type='submit'],
  58. // button, a, and label over deeper elements inside them
  59. // this will not yield the <span> inside the button,
  60. // but the <button> itself
  61. cy.get('.query-button')
  62. .contains('Save Form').should('have.class', 'btn')
  63. })
  64. it('.within() - query DOM elements within a specific element', function () {
  65. // https://on.cypress.io/within
  66. cy.get('.query-form').within(function () {
  67. cy.get('input:first').should('have.attr', 'placeholder', 'Email')
  68. cy.get('input:last').should('have.attr', 'placeholder', 'Password')
  69. })
  70. })
  71. it('cy.root() - query the root DOM element', function () {
  72. // https://on.cypress.io/root
  73. // By default, root is the document
  74. cy.root().should('match', 'html')
  75. cy.get('.query-ul').within(function () {
  76. // In this within, the root is now the ul DOM element
  77. cy.root().should('have.class', 'query-ul')
  78. })
  79. })
  80. })
  81. context('Traversal', function () {
  82. beforeEach(function () {
  83. cy.visit('https://example.cypress.io/commands/traversal')
  84. })
  85. // Let's query for some DOM elements and make assertions
  86. it('.children() - get child DOM elements', function () {
  87. // https://on.cypress.io/children
  88. cy.get('.traversal-breadcrumb').children('.active')
  89. .should('contain', 'Data')
  90. })
  91. it('.closest() - get closest ancestor DOM element', function () {
  92. // https://on.cypress.io/closest
  93. cy.get('.traversal-badge').closest('ul')
  94. .should('have.class', 'list-group')
  95. })
  96. it('.eq() - get a DOM element at a specific index', function () {
  97. // https://on.cypress.io/eq
  98. cy.get('.traversal-list>li').eq(1).should('contain', 'siamese')
  99. })
  100. it('.filter() - get DOM elements that match the selector', function () {
  101. // https://on.cypress.io/filter
  102. cy.get('.traversal-nav>li').filter('.active').should('contain', 'About')
  103. })
  104. it('.find() - get descendant DOM elements of the selector', function () {
  105. // https://on.cypress.io/find
  106. cy.get('.traversal-pagination').find('li').find('a')
  107. .should('have.length', 7)
  108. })
  109. it('.first() - get first DOM element', function () {
  110. // https://on.cypress.io/first
  111. cy.get('.traversal-table td').first().should('contain', '1')
  112. })
  113. it('.last() - get last DOM element', function () {
  114. // https://on.cypress.io/last
  115. cy.get('.traversal-buttons .btn').last().should('contain', 'Submit')
  116. })
  117. it('.next() - get next sibling DOM element', function () {
  118. // https://on.cypress.io/next
  119. cy.get('.traversal-ul').contains('apples').next().should('contain', 'oranges')
  120. })
  121. it('.nextAll() - get all next sibling DOM elements', function () {
  122. // https://on.cypress.io/nextall
  123. cy.get('.traversal-next-all').contains('oranges')
  124. .nextAll().should('have.length', 3)
  125. })
  126. it('.nextUntil() - get next sibling DOM elements until next el', function () {
  127. // https://on.cypress.io/nextuntil
  128. cy.get('#veggies').nextUntil('#nuts').should('have.length', 3)
  129. })
  130. it('.not() - remove DOM elements from set of DOM elements', function () {
  131. // https://on.cypress.io/not
  132. cy.get('.traversal-disabled .btn').not('[disabled]').should('not.contain', 'Disabled')
  133. })
  134. it('.parent() - get parent DOM element from DOM elements', function () {
  135. // https://on.cypress.io/parent
  136. cy.get('.traversal-mark').parent().should('contain', 'Morbi leo risus')
  137. })
  138. it('.parents() - get parent DOM elements from DOM elements', function () {
  139. // https://on.cypress.io/parents
  140. cy.get('.traversal-cite').parents().should('match', 'blockquote')
  141. })
  142. it('.parentsUntil() - get parent DOM elements from DOM elements until el', function () {
  143. // https://on.cypress.io/parentsuntil
  144. cy.get('.clothes-nav').find('.active').parentsUntil('.clothes-nav')
  145. .should('have.length', 2)
  146. })
  147. it('.prev() - get previous sibling DOM element', function () {
  148. // https://on.cypress.io/prev
  149. cy.get('.birds').find('.active').prev().should('contain', 'Lorikeets')
  150. })
  151. it('.prevAll() - get all previous sibling DOM elements', function () {
  152. // https://on.cypress.io/prevAll
  153. cy.get('.fruits-list').find('.third').prevAll().should('have.length', 2)
  154. })
  155. it('.prevUntil() - get all previous sibling DOM elements until el', function () {
  156. // https://on.cypress.io/prevUntil
  157. cy.get('.foods-list').find('#nuts').prevUntil('#veggies')
  158. })
  159. it('.siblings() - get all sibling DOM elements', function () {
  160. // https://on.cypress.io/siblings
  161. cy.get('.traversal-pills .active').siblings().should('have.length', 2)
  162. })
  163. })
  164. context('Actions', function () {
  165. beforeEach(function () {
  166. cy.visit('https://example.cypress.io/commands/actions')
  167. })
  168. // Let's perform some actions on DOM elements
  169. // https://on.cypress.io/interacting-with-elements
  170. it('.type() - type into a DOM element', function () {
  171. // https://on.cypress.io/type
  172. cy.get('.action-email')
  173. .type('fake@email.com').should('have.value', 'fake@email.com')
  174. // .type() with special character sequences
  175. .type('{leftarrow}{rightarrow}{uparrow}{downarrow}')
  176. .type('{del}{selectall}{backspace}')
  177. // .type() with key modifiers
  178. .type('{alt}{option}') //these are equivalent
  179. .type('{ctrl}{control}') //these are equivalent
  180. .type('{meta}{command}{cmd}') //these are equivalent
  181. .type('{shift}')
  182. // Delay each keypress by 0.1 sec
  183. .type('slow.typing@email.com', { delay: 100 })
  184. .should('have.value', 'slow.typing@email.com')
  185. cy.get('.action-disabled')
  186. // Ignore error checking prior to type
  187. // like whether the input is visible or disabled
  188. .type('disabled error checking', { force: true })
  189. .should('have.value', 'disabled error checking')
  190. })
  191. it('.focus() - focus on a DOM element', function () {
  192. // https://on.cypress.io/focus
  193. cy.get('.action-focus').focus()
  194. .should('have.class', 'focus')
  195. .prev().should('have.attr', 'style', 'color: orange;')
  196. })
  197. it('.blur() - blur off a DOM element', function () {
  198. // https://on.cypress.io/blur
  199. cy.get('.action-blur').type('I\'m about to blur').blur()
  200. .should('have.class', 'error')
  201. .prev().should('have.attr', 'style', 'color: red;')
  202. })
  203. it('.clear() - clears an input or textarea element', function () {
  204. // https://on.cypress.io/clear
  205. cy.get('.action-clear').type('We are going to clear this text')
  206. .should('have.value', 'We are going to clear this text')
  207. .clear()
  208. .should('have.value', '')
  209. })
  210. it('.submit() - submit a form', function () {
  211. // https://on.cypress.io/submit
  212. cy.get('.action-form')
  213. .find('[type="text"]').type('HALFOFF')
  214. cy.get('.action-form').submit()
  215. .next().should('contain', 'Your form has been submitted!')
  216. })
  217. it('.click() - click on a DOM element', function () {
  218. // https://on.cypress.io/click
  219. cy.get('.action-btn').click()
  220. // You can click on 9 specific positions of an element:
  221. // -----------------------------------
  222. // | topLeft top topRight |
  223. // | |
  224. // | |
  225. // | |
  226. // | left center right |
  227. // | |
  228. // | |
  229. // | |
  230. // | bottomLeft bottom bottomRight |
  231. // -----------------------------------
  232. // clicking in the center of the element is the default
  233. cy.get('#action-canvas').click()
  234. cy.get('#action-canvas').click('topLeft')
  235. cy.get('#action-canvas').click('top')
  236. cy.get('#action-canvas').click('topRight')
  237. cy.get('#action-canvas').click('left')
  238. cy.get('#action-canvas').click('right')
  239. cy.get('#action-canvas').click('bottomLeft')
  240. cy.get('#action-canvas').click('bottom')
  241. cy.get('#action-canvas').click('bottomRight')
  242. // .click() accepts an x and y coordinate
  243. // that controls where the click occurs :)
  244. cy.get('#action-canvas')
  245. .click(80, 75) // click 80px on x coord and 75px on y coord
  246. .click(170, 75)
  247. .click(80, 165)
  248. .click(100, 185)
  249. .click(125, 190)
  250. .click(150, 185)
  251. .click(170, 165)
  252. // click multiple elements by passing multiple: true
  253. cy.get('.action-labels>.label').click({ multiple: true })
  254. // Ignore error checking prior to clicking
  255. // like whether the element is visible, clickable or disabled
  256. // this button below is covered by another element.
  257. cy.get('.action-opacity>.btn').click({ force: true })
  258. })
  259. it('.dblclick() - double click on a DOM element', function () {
  260. // Our app has a listener on 'dblclick' event in our 'scripts.js'
  261. // that hides the div and shows an input on double click
  262. // https://on.cypress.io/dblclick
  263. cy.get('.action-div').dblclick().should('not.be.visible')
  264. cy.get('.action-input-hidden').should('be.visible')
  265. })
  266. it('cy.check() - check a checkbox or radio element', function () {
  267. // By default, .check() will check all
  268. // matching checkbox or radio elements in succession, one after another
  269. // https://on.cypress.io/check
  270. cy.get('.action-checkboxes [type="checkbox"]').not('[disabled]')
  271. .check().should('be.checked')
  272. cy.get('.action-radios [type="radio"]').not('[disabled]')
  273. .check().should('be.checked')
  274. // .check() accepts a value argument
  275. // that checks only checkboxes or radios
  276. // with matching values
  277. cy.get('.action-radios [type="radio"]').check('radio1').should('be.checked')
  278. // .check() accepts an array of values
  279. // that checks only checkboxes or radios
  280. // with matching values
  281. cy.get('.action-multiple-checkboxes [type="checkbox"]')
  282. .check(['checkbox1', 'checkbox2']).should('be.checked')
  283. // Ignore error checking prior to checking
  284. // like whether the element is visible, clickable or disabled
  285. // this checkbox below is disabled.
  286. cy.get('.action-checkboxes [disabled]')
  287. .check({ force: true }).should('be.checked')
  288. cy.get('.action-radios [type="radio"]')
  289. .check('radio3', { force: true }).should('be.checked')
  290. })
  291. it('.uncheck() - uncheck a checkbox element', function () {
  292. // By default, .uncheck() will uncheck all matching
  293. // checkbox elements in succession, one after another
  294. // https://on.cypress.io/uncheck
  295. cy.get('.action-check [type="checkbox"]')
  296. .not('[disabled]')
  297. .uncheck().should('not.be.checked')
  298. // .uncheck() accepts a value argument
  299. // that unchecks only checkboxes
  300. // with matching values
  301. cy.get('.action-check [type="checkbox"]')
  302. .check('checkbox1')
  303. .uncheck('checkbox1').should('not.be.checked')
  304. // .uncheck() accepts an array of values
  305. // that unchecks only checkboxes or radios
  306. // with matching values
  307. cy.get('.action-check [type="checkbox"]')
  308. .check(['checkbox1', 'checkbox3'])
  309. .uncheck(['checkbox1', 'checkbox3']).should('not.be.checked')
  310. // Ignore error checking prior to unchecking
  311. // like whether the element is visible, clickable or disabled
  312. // this checkbox below is disabled.
  313. cy.get('.action-check [disabled]')
  314. .uncheck({ force: true }).should('not.be.checked')
  315. })
  316. it('.select() - select an option in a <select> element', function () {
  317. // https://on.cypress.io/select
  318. // Select option with matching text content
  319. cy.get('.action-select').select('apples')
  320. // Select option with matching value
  321. cy.get('.action-select').select('fr-bananas')
  322. // Select options with matching text content
  323. cy.get('.action-select-multiple')
  324. .select(['apples', 'oranges', 'bananas'])
  325. // Select options with matching values
  326. cy.get('.action-select-multiple')
  327. .select(['fr-apples', 'fr-oranges', 'fr-bananas'])
  328. })
  329. it('.scrollIntoView() - scroll an element into view', function () {
  330. // https://on.cypress.io/scrollintoview
  331. // normally all of these buttons are hidden, because they're not within
  332. // the viewable area of their parent (we need to scroll to see them)
  333. cy.get('#scroll-horizontal button')
  334. .should('not.be.visible')
  335. // scroll the button into view, as if the user had scrolled
  336. cy.get('#scroll-horizontal button').scrollIntoView()
  337. .should('be.visible')
  338. cy.get('#scroll-vertical button')
  339. .should('not.be.visible')
  340. // Cypress handles the scroll direction needed
  341. cy.get('#scroll-vertical button').scrollIntoView()
  342. .should('be.visible')
  343. cy.get('#scroll-both button')
  344. .should('not.be.visible')
  345. // Cypress knows to scroll to the right and down
  346. cy.get('#scroll-both button').scrollIntoView()
  347. .should('be.visible')
  348. })
  349. it('cy.scrollTo() - scroll the window or element to a position', function () {
  350. // https://on.cypress.io/scrollTo
  351. // You can scroll to 9 specific positions of an element:
  352. // -----------------------------------
  353. // | topLeft top topRight |
  354. // | |
  355. // | |
  356. // | |
  357. // | left center right |
  358. // | |
  359. // | |
  360. // | |
  361. // | bottomLeft bottom bottomRight |
  362. // -----------------------------------
  363. // if you chain .scrollTo() off of cy, we will
  364. // scroll the entire window
  365. cy.scrollTo('bottom')
  366. cy.get('#scrollable-horizontal').scrollTo('right')
  367. // or you can scroll to a specific coordinate:
  368. // (x axis, y axis) in pixels
  369. cy.get('#scrollable-vertical').scrollTo(250, 250)
  370. // or you can scroll to a specific percentage
  371. // of the (width, height) of the element
  372. cy.get('#scrollable-both').scrollTo('75%', '25%')
  373. // control the easing of the scroll (default is 'swing')
  374. cy.get('#scrollable-vertical').scrollTo('center', { easing: 'linear' })
  375. // control the duration of the scroll (in ms)
  376. cy.get('#scrollable-both').scrollTo('center', { duration: 2000 })
  377. })
  378. it('.trigger() - trigger an event on a DOM element', function () {
  379. // To interact with a range input (slider), we need to set its value and
  380. // then trigger the appropriate event to signal it has changed
  381. // Here, we invoke jQuery's val() method to set the value
  382. // and trigger the 'change' event
  383. // Note that some implementations may rely on the 'input' event,
  384. // which is fired as a user moves the slider, but is not supported
  385. // by some browsers
  386. // https://on.cypress.io/trigger
  387. cy.get('.trigger-input-range')
  388. .invoke('val', 25)
  389. .trigger('change')
  390. .get('input[type=range]').siblings('p')
  391. .should('have.text', '25')
  392. // See our example recipes for more examples of using trigger
  393. // https://on.cypress.io/examples
  394. })
  395. })
  396. context('Window', function () {
  397. beforeEach(function () {
  398. cy.visit('https://example.cypress.io/commands/window')
  399. })
  400. it('cy.window() - get the global window object', function () {
  401. // https://on.cypress.io/window
  402. cy.window().should('have.property', 'top')
  403. })
  404. it('cy.document() - get the document object', function () {
  405. // https://on.cypress.io/document
  406. cy.document().should('have.property', 'charset').and('eq', 'UTF-8')
  407. })
  408. it('cy.title() - get the title', function () {
  409. // https://on.cypress.io/title
  410. cy.title().should('include', 'Kitchen Sink')
  411. })
  412. })
  413. context('Viewport', function () {
  414. beforeEach(function () {
  415. cy.visit('https://example.cypress.io/commands/viewport')
  416. })
  417. it('cy.viewport() - set the viewport size and dimension', function () {
  418. cy.get('#navbar').should('be.visible')
  419. // https://on.cypress.io/viewport
  420. cy.viewport(320, 480)
  421. // the navbar should have collapse since our screen is smaller
  422. cy.get('#navbar').should('not.be.visible')
  423. cy.get('.navbar-toggle').should('be.visible').click()
  424. cy.get('.nav').find('a').should('be.visible')
  425. // lets see what our app looks like on a super large screen
  426. cy.viewport(2999, 2999)
  427. // cy.viewport() accepts a set of preset sizes
  428. // to easily set the screen to a device's width and height
  429. // We added a cy.wait() between each viewport change so you can see
  430. // the change otherwise it's a little too fast to see :)
  431. cy.viewport('macbook-15')
  432. cy.wait(200)
  433. cy.viewport('macbook-13')
  434. cy.wait(200)
  435. cy.viewport('macbook-11')
  436. cy.wait(200)
  437. cy.viewport('ipad-2')
  438. cy.wait(200)
  439. cy.viewport('ipad-mini')
  440. cy.wait(200)
  441. cy.viewport('iphone-6+')
  442. cy.wait(200)
  443. cy.viewport('iphone-6')
  444. cy.wait(200)
  445. cy.viewport('iphone-5')
  446. cy.wait(200)
  447. cy.viewport('iphone-4')
  448. cy.wait(200)
  449. cy.viewport('iphone-3')
  450. cy.wait(200)
  451. // cy.viewport() accepts an orientation for all presets
  452. // the default orientation is 'portrait'
  453. cy.viewport('ipad-2', 'portrait')
  454. cy.wait(200)
  455. cy.viewport('iphone-4', 'landscape')
  456. cy.wait(200)
  457. // The viewport will be reset back to the default dimensions
  458. // in between tests (the default is set in cypress.json)
  459. })
  460. })
  461. context('Location', function () {
  462. beforeEach(function () {
  463. cy.visit('https://example.cypress.io/commands/location')
  464. })
  465. // We look at the url to make assertions
  466. // about the page's state
  467. it('cy.hash() - get the current URL hash', function () {
  468. // https://on.cypress.io/hash
  469. cy.hash().should('be.empty')
  470. })
  471. it('cy.location() - get window.location', function () {
  472. // https://on.cypress.io/location
  473. cy.location().should(function (location) {
  474. expect(location.hash).to.be.empty
  475. expect(location.href).to.eq('https://example.cypress.io/commands/location')
  476. expect(location.host).to.eq('example.cypress.io')
  477. expect(location.hostname).to.eq('example.cypress.io')
  478. expect(location.origin).to.eq('https://example.cypress.io')
  479. expect(location.pathname).to.eq('/commands/location')
  480. expect(location.port).to.eq('')
  481. expect(location.protocol).to.eq('https:')
  482. expect(location.search).to.be.empty
  483. })
  484. })
  485. it('cy.url() - get the current URL', function () {
  486. // https://on.cypress.io/url
  487. cy.url().should('eq', 'https://example.cypress.io/commands/location')
  488. })
  489. })
  490. context('Navigation', function () {
  491. beforeEach(function () {
  492. cy.visit('https://example.cypress.io')
  493. cy.get('.navbar-nav').contains('Commands').click()
  494. cy.get('.dropdown-menu').contains('Navigation').click()
  495. })
  496. it('cy.go() - go back or forward in the browser\'s history', function () {
  497. cy.location('pathname').should('include', 'navigation')
  498. // https://on.cypress.io/go
  499. cy.go('back')
  500. cy.location('pathname').should('not.include', 'navigation')
  501. cy.go('forward')
  502. cy.location('pathname').should('include', 'navigation')
  503. // equivalent to clicking back
  504. cy.go(-1)
  505. cy.location('pathname').should('not.include', 'navigation')
  506. // equivalent to clicking forward
  507. cy.go(1)
  508. cy.location('pathname').should('include', 'navigation')
  509. })
  510. it('cy.reload() - reload the page', function () {
  511. // https://on.cypress.io/reload
  512. cy.reload()
  513. // reload the page without using the cache
  514. cy.reload(true)
  515. })
  516. it('cy.visit() - visit a remote url', function () {
  517. // Visit any sub-domain of your current domain
  518. // https://on.cypress.io/visit
  519. // Pass options to the visit
  520. cy.visit('https://example.cypress.io/commands/navigation', {
  521. timeout: 50000, // increase total time for the visit to resolve
  522. onBeforeLoad (contentWindow) {
  523. // contentWindow is the remote page's window object
  524. },
  525. onLoad (contentWindow) {
  526. // contentWindow is the remote page's window object
  527. },
  528. })
  529. })
  530. })
  531. context('Assertions', function () {
  532. beforeEach(function () {
  533. cy.visit('https://example.cypress.io/commands/assertions')
  534. })
  535. describe('Implicit Assertions', function () {
  536. it('.should() - make an assertion about the current subject', function () {
  537. // https://on.cypress.io/should
  538. cy.get('.assertion-table')
  539. .find('tbody tr:last').should('have.class', 'success')
  540. })
  541. it('.and() - chain multiple assertions together', function () {
  542. // https://on.cypress.io/and
  543. cy.get('.assertions-link')
  544. .should('have.class', 'active')
  545. .and('have.attr', 'href')
  546. .and('include', 'cypress.io')
  547. })
  548. })
  549. describe('Explicit Assertions', function () {
  550. // https://on.cypress.io/assertions
  551. it('expect - assert shape of an object', function () {
  552. const person = {
  553. name: 'Joe',
  554. age: 20,
  555. }
  556. expect(person).to.have.all.keys('name', 'age')
  557. })
  558. it('expect - make an assertion about a specified subject', function () {
  559. // We can use Chai's BDD style assertions
  560. expect(true).to.be.true
  561. // Pass a function to should that can have any number
  562. // of explicit assertions within it.
  563. cy.get('.assertions-p').find('p')
  564. .should(function ($p) {
  565. // return an array of texts from all of the p's
  566. let texts = $p.map(function (i, el) {
  567. // https://on.cypress.io/$
  568. return Cypress.$(el).text()
  569. })
  570. // jquery map returns jquery object
  571. // and .get() convert this to simple array
  572. texts = texts.get()
  573. // array should have length of 3
  574. expect(texts).to.have.length(3)
  575. // set this specific subject
  576. expect(texts).to.deep.eq([
  577. 'Some text from first p',
  578. 'More text from second p',
  579. 'And even more text from third p',
  580. ])
  581. })
  582. })
  583. })
  584. })
  585. context('Misc', function () {
  586. beforeEach(function () {
  587. cy.visit('https://example.cypress.io/commands/misc')
  588. })
  589. it('.end() - end the command chain', function () {
  590. // cy.end is useful when you want to end a chain of commands
  591. // and force Cypress to re-query from the root element
  592. // https://on.cypress.io/end
  593. cy.get('.misc-table').within(function () {
  594. // ends the current chain and yields null
  595. cy.contains('Cheryl').click().end()
  596. // queries the entire table again
  597. cy.contains('Charles').click()
  598. })
  599. })
  600. it('cy.exec() - execute a system command', function () {
  601. // cy.exec allows you to execute a system command.
  602. // so you can take actions necessary for your test,
  603. // but outside the scope of Cypress.
  604. // https://on.cypress.io/exec
  605. cy.exec('echo Jane Lane')
  606. .its('stdout').should('contain', 'Jane Lane')
  607. // we can use Cypress.platform string to
  608. // select appropriate command
  609. // https://on.cypress/io/platform
  610. cy.log(`Platform ${Cypress.platform} architecture ${Cypress.arch}`)
  611. if (Cypress.platform === 'win32') {
  612. cy.exec('print cypress.json')
  613. .its('stderr').should('be.empty')
  614. } else {
  615. cy.exec('cat cypress.json')
  616. .its('stderr').should('be.empty')
  617. cy.exec('pwd')
  618. .its('code').should('eq', 0)
  619. }
  620. })
  621. it('cy.focused() - get the DOM element that has focus', function () {
  622. // https://on.cypress.io/focused
  623. cy.get('.misc-form').find('#name').click()
  624. cy.focused().should('have.id', 'name')
  625. cy.get('.misc-form').find('#description').click()
  626. cy.focused().should('have.id', 'description')
  627. })
  628. it('cy.screenshot() - take a screenshot', function () {
  629. // https://on.cypress.io/screenshot
  630. cy.screenshot('my-image')
  631. })
  632. it('cy.wrap() - wrap an object', function () {
  633. // https://on.cypress.io/wrap
  634. cy.wrap({ foo: 'bar' })
  635. .should('have.property', 'foo')
  636. .and('include', 'bar')
  637. })
  638. })
  639. context('Connectors', function () {
  640. beforeEach(function () {
  641. cy.visit('https://example.cypress.io/commands/connectors')
  642. })
  643. it('.each() - iterate over an array of elements', function () {
  644. // https://on.cypress.io/each
  645. cy.get('.connectors-each-ul>li')
  646. .each(function ($el, index, $list) {
  647. console.log($el, index, $list)
  648. })
  649. })
  650. it('.its() - get properties on the current subject', function () {
  651. // https://on.cypress.io/its
  652. cy.get('.connectors-its-ul>li')
  653. // calls the 'length' property yielding that value
  654. .its('length')
  655. .should('be.gt', 2)
  656. })
  657. it('.invoke() - invoke a function on the current subject', function () {
  658. // our div is hidden in our script.js
  659. // $('.connectors-div').hide()
  660. // https://on.cypress.io/invoke
  661. cy.get('.connectors-div').should('be.hidden')
  662. // call the jquery method 'show' on the 'div.container'
  663. .invoke('show')
  664. .should('be.visible')
  665. })
  666. it('.spread() - spread an array as individual args to callback function', function () {
  667. // https://on.cypress.io/spread
  668. let arr = ['foo', 'bar', 'baz']
  669. cy.wrap(arr).spread(function (foo, bar, baz) {
  670. expect(foo).to.eq('foo')
  671. expect(bar).to.eq('bar')
  672. expect(baz).to.eq('baz')
  673. })
  674. })
  675. it('.then() - invoke a callback function with the current subject', function () {
  676. // https://on.cypress.io/then
  677. cy.get('.connectors-list>li').then(function ($lis) {
  678. expect($lis).to.have.length(3)
  679. expect($lis.eq(0)).to.contain('Walk the dog')
  680. expect($lis.eq(1)).to.contain('Feed the cat')
  681. expect($lis.eq(2)).to.contain('Write JavaScript')
  682. })
  683. })
  684. })
  685. context('Aliasing', function () {
  686. beforeEach(function () {
  687. cy.visit('https://example.cypress.io/commands/aliasing')
  688. })
  689. // We alias a DOM element for use later
  690. // We don't have to traverse to the element
  691. // later in our code, we just reference it with @
  692. it('.as() - alias a route or DOM element for later use', function () {
  693. // this is a good use case for an alias,
  694. // we don't want to write this long traversal again
  695. // https://on.cypress.io/as
  696. cy.get('.as-table').find('tbody>tr')
  697. .first().find('td').first().find('button').as('firstBtn')
  698. // maybe do some more testing here...
  699. // when we reference the alias, we place an
  700. // @ in front of it's name
  701. cy.get('@firstBtn').click()
  702. cy.get('@firstBtn')
  703. .should('have.class', 'btn-success')
  704. .and('contain', 'Changed')
  705. })
  706. })
  707. context('Waiting', function () {
  708. beforeEach(function () {
  709. cy.visit('https://example.cypress.io/commands/waiting')
  710. })
  711. // BE CAREFUL of adding unnecessary wait times.
  712. // https://on.cypress.io/wait
  713. it('cy.wait() - wait for a specific amount of time', function () {
  714. cy.get('.wait-input1').type('Wait 1000ms after typing')
  715. cy.wait(1000)
  716. cy.get('.wait-input2').type('Wait 1000ms after typing')
  717. cy.wait(1000)
  718. cy.get('.wait-input3').type('Wait 1000ms after typing')
  719. cy.wait(1000)
  720. })
  721. // Waiting for a specific resource to resolve
  722. // is covered within the cy.route() test below
  723. })
  724. context('Network Requests', function () {
  725. beforeEach(function () {
  726. cy.visit('https://example.cypress.io/commands/network-requests')
  727. })
  728. // Manage AJAX / XHR requests in your app
  729. it('cy.server() - control behavior of network requests and responses', function () {
  730. // https://on.cypress.io/server
  731. cy.server().should(function (server) {
  732. // the default options on server
  733. // you can override any of these options
  734. expect(server.delay).to.eq(0)
  735. expect(server.method).to.eq('GET')
  736. expect(server.status).to.eq(200)
  737. expect(server.headers).to.be.null
  738. expect(server.response).to.be.null
  739. expect(server.onRequest).to.be.undefined
  740. expect(server.onResponse).to.be.undefined
  741. expect(server.onAbort).to.be.undefined
  742. // These options control the server behavior
  743. // affecting all requests
  744. // pass false to disable existing route stubs
  745. expect(server.enable).to.be.true
  746. // forces requests that don't match your routes to 404
  747. expect(server.force404).to.be.false
  748. // whitelists requests from ever being logged or stubbed
  749. expect(server.whitelist).to.be.a('function')
  750. })
  751. cy.server({
  752. method: 'POST',
  753. delay: 1000,
  754. status: 422,
  755. response: {},
  756. })
  757. // any route commands will now inherit the above options
  758. // from the server. anything we pass specifically
  759. // to route will override the defaults though.
  760. })
  761. it('cy.request() - make an XHR request', function () {
  762. // https://on.cypress.io/request
  763. cy.request('https://jsonplaceholder.typicode.com/comments')
  764. .should(function (response) {
  765. expect(response.status).to.eq(200)
  766. expect(response.body).to.have.length(500)
  767. expect(response).to.have.property('headers')
  768. expect(response).to.have.property('duration')
  769. })
  770. })
  771. it('cy.route() - route responses to matching requests', function () {
  772. let message = 'whoa, this comment doesn\'t exist'
  773. cy.server()
  774. // **** GET comments route ****
  775. // https://on.cypress.io/route
  776. cy.route(/comments\/1/).as('getComment')
  777. // we have code that fetches a comment when
  778. // the button is clicked in scripts.js
  779. cy.get('.network-btn').click()
  780. // **** Wait ****
  781. // Wait for a specific resource to resolve
  782. // continuing to the next command
  783. // https://on.cypress.io/wait
  784. cy.wait('@getComment').its('status').should('eq', 200)
  785. // **** POST comment route ****
  786. // Specify the route to listen to method 'POST'
  787. cy.route('POST', '/comments').as('postComment')
  788. // we have code that posts a comment when
  789. // the button is clicked in scripts.js
  790. cy.get('.network-post').click()
  791. cy.wait('@postComment')
  792. // get the route
  793. cy.get('@postComment').then(function (xhr) {
  794. expect(xhr.requestBody).to.include('email')
  795. expect(xhr.requestHeaders).to.have.property('Content-Type')
  796. expect(xhr.responseBody).to.have.property('name', 'Using POST in cy.route()')
  797. })
  798. // **** Stubbed PUT comment route ****
  799. cy.route({
  800. method: 'PUT',
  801. url: /comments\/\d+/,
  802. status: 404,
  803. response: { error: message },
  804. delay: 500,
  805. }).as('putComment')
  806. // we have code that puts a comment when
  807. // the button is clicked in scripts.js
  808. cy.get('.network-put').click()
  809. cy.wait('@putComment')
  810. // our 404 statusCode logic in scripts.js executed
  811. cy.get('.network-put-comment').should('contain', message)
  812. })
  813. })
  814. context('Files', function () {
  815. beforeEach(function () {
  816. cy.visit('https://example.cypress.io/commands/files')
  817. })
  818. it('cy.fixture() - load a fixture', function () {
  819. // Instead of writing a response inline you can
  820. // connect a response with a fixture file
  821. // located in fixtures folder.
  822. cy.server()
  823. // https://on.cypress.io/fixture
  824. cy.fixture('example.json').as('comment')
  825. cy.route(/comments/, '@comment').as('getComment')
  826. // we have code that gets a comment when
  827. // the button is clicked in scripts.js
  828. cy.get('.fixture-btn').click()
  829. cy.wait('@getComment').its('responseBody')
  830. .should('have.property', 'name')
  831. .and('include', 'Using fixtures to represent data')
  832. // you can also just write the fixture in the route
  833. cy.route(/comments/, 'fixture:example.json').as('getComment')
  834. // we have code that gets a comment when
  835. // the button is clicked in scripts.js
  836. cy.get('.fixture-btn').click()
  837. cy.wait('@getComment').its('responseBody')
  838. .should('have.property', 'name')
  839. .and('include', 'Using fixtures to represent data')
  840. // or write fx to represent fixture
  841. // by default it assumes it's .json
  842. cy.route(/comments/, 'fx:example').as('getComment')
  843. // we have code that gets a comment when
  844. // the button is clicked in scripts.js
  845. cy.get('.fixture-btn').click()
  846. cy.wait('@getComment').its('responseBody')
  847. .should('have.property', 'name')
  848. .and('include', 'Using fixtures to represent data')
  849. })
  850. it('cy.readFile() - read a files contents', function () {
  851. // You can read a file and yield its contents
  852. // The filePath is relative to your project's root.
  853. // https://on.cypress.io/readfile
  854. cy.readFile('cypress.json').then(function (json) {
  855. expect(json).to.be.an('object')
  856. })
  857. })
  858. it('cy.writeFile() - write to a file', function () {
  859. // You can write to a file with the specified contents
  860. // Use a response from a request to automatically
  861. // generate a fixture file for use later
  862. cy.request('https://jsonplaceholder.typicode.com/users')
  863. .then(function (response) {
  864. // https://on.cypress.io/writefile
  865. cy.writeFile('cypress/fixtures/users.json', response.body)
  866. })
  867. cy.fixture('users').should(function (users) {
  868. expect(users[0].name).to.exist
  869. })
  870. // JavaScript arrays and objects are stringified and formatted into text.
  871. cy.writeFile('cypress/fixtures/profile.json', {
  872. id: 8739,
  873. name: 'Jane',
  874. email: 'jane@example.com',
  875. })
  876. cy.fixture('profile').should(function (profile) {
  877. expect(profile.name).to.eq('Jane')
  878. })
  879. })
  880. })
  881. context('Local Storage', function () {
  882. beforeEach(function () {
  883. cy.visit('https://example.cypress.io/commands/local-storage')
  884. })
  885. // Although local storage is automatically cleared
  886. // to maintain a clean state in between tests
  887. // sometimes we need to clear the local storage manually
  888. it('cy.clearLocalStorage() - clear all data in local storage', function () {
  889. // https://on.cypress.io/clearlocalstorage
  890. cy.get('.ls-btn').click().should(function () {
  891. expect(localStorage.getItem('prop1')).to.eq('red')
  892. expect(localStorage.getItem('prop2')).to.eq('blue')
  893. expect(localStorage.getItem('prop3')).to.eq('magenta')
  894. })
  895. // clearLocalStorage() yields the localStorage object
  896. cy.clearLocalStorage().should(function (ls) {
  897. expect(ls.getItem('prop1')).to.be.null
  898. expect(ls.getItem('prop2')).to.be.null
  899. expect(ls.getItem('prop3')).to.be.null
  900. })
  901. // **** Clear key matching string in Local Storage ****
  902. cy.get('.ls-btn').click().should(function () {
  903. expect(localStorage.getItem('prop1')).to.eq('red')
  904. expect(localStorage.getItem('prop2')).to.eq('blue')
  905. expect(localStorage.getItem('prop3')).to.eq('magenta')
  906. })
  907. cy.clearLocalStorage('prop1').should(function (ls) {
  908. expect(ls.getItem('prop1')).to.be.null
  909. expect(ls.getItem('prop2')).to.eq('blue')
  910. expect(ls.getItem('prop3')).to.eq('magenta')
  911. })
  912. // **** Clear key's matching regex in Local Storage ****
  913. cy.get('.ls-btn').click().should(function () {
  914. expect(localStorage.getItem('prop1')).to.eq('red')
  915. expect(localStorage.getItem('prop2')).to.eq('blue')
  916. expect(localStorage.getItem('prop3')).to.eq('magenta')
  917. })
  918. cy.clearLocalStorage(/prop1|2/).should(function (ls) {
  919. expect(ls.getItem('prop1')).to.be.null
  920. expect(ls.getItem('prop2')).to.be.null
  921. expect(ls.getItem('prop3')).to.eq('magenta')
  922. })
  923. })
  924. })
  925. context('Cookies', function () {
  926. beforeEach(function () {
  927. Cypress.Cookies.debug(true)
  928. cy.visit('https://example.cypress.io/commands/cookies')
  929. // clear cookies again after visiting to remove
  930. // any 3rd party cookies picked up such as cloudflare
  931. cy.clearCookies()
  932. })
  933. it('cy.getCookie() - get a browser cookie', function () {
  934. // https://on.cypress.io/getcookie
  935. cy.get('#getCookie .set-a-cookie').click()
  936. // cy.getCookie() yields a cookie object
  937. cy.getCookie('token').should('have.property', 'value', '123ABC')
  938. })
  939. it('cy.getCookies() - get browser cookies', function () {
  940. // https://on.cypress.io/getcookies
  941. cy.getCookies().should('be.empty')
  942. cy.get('#getCookies .set-a-cookie').click()
  943. // cy.getCookies() yields an array of cookies
  944. cy.getCookies().should('have.length', 1).should(function (cookies) {
  945. // each cookie has these properties
  946. expect(cookies[0]).to.have.property('name', 'token')
  947. expect(cookies[0]).to.have.property('value', '123ABC')
  948. expect(cookies[0]).to.have.property('httpOnly', false)
  949. expect(cookies[0]).to.have.property('secure', false)
  950. expect(cookies[0]).to.have.property('domain')
  951. expect(cookies[0]).to.have.property('path')
  952. })
  953. })
  954. it('cy.setCookie() - set a browser cookie', function () {
  955. // https://on.cypress.io/setcookie
  956. cy.getCookies().should('be.empty')
  957. cy.setCookie('foo', 'bar')
  958. // cy.getCookie() yields a cookie object
  959. cy.getCookie('foo').should('have.property', 'value', 'bar')
  960. })
  961. it('cy.clearCookie() - clear a browser cookie', function () {
  962. // https://on.cypress.io/clearcookie
  963. cy.getCookie('token').should('be.null')
  964. cy.get('#clearCookie .set-a-cookie').click()
  965. cy.getCookie('token').should('have.property', 'value', '123ABC')
  966. // cy.clearCookies() yields null
  967. cy.clearCookie('token').should('be.null')
  968. cy.getCookie('token').should('be.null')
  969. })
  970. it('cy.clearCookies() - clear browser cookies', function () {
  971. // https://on.cypress.io/clearcookies
  972. cy.getCookies().should('be.empty')
  973. cy.get('#clearCookies .set-a-cookie').click()
  974. cy.getCookies().should('have.length', 1)
  975. // cy.clearCookies() yields null
  976. cy.clearCookies()
  977. cy.getCookies().should('be.empty')
  978. })
  979. })
  980. context('Spies, Stubs, and Clock', function () {
  981. it('cy.spy() - wrap a method in a spy', function () {
  982. // https://on.cypress.io/spy
  983. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  984. let obj = {
  985. foo () {},
  986. }
  987. let spy = cy.spy(obj, 'foo').as('anyArgs')
  988. obj.foo()
  989. expect(spy).to.be.called
  990. })
  991. it('cy.stub() - create a stub and/or replace a function with a stub', function () {
  992. // https://on.cypress.io/stub
  993. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  994. let obj = {
  995. foo () {},
  996. }
  997. let stub = cy.stub(obj, 'foo').as('foo')
  998. obj.foo('foo', 'bar')
  999. expect(stub).to.be.called
  1000. })
  1001. it('cy.clock() - control time in the browser', function () {
  1002. // create the date in UTC so its always the same
  1003. // no matter what local timezone the browser is running in
  1004. let now = new Date(Date.UTC(2017, 2, 14)).getTime()
  1005. // https://on.cypress.io/clock
  1006. cy.clock(now)
  1007. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  1008. cy.get('#clock-div').click()
  1009. .should('have.text', '1489449600')
  1010. })
  1011. it('cy.tick() - move time in the browser', function () {
  1012. // create the date in UTC so its always the same
  1013. // no matter what local timezone the browser is running in
  1014. let now = new Date(Date.UTC(2017, 2, 14)).getTime()
  1015. // https://on.cypress.io/tick
  1016. cy.clock(now)
  1017. cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
  1018. cy.get('#tick-div').click()
  1019. .should('have.text', '1489449600')
  1020. cy.tick(10000) // 10 seconds passed
  1021. cy.get('#tick-div').click()
  1022. .should('have.text', '1489449610')
  1023. })
  1024. })
  1025. context('Utilities', function () {
  1026. beforeEach(function () {
  1027. cy.visit('https://example.cypress.io/utilities')
  1028. })
  1029. it('Cypress._.method() - call a lodash method', function () {
  1030. // use the _.chain, _.map, _.take, and _.value functions
  1031. // https://on.cypress.io/_
  1032. cy.request('https://jsonplaceholder.typicode.com/users')
  1033. .then(function (response) {
  1034. let ids = Cypress._.chain(response.body).map('id').take(3).value()
  1035. expect(ids).to.deep.eq([1, 2, 3])
  1036. })
  1037. })
  1038. it('Cypress.$(selector) - call a jQuery method', function () {
  1039. // https://on.cypress.io/$
  1040. let $li = Cypress.$('.utility-jquery li:first')
  1041. cy.wrap($li)
  1042. .should('not.have.class', 'active')
  1043. .click()
  1044. .should('have.class', 'active')
  1045. })
  1046. it('Cypress.moment() - format or parse dates using a moment method', function () {
  1047. // use moment's format function
  1048. // https://on.cypress.io/cypress-moment
  1049. let time = Cypress.moment().utc('2014-04-25T19:38:53.196Z').format('h:mm A')
  1050. cy.get('.utility-moment').contains('3:38 PM')
  1051. .should('have.class', 'badge')
  1052. })
  1053. it('Cypress.Blob.method() - blob utilities and base64 string conversion', function () {
  1054. cy.get('.utility-blob').then(function ($div) {
  1055. // https://on.cypress.io/blob
  1056. // https://github.com/nolanlawson/blob-util#imgSrcToDataURL
  1057. // get the dataUrl string for the javascript-logo
  1058. return Cypress.Blob.imgSrcToDataURL('https://example.cypress.io/assets/img/javascript-logo.png', undefined, 'anonymous')
  1059. .then(function (dataUrl) {
  1060. // create an <img> element and set its src to the dataUrl
  1061. let img = Cypress.$('<img />', { src: dataUrl })
  1062. // need to explicitly return cy here since we are initially returning
  1063. // the Cypress.Blob.imgSrcToDataURL promise to our test
  1064. // append the image
  1065. $div.append(img)
  1066. cy.get('.utility-blob img').click()
  1067. .should('have.attr', 'src', dataUrl)
  1068. })
  1069. })
  1070. })
  1071. it('new Cypress.Promise(function) - instantiate a bluebird promise', function () {
  1072. // https://on.cypress.io/promise
  1073. let waited = false
  1074. function waitOneSecond () {
  1075. // return a promise that resolves after 1 second
  1076. return new Cypress.Promise(function (resolve, reject) {
  1077. setTimeout(function () {
  1078. // set waited to true
  1079. waited = true
  1080. // resolve with 'foo' string
  1081. resolve('foo')
  1082. }, 1000)
  1083. })
  1084. }
  1085. cy.then(function () {
  1086. // return a promise to cy.then() that
  1087. // is awaited until it resolves
  1088. return waitOneSecond().then(function (str) {
  1089. expect(str).to.eq('foo')
  1090. expect(waited).to.be.true
  1091. })
  1092. })
  1093. })
  1094. })
  1095. context('Cypress.config()', function () {
  1096. beforeEach(function () {
  1097. cy.visit('https://example.cypress.io/cypress-api/config')
  1098. })
  1099. it('Cypress.config() - get and set configuration options', function () {
  1100. // https://on.cypress.io/config
  1101. let myConfig = Cypress.config()
  1102. expect(myConfig).to.have.property('animationDistanceThreshold', 5)
  1103. expect(myConfig).to.have.property('baseUrl', null)
  1104. expect(myConfig).to.have.property('defaultCommandTimeout', 4000)
  1105. expect(myConfig).to.have.property('requestTimeout', 5000)
  1106. expect(myConfig).to.have.property('responseTimeout', 30000)
  1107. expect(myConfig).to.have.property('viewportHeight', 660)
  1108. expect(myConfig).to.have.property('viewportWidth', 1000)
  1109. expect(myConfig).to.have.property('pageLoadTimeout', 60000)
  1110. expect(myConfig).to.have.property('waitForAnimations', true)
  1111. expect(Cypress.config('pageLoadTimeout')).to.eq(60000)
  1112. // this will change the config for the rest of your tests!
  1113. Cypress.config('pageLoadTimeout', 20000)
  1114. expect(Cypress.config('pageLoadTimeout')).to.eq(20000)
  1115. Cypress.config('pageLoadTimeout', 60000)
  1116. })
  1117. })
  1118. context('Cypress.env()', function () {
  1119. beforeEach(function () {
  1120. cy.visit('https://example.cypress.io/cypress-api/env')
  1121. })
  1122. // We can set environment variables for highly dynamic values
  1123. // https://on.cypress.io/environment-variables
  1124. it('Cypress.env() - get environment variables', function () {
  1125. // https://on.cypress.io/env
  1126. // set multiple environment variables
  1127. Cypress.env({
  1128. host: 'veronica.dev.local',
  1129. api_server: 'http://localhost:8888/v1/',
  1130. })
  1131. // get environment variable
  1132. expect(Cypress.env('host')).to.eq('veronica.dev.local')
  1133. // set environment variable
  1134. Cypress.env('api_server', 'http://localhost:8888/v2/')
  1135. expect(Cypress.env('api_server')).to.eq('http://localhost:8888/v2/')
  1136. // get all environment variable
  1137. expect(Cypress.env()).to.have.property('host', 'veronica.dev.local')
  1138. expect(Cypress.env()).to.have.property('api_server', 'http://localhost:8888/v2/')
  1139. })
  1140. })
  1141. context('Cypress.Cookies', function () {
  1142. beforeEach(function () {
  1143. cy.visit('https://example.cypress.io/cypress-api/cookies')
  1144. })
  1145. // https://on.cypress.io/cookies
  1146. it('Cypress.Cookies.debug() - enable or disable debugging', function () {
  1147. Cypress.Cookies.debug(true)
  1148. // Cypress will now log in the console when
  1149. // cookies are set or cleared
  1150. cy.setCookie('fakeCookie', '123ABC')
  1151. cy.clearCookie('fakeCookie')
  1152. cy.setCookie('fakeCookie', '123ABC')
  1153. cy.clearCookie('fakeCookie')
  1154. cy.setCookie('fakeCookie', '123ABC')
  1155. })
  1156. it('Cypress.Cookies.preserveOnce() - preserve cookies by key', function () {
  1157. // normally cookies are reset after each test
  1158. cy.getCookie('fakeCookie').should('not.be.ok')
  1159. // preserving a cookie will not clear it when
  1160. // the next test starts
  1161. cy.setCookie('lastCookie', '789XYZ')
  1162. Cypress.Cookies.preserveOnce('lastCookie')
  1163. })
  1164. it('Cypress.Cookies.defaults() - set defaults for all cookies', function () {
  1165. // now any cookie with the name 'session_id' will
  1166. // not be cleared before each new test runs
  1167. Cypress.Cookies.defaults({
  1168. whitelist: 'session_id',
  1169. })
  1170. })
  1171. })
  1172. context('Cypress.dom', function () {
  1173. beforeEach(function () {
  1174. cy.visit('https://example.cypress.io/cypress-api/dom')
  1175. })
  1176. // https://on.cypress.io/dom
  1177. it('Cypress.dom.isHidden() - determine if a DOM element is hidden', function () {
  1178. let hiddenP = Cypress.$('.dom-p p.hidden').get(0)
  1179. let visibleP = Cypress.$('.dom-p p.visible').get(0)
  1180. // our first paragraph has css class 'hidden'
  1181. expect(Cypress.dom.isHidden(hiddenP)).to.be.true
  1182. expect(Cypress.dom.isHidden(visibleP)).to.be.false
  1183. })
  1184. })
  1185. context('Cypress.Server', function () {
  1186. beforeEach(function () {
  1187. cy.visit('https://example.cypress.io/cypress-api/server')
  1188. })
  1189. // Permanently override server options for
  1190. // all instances of cy.server()
  1191. // https://on.cypress.io/cypress-server
  1192. it('Cypress.Server.defaults() - change default config of server', function () {
  1193. Cypress.Server.defaults({
  1194. delay: 0,
  1195. force404: false,
  1196. whitelist (xhr) {
  1197. // handle custom logic for whitelisting
  1198. },
  1199. })
  1200. })
  1201. })
  1202. })