Files
cypress/docs/source/api/commands/then.md
T
2017-05-15 14:10:01 -04:00

4.0 KiB

title: then comments: true

cy.then() will yield you the current subject as the first argument.

cy.then() is modeled identically to the way Promises work in JavaScript. Whatever is returned from the callback function becomes the new subject, and will flow into the next command, with the exception of null and undefined.

When null or undefined is returned by the callback function, the subject will not be modified and will instead carry over to the next command.

Just like Promises, you can return any compatible "thenable" (anything that has a .then() interface) and Cypress will wait for that to resolve before continuing forward through the chain of commands.

Returns the return of the callback function
Timeout cy.then will retry for the duration of the defaultCommandTimeout or the duration of the timeout specified in the command's options.

cy.then( function )

Yield the current subject as the first argument.


Usage

The element input is yielded

<form id="todos">
  <input type="text" class="addTodo" />
</form>
cy.get("form").find("input").then(function($input){
  // work with $input subject here
  // we can potentially use it within an assertion
  // or just call some methods on it and return a new subject
})

Options

Pass in an options object to change the default behavior of cy.then.

cy.click( options, function )

Option Default Notes
timeout defaultCommandTimeout Total time to retry the click

Usage

Assert explicitly about the subject <li>'s

<div id="todos">
  <li>Walk the dog</li>
  <li>Feed the cat</li>
  <li>Write JavaScript</li>
</div>
cy.get("#todos li").then(function($lis){
  expect($lis).to.have.length(3)
  expect($lis.eq(0)).to.contain("Walk the dog")
  expect($lis.eq(1)).to.contain("Feed the cat")
  expect($lis.eq(2)).to.contain("Write JavaScript")
})

Normally you'd use implicit subject assertions via should or and, but it's sometimes it's more convenient to write explicit assertions about a given subject.

{% note warning %} Any errors raised by failed assertions will immediately bubble up and cause the test to fail. {% endnote %}


The subject is changed by returning {foo: 'bar'}

cy.then(function(){
  return {foo: "bar"}
}).then(function(obj){
  // subject is now the obj {foo: "bar"}
  expect(obj).to.deep.eq({foo: "bar"}) // true
})

Cypress waits for the Promise to resolve before continuing

// if using Q
cy.get("button").click().then(function($button){
  var p = Q.defer()

  setTimeout(function(){
    p.resolve()
  }, 5000)

  return p.promise
})

// if using bluebird
cy.get("button").click().then(function($button){
  return Promise.delay(5000)
})

// if using jQuery deferred's
cy.get("button").click().then(function($button){
  var df = $.Deferred()

  setTimeout(function(){
    df.resolve()
  }, 5000)

  return df
})

Returning null or undefined will not modify the subject

cy
  .get("form").then(function($form){
    console.log("form is:", $form)
    // undefined is returned here, therefore
    // the $form subject will automatically
    // carry over and allow for continued chaining
  }).find("input").then(function($input){
    // we have our real $input element here since
    // our form element carried over and we called
    // .find("input") on it
  })

Options Usage

cy.then({timeout: 7000}, function(){
  // code here
})

Related