Commands Flashcards
cy.document()
Get the window.document of the page that is currently active.
.and()
Alias to .should().
- Used to make a chain of .should assertions more human-readable.
Common Usages: .and(chainers) .and(chainers, value) .and(chainers, method, value) .and(callbackFn)
Example:
cy.get(‘.err’).should(‘be.empty’).and(‘be.hidden’)
.as(aliasName)
A command that stores an element in an alias for use later.
.check()
Command used to set checkboxes or radio input elements.
.check() .check(value) .check(values) .check(options) .check(value, options) .check(values, options)
cy. get(‘[type=”checkbox”]’).check() // Check checkbox element
cy. get(‘[type=”radio”]’).first().check() // Check first radio element
.children()
Finds all the children of a given DOM element. Similar to the jQuery.children() command.
.children()
.children(selector)
.children(options)
.children(selector, options)
https://docs.cypress.io/api/commands/children.html#Syntax
.clear()
Command used to clear the value of an input or textarea.
Yields: Same as input.
.click()
Command to work with clickable DOM elements. No arguments.
.click() .click(options) .click(position) .click(position, options) .click(x, y) .click(x, y, options)
Yields: Same as input.
cy.clock()
Command used to override the global clock. cy.clock() overrides native global functions related to time allowing them to be controlled synchronously via cy.tick() or the yielded clock object.
https://docs.cypress.io/api/commands/clock.html#Syntax
.closed()
Get the first DOM element that matches the selector (whether it be itself or one of its ancestors). Worked in the same manner as the JQuery .closed() command.
Yields: the new DOM element(s) it found.
.contains()
Get the DOM element containing the text.
Chained: .contains(content) .contains(content, options) .contains(selector, content) .contains(selector, content, options)
From Cy root
cy. contains(content)
cy. contains(content, options)
cy. contains(selector, content)
cy. contains(selector, content, options)
Yields: the new DOM element it found.
.dblclick()
Double-click a DOM element.
Usages:
cy. get(‘button’).dblclick() // Double click on button
cy. focused().dblclick() // Double click on el with focus
cy. contains(‘Welcome’).dblclick() // Double click on first el containing ‘Welcome’
Yields: the same subject it was given from the previous command.
.debug()
Set a debugger and log what the previous command yields.
cy.document()
Get the window.document of the page that is currently active.
.each(callbackFn)
Command that allows for an assertion to iterate through an array like structure (arrays or objects with a length property).
Yields: the same subject it was given from the previous command.
.end()
Command to end a chain of commands. This command is useful when you want to end a chain of commands and force the next command to not receive what was yielded in the previous command.
Yields: Null and yields null cannot be chained further.
.eq()
Get A DOM element at a specific index in an array of elements.
Usage:
cy. get(‘tbody>tr’).eq(0) // Yield first ‘tr’ in ‘tbody’
cy. get(‘ul>li’).eq(4) // Yield fifth ‘li’ in ‘ul’
Yields: the new DOM element(s) it found.
cy.exec(command, options)
Cy root command to run a system command.
Usage:
cy.exec(‘npm run build’)
.filter(selector, options)
Get the DOM elements that match a specific selector.
Usage:
cy.get(‘ul’).find(‘>li’).filter(‘.active’)
Yields: new DOM element(s) it found.
.find()
Command used in a chain to getfind the descendent DOM elements of a specific selector. Similar to the jQuery .find().
Usage:
cy.get(‘.article’).find(‘footer’)
Yields: the new DOM element(s) it found.
.first(options)
Get the first DOM element within a set of DOM elements.
Usage:
cy.get(‘nav a’).first()
Yields: the new DOM elements it found.
cy.fixture(filePath, encoding, options)
Cy command to access predefined fixture elements.
Usage: cy.fixture('users').as('usersJson') // load data from users.json cy.fixture('logo.png').then((logo) => { // load data from logo.png })
Yields: the contents of the file. Formatting is determined by its file extension.
.focus()
.focus(options)
Command used to set the focus on a specific DOM element.
Usage:
cy.get(‘[type=”input”]’).focus()
Yields: yields the same subject it was given from the previous command.
cy. focused()
cy. focused(options)
A Command that gets an already selected DOM element.
Yields: the DOM element(s) it found.
cy.get(selector’)
Cy root command to get a given DOM element using the selector value. Very similar to the jQuery $(…).
Usage:
cy.get(‘.list > li’) // Yield the <li>’s in .list
Yields: the DOM element(s) it found.</li>
cy.go(direction, options)
Cy root command to navigate back or forward to the previous or next URL in the browser’s history.
Usage:
cy.go(‘back’)
Yields: The window object after the page finishes loading.
cy.hash(options)
Cy root command to access the current URL hash of the page that is currently active. Aliases to cy.location(‘hash’).
Usage:
cy.hash().should(‘eq’, ‘#/users/1’) // => true
Yields: yields the current URL’s hash (including the # character). or when no hash it yields an empty string.
.invoke(fnName)
A chained command to invoke a function. Muse be passed in an element.
.its(propertyName, options)
Get a property’s value on the previously yielded subject.
Usage:
cy.wrap({ age: 52 }).its(‘age’).should(‘eq’, 52)
Yields: the value of the property.
.last(options)
A chainable command to get the last DOM element within a set of DOM elements.
Yields: the new DOM element(s) it found.
cy.location(key, options)
A Cy root command to get the global window.location object of the page that is currently active.
Usage:
cy.location().should((loc) => {
expect(loc.hash).to.eq(‘#/users/123/edit’)
expect(loc.host).to.eq(‘localhost:8000’)
expect(loc.hostname).to.eq(‘localhost’)
expect(loc.href).to.eq(‘http://localhost:8000/app/index.html?q=dan#/users/123/edit’)
expect(loc.origin).to.eq(‘http://localhost:8000’)
expect(loc.pathname).to.eq(‘/app/index.html’)
expect(loc.port).to.eq(‘8000’)
expect(loc.protocol).to.eq(‘http:’)
expect(loc.search).to.eq(‘?q=dan’)
expect(loc.toString()).to.eq(‘http://localhost:8000/app/index.html?q=brian#/users/123/edit’)
})
Yields: the location object and its properties.