4.Cypress事件赋值清空赋值与断言

436 阅读2分钟

一、事件

1.blur

Blur a focused element.

Syntax

.blur()
.blur(options)

Usage

cy.get('[type="email"]').type('me@email.com').blur() // Blur email input
cy.get('[tabindex="1"]').focus().blur()              // Blur el with tabindex

2.check

Check checkbox(es) or radio(s).

Syntax

.check()
.check(value)
.check(values)
.check(options)
.check(value, options)
.check(values, options)

Usage

cy.get('[type="checkbox"]').check()       // Check checkbox element
cy.get('[type="radio"]').first().check()  // Check first radio element

3.click

Click a DOM element.

Syntax

.click()
.click(options)
.click(position)
.click(position, options)
.click(x, y)
.click(x, y, options)

Usage

cy.get('.btn').click()          // Click on button
cy.focused().click()            // Click on el with focus
cy.contains('Welcome').click()  // Click on first el containing 'Welcome'

4.dblclick

Double-click a DOM element.

Syntax

.dblclick()
.dblclick(options)
.dblclick(position)
.dblclick(position, options)
.dblclick(x, y)
.dblclick(x, y, options)

Usage

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'

5.focus

Focus on a DOM element.

Syntax

.focus()
.focus(options)

Usage

cy.get('input').first().focus() // Focus on the first input

6.rightclick

Right click a DOM element.

Syntax

.rightclick()
.rightclick(options)
.rightclick(position)
.rightclick(position, options)
.rightclick(x, y)
.rightclick(x, y, options)

Usage

cy.get('.menu').rightclick()       // Right click on .menu
cy.focused().rightclick()          // Right click on el with focus
cy.contains('Today').rightclick()  // Right click on first el containing 'Today'

7.scrollIntoView

Scroll an element into view.

Syntax

.scrollIntoView()
.scrollIntoView(options)

Usage

cy.get('footer').scrollIntoView() // Scrolls 'footer' into view

8.scrollTo

Scroll to a specific position.

Syntax

cy.scrollTo(position)
cy.scrollTo(x, y)
cy.scrollTo(position, options)
cy.scrollTo(x, y, options)

// ---or---

.scrollTo(position)
.scrollTo(x, y)
.scrollTo(position, options)
.scrollTo(x, y, options)

Usage

cy.scrollTo(0, 500)                     // Scroll the window 500px down
cy.get('.sidebar').scrollTo('bottom')   // Scroll 'sidebar' to its bottom

9.select

Select an <option> within a <select>.

Syntax

.select(value)
.select(values)
.select(value, options)
.select(values, options)

Usage

cy.get('select').select('user-1') // Select the 'user-1' option

10.trigger

Trigger an event on a DOM element.

Syntax

.trigger(eventName)
.trigger(eventName, position)
.trigger(eventName, options)
.trigger(eventName, x, y)
.trigger(eventName, position, options)
.trigger(eventName, x, y, options)

Usage

cy.get('a').trigger('mousedown') // Trigger mousedown event on link

11.uncheck

Uncheck checkbox(es).

Syntax

.uncheck()
.uncheck(value)
.uncheck(values)
.uncheck(options)
.uncheck(value, options)
.uncheck(values, options)

Usage

cy.get('[type="checkbox"]').uncheck()   // Unchecks checkbox element

二、赋值与清空

1.clear

Clear the value of an input or textarea.

Syntax

.clear()
.clear(options)

Usage

cy.get('[type="text"]').clear()        // Clear text input
cy.get('textarea').type('Hi!').clear() // Clear textarea
cy.focused().clear()                   // Clear focused input/textarea

2.clearCookie

Clear a specific browser cookie.

Syntax

cy.clearCookie(name)
cy.clearCookie(name, options)

Usage

cy.clearCookie('authId')    // clear the 'authId' cookie

3.clearCookies

Clear all browser cookies for current domain and subdomain.

Syntax

cy.clearCookies()
cy.clearCookies(options)

Usage

cy.clearCookies()     // clear all cookies

4.clearLocalStorage

Clear data in localStorage for current domain and subdomain.

Syntax

cy.clearLocalStorage()
cy.clearLocalStorage(key)
cy.clearLocalStorage(options)
cy.clearLocalStorage(keys, options)

Usage

cy.clearLocalStorage()  // clear all local storage

5.getCookie

Get a browser cookie by its name.

Syntax

cy.getCookie(name)
cy.getCookie(name, options)

Usage

cy.getCookie('auth_key')     // Get cookie with name 'auth_key'

6.getCookies

Get all of the browser cookies.

Syntax

cy.getCookies()
cy.getCookies(options)

Usage

cy.getCookies()    // Get all cookies

7.setCookie

Set a browser cookie.

Syntax

cy.setCookie(name, value)
cy.setCookie(name, value, options)

Usage

cy.setCookie('auth_key', '123key') // Set the 'auth_key' cookie to '123key'

三、断言
1.should
.should('be.属性') //断言选中元素拥有特定属性
.should('have.css', css名称) //断言选中元素拥有特定class
should('have.value', value) //断言选中元素拥有特定value
2.and
and是should的别名,用法基本一致