Cypress 简明教程

Cypress - Basic Commands

基本的Cypress命令如下:

Cypress basic commands are listed below −

and

它用于创建断言,是.should()的别名。

It is used to create an assertion and is an alias of .should ().

用法如下:

The usage is as follows −

//element is visible & enabled
cy.get('#txt').should('be.visible').and('be.enabled')
//element is checked
cy.contains('Subject').and('be.checked')

as

它提供了别名以便以后使用。

It provides an alias for later usage.

用法如下:

The usage is as follows −

//alias element as parent
cy.get('#txt').find('li').first().as('parent')

blur

它模糊焦点中元素。

It blurs an element in focus.

用法如下:

The usage is as follows −

//blur input
cy.get('#txt'). type('abc').blur()

check

它选中单选按钮或复选框,并应用于拥有 input 标签的元素。

It checks radio buttons or checkboxes and is applied to elements having input tags.

用法如下:

The usage is as follows −

//checks element having class attribute chkbox
cy.get('.chkbox').check()

children

它获得元素的子元素。

It obtains the sub elements of an element.

用法如下:

The usage is as follows −

//obtains children of element n
cy.get('n').children()

clear

它从 textarea 或 input 中删除值。

It removes the value from textarea or input.

用法如下:

The usage is as follows −

//removes input abc
cy.get('#txt'). type('abc').clear()

clearCookie

它删除特定的浏览器 cookie。

It removes a particular browser cookie.

用法如下:

The usage is as follows −

//clear abc cookie
cy.clearCookie('abc')

clearCookies

它从现有域和子域中删除浏览器 cookie。

It removes the browser cookies from an existing domain and subdomain.

用法如下:

The usage is as follows −

//clear all cookies
cy.clearCookies()

clearLocalStorage

它从现有域和子域中删除本地存储数据。

It removes the local Storage data from an existing domain and subdomain.

用法如下:

The usage is as follows −

//clear all local storage
cy. clearLocalStorage ()

click

它点击 Document Object Model (DOM) 中的元素。

It clicks an element in Document Object Model (DOM).

用法如下:

The usage is as follows −

//click on element with id txt
cy.get('#txt').click()

contains

它获得具有特定文本的元素。元素可以拥有比文本更多内容并且仍然匹配。

It obtains an element having a specific text. The element can have more than the text and still match.

用法如下:

The usage is as follows −

//returns element in #txt having Tutor text
cy.get('#txt').contains('Tutor')

dblclick

它双击 Document Object Model (DOM) 中的元素。

It double-clicks an element in Document Object Model (DOM).

用法如下:

The usage is as follows −

//double clicks element with id txt
cy.get('#txt').dblclick()

debug

它修复调试器,并由先前命令返回日志值。

It fixes a debugger and log values are returned by prior command.

用法如下:

The usage is as follows −

//pause to debug at start of command
cy.get('#txt').debug()

document

它在活动页面上获得 window.document。

It obtains window.document on the active page.

用法如下:

The usage is as follows −

cy.document()

each

它遍历具有 length 属性的数组。

It iterates through an array having the property length.

用法如下:

The usage is as follows −

//iterate through individual li
cy.get('li').each(() => {...})

end

它结束命令链。

It ends a command chain.

用法如下:

The usage is as follows −

//obtain null instead of input
cy.contains('input').end()

eq

它引用元素数组中特定索引处的元素。

It refers to an element at a particular index in an array of elements.

用法如下:

The usage is as follows −

//obtain third td in tr
cy.get('tr>td').eq(2)

exec

它运行系统命令。

It runs a system command.

用法如下:

The usage is as follows −

cy.exec('npm init')

find

它获得特定定位器的后代元素。

It obtains the descendant elements of a particular locator.

用法如下:

The usage is as follows −

//obtain td from tr
cy.get('tr').find('td')

first

它从一组元素中获取第一个元素。

It obtains the first element from a group of elements.

用法如下:

The usage is as follows −

//obtain first td in tr
cy.get('tr>td').first()

get

它通过定位器获取单个或多个元素。

It obtains single or multiple elements by locator.

用法如下:

The usage is as follows −

//obtain td from tr

find

它获得特定定位器的后代元素。

It obtains the descendant elements of a particular locator.

用法如下:

The usage is as follows −

//obtain all td from tr in list
cy.get('tr>td')

getCookie

它通过名称获取特定浏览器 cookie。

It obtains a particular browser cookie by its name.

用法如下:

The usage is as follows −

cy.getCookie('abc')

getCookies

它获取所有 cookie。

It obtains all the cookies

用法如下:

The usage is as follows −

cy.getCookies()

go

在浏览器历史记录中向前或向后移动到下一个或上一个 URL。

It moves forward or backward to the next or previous URL in browser history.

用法如下:

The usage is as follows −

//like clicking back button
cy.go('back')
//like clicking forward button
cy.go('forward')

visit

它启动一个 URL。

It launches an URL.

用法如下:

The usage is as follows −

cy.visit('https://www.tutorialspoint.com/index.htm')

next

它获取文档对象模型 (DOM) 中元素组中元素的直接同级元素。

It obtains the immediate sibling of an element within a group of elements in Document Object Model (DOM).

用法如下:

The usage is as follows −

//gives the following link in element l.
cy.get('l a:first').next()

parent

它从 DOM 中元素组中获取父元素。

It obtains the parent element from a group of elements in DOM.

用法如下:

The usage is as follows −

//get parent of element with class h
cy.get('.h').parent()

should

它用于创建断言,是 .and() 的别名。

It is used to create an assertion and is an alias of .and ().

用法如下:

The usage is as follows −

//assert element is visible & enabled
cy.get('#txt').should('be.visible').and('be.enabled')

wait

在执行下一步之前,以毫秒为单位或以别名元素等待一段时间。

Wait for a certain time in milliseconds or for an aliased element prior to moving the following step.

用法如下:

The usage is as follows −

cy.wait(1000)

title

它获取活动页面的 document.title。

It obtains the document.title of the active page.

用法如下:

The usage is as follows −

cy.title()

viewport

它管理屏幕的尺寸和定位。

It manages the dimension and positioning of the screen.

用法如下:

The usage is as follows −

// viewport to 100px and 500px
cy.viewport(100, 500)

log

它将消息打印到命令日志。

It prints the messages to the Command Log.

用法如下:

The usage is as follows −

cy.log('Cypress logging ')

reload

它用于页面的重新加载。

It is used for page reloading.

用法如下:

The usage is as follows −

cy.reload()