- all of the straightforward ones are done - still working on the custom / complex ones
3.3 KiB
title, comments
| title | comments |
|---|---|
| its | false |
Get a property on the previously yielded subject.
{% note info %}
If you want to call a function on the previously yielded subject, use {% url .invoke() invoke %}.
{% endnote %}
Syntax
.its(propertyName)
Usage
.its() requires being chained off another cy command that yields an object with properties.
{% fa fa-check-circle green %} Valid Usage
cy.wrap({width: '50'}).its('width') // Get the 'width' property
cy.location().its('href') // Get the 'href' property
{% fa fa-exclamation-triangle red %} Invalid Usage
cy.its('window') // Errors, cannot be chained off 'cy'
cy.clearCookies().its('length') // Errors, 'clearCookies' does not yield Object
Arguments
{% fa fa-angle-right %} propertyName (String)
Name of property or nested properties (with dot notation) to get.
Yields {% helper_icon yields %}
{% yields changes_subject .its 'yields the value of the property' }
Timeouts {% helper_icon timeout %}
Examples
Plain Objects
Get property
cy.wrap({age: 52}).its('age').should('eq', 52) // true
DOM Elements
Get the length property of a DOM element
cy
.get('ul li') // this yields us a jquery object
.its('length') // calls 'length' property returning that value
.should('be.gt', 2) // ensure the length is greater than 2
})
Strings
Get length of title
cy.title().its('length').should('eq', 24)
Functions
Get function property
var fn = function(){
return 42
}
cy.wrap({getNum: fn}).its('getNum').should('be.a', 'function')
Inspect function
You can access functions to then drill into their own properties instead of invoking them.
// Your app code
// a basic Factory constructor
var Factory = function(arg){
// ...
}
Factory.create = function(arg){
return new Factory(arg)
}
// assign it to the window
window.Factory = Factory
cy
.window() // yields window object
.its('Factory') // yields Factory function
.invoke('create', 'arg') // now invoke properties on it
Use .its() to test window.fetch
{% note info %}
{% url "Check out our example recipe on testing window.fetch using .its()" stubs-spies-and-clocks-recipe %}
{% endnote %}
Nested Properties
You can drill into nested properties by using dot notation.
var user = {
contacts: {
work: {
name: 'Kamil'
}
}
}
cy.wrap(user).its('contacts.work.name').should('eq', 'Kamil') // true
Command Log
Get responseBody of aliased route
cy.server()
cy.route(/comments/, 'fixture:comments.json').as('getComments')
cy.get('#fetch-comments').click()
cy.wait('@getComments').its('responseBody').should('deep.eq', [
{id: 1, comment: 'hi'},
{id: 2, comment: 'there'}
])
The commands above will display in the command log as:
When clicking on its within the command log, the console outputs the following:
See also
- {% url
.invoke()invoke %} - {% url
.then()then %} - {% url
cy.wrap()wrap %}

