3.5 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| invoke | true |
cy.invoke invokes functions on the current subject.
If you want to call a regular property that is not a function on the current subject, use cy.its.
| Returns | the return value of the invoked property |
| Timeout | cy.invoke cannot timeout unless you've added assertions. The assertions will retry for the duration of defaultCommandTimeout |
cy.invoke( functionName )
Invokes the function with the specified name
cy.invoke( functionName, *arguments )
Invokes the function with the specified name and forwards any additional arguments to the function call. There are no limits to the number of arguments.
Function Usage
Assert on a function after invoke
var fn = function(){
return "bar"
}
cy.wrap({foo: fn}).invoke("foo").should("eq", "bar") // true
{% note info Using cy.invoke('text') %} Check out our example recipe where we use cy.invoke('text') to test against HTML content {% endnote %}
Properties that are functions are invoked
// force a hidden div to be 'display: block'
// so we can interact with its children elements
cy
.get("div.container").should("be.hidden") // true
.invoke("show") // call the jquery method 'show' on the 'div.container'
.should("be.visible") // true
.find("input").type("Cypress is great")
{% note info Using cy.invoke('show') and cy.invoke('trigger') %} Check out our example recipe where we use cy.invoke('show') and cy.invoke('trigger') to click an element that is only visible on hover {% endnote %}
Useful for 3rd party plugins
// as a slightly verbose approach
cy.get("input").invoke("getKendoDropDownList").then(function(dropDownList){
// the return of $input.getKendoDropDownList() has now become the new subject
// whatever the select method returns becomes the next subject after this
return dropDownList.select("apples")
})
We can rewrite the previous example in a more terse way and add an assertion.
cy
.get("input")
.invoke("getKendoDropDownList")
.invoke("select", "apples")
.its("val").should("match", /apples/)
Function with Arguments Usage
Send specific arguments to the function
var fn = function(a, b, c){
return a + b + c
}
cy
.wrap({sum: fn})
.invoke("sum", 2, 4, 6)
.should("be.gt", 10) // true
.and("be.lt", 20) // true
{% note info Using cy.invoke('removeAttr', 'target') %} Check out our example recipe where we use cy.invoke('removeAttr', 'target') to test clicking on a link without opening in a new tab {% endnote %}
Arguments are automatically forwarded to the function
cy
.get("img").invoke("attr", "src")
.should("include", "myLogo")