3.Cypress元素选择器

1,250 阅读3分钟

一、children

获取DOM中的子元素。

Syntax

.children()
.children(selector)
.children(options)
.children(selector, options)

Usage

cy.get('nav').children()     // Yield children of nav

二、closest

获取与dom最接近的目标元素

Syntax

.closest(selector)
.closest(selector, options)

Usage

cy.get('td').closest('.filled') // Yield closest el with class '.filled'

三、contains

包含目标内容的元素

Syntax

.contains(content)
.contains(content, options)
.contains(selector, content)
.contains(selector, content, options)

// ---or---

cy.contains(content)
cy.contains(content, options)
cy.contains(selector, content)
cy.contains(selector, content, options)

Usage

cy.get('.nav').contains('About')  // Yield el in .nav containing 'About'
cy.contains('Hello')              // Yield first el in document containing 'Hello'

四、document

获取window.document

Syntax

cy.document()
cy.document(options)

Usage

cy.document()     // yield the window.document object

五、eq

从目标元素中选定特定索引的元素

Syntax

.eq(index)
.eq(indexFromEnd)
.eq(index, options)
.eq(indexFromEnd, options)

Usage

cy.get('tbody>tr').eq(0)    // Yield first 'tr' in 'tbody'
cy.get('ul>li').eq(4)       // Yield fifth 'li' in 'ul'

六、filter

获取与特定选择器匹配的DOM元素

Syntax

.filter(selector)
.filter(selector, options)

Usage

cy.get('td').filter('.users') // Yield all el's with class '.users'

七、find

获取特定选择器的后代DOM元素。

Syntax

.find(selector)
.find(selector, options)

Usage

cy.get('.article').find('footer') // Yield 'footer' within '.article'

八、first

获取一组DOM元素中的第一个DOM元素。

Syntax

.first()
.first(options)

Usage

cy.get('nav a').first()     // Yield first link in nav

九、focused

获取选中状态的元素

Syntax

cy.focused()
cy.focused(options)

Usage

cy.focused()    // Yields the element currently in focus

十、get

获取目标元素,语法类似jq

Syntax

cy.get(selector)
cy.get(alias)
cy.get(selector, options)
cy.get(alias, options)

Usage

cy.get('.list > li') // Yield the <li>'s in .list

十一、its

获取选中项的属性

Syntax

.its(propertyName)
.its(propertyName, options)

Usage

cy.wrap({ width: '50' }).its('width') // Get the 'width' property
cy.window().its('sessionStorage')     // Get the 'sessionStorage' property

十二、last

获取一组DOM元素中的最后一个DOM元素。

Syntax

.last()
.last(options)

Usage

cy.get('nav a').last()     // Yield last link in nav

十三、location

获取window.location的信息

Syntax

cy.location()
cy.location(key)
cy.location(options)
cy.location(key, options)

Usage

cy.location()       // Get location object
cy.location('host') // Get the host of the location object
cy.location('port') // Get the port of the location object

十四、next

获取目标元素的下一个元素

Syntax

.next()
.next(selector)
.next(options)
.next(selector, options)

Usage

cy.get('nav a:first').next() // Yield next link in nav

十五、nextAll

获取目标元素之后的所有平级元素

Syntax

.nextAll()
.nextAll(selector)
.nextAll(options)
.nextAll(selector, options)

Usage

cy.get('.active').nextAll() // Yield all links next to `.active`

十六、nextUntil

由目标元素到指定元素之间包含目标元素的元素集合

Syntax

.nextUntil(selector)
.nextUntil(selector, filter)
.nextUntil(selector, filter, options)
.nextUntil(element)
.nextUntil(element, filter)
.nextUntil(element, filter, options)

Usage

cy.get('div').nextUntil('.warning') // Yield siblings after 'div' until '.warning'

十七、not

获取目标元素除去指定元素所剩的元素合集

Syntax

.not(selector)
.not(selector, options)

Usage

cy.get('input').not('.required') // Yield all inputs without class '.required'

十八、parent

获取目标元素的父元素

Syntax

.parent()
.parent(selector)
.parent(options)
.parent(selector, options)

Usage

cy.get('header').parent() // Yield parent el of `header`

十九、parents

获取目标元素的祖籍元素合集

Syntax

.parents()
.parents(selector)
.parents(options)
.parents(selector, options)

Usage

cy.get('aside').parents()  // Yield parents of aside

二十、parentsUntil

由目标元素的父级到指定元素之间包含目标元素的元素集合

Syntax

.parentsUntil(selector)
.parentsUntil(selector, filter)
.parentsUntil(selector, filter, options)
.parentsUntil(element)
.parentsUntil(element, filter)
.parentsUntil(element, filter, options)

Usage

cy.get('p').parentsUntil('.article') // Yield parents of 'p' until '.article'

二十一、prev

获取目标元素的前一个元素

Syntax

.prev()
.prev(selector)
.prev(options)
.prev(selector, options)

Usage

cy.get('tr.highlight').prev() // Yield previous 'tr'

二十二、prevAll

获取目标元素之前的所有同级元素

Syntax

.prevAll()
.prevAll(selector)
.prevAll(options)
.prevAll(selector, options)

Usage

cy.get('.active').prevAll() // Yield all links previous to `.active`

二十三、prevUntil

目标元素之前到指定元素之间包含目标元素的元素集合

Syntax

.prevUntil(selector)
.prevUntil(selector, filter)
.prevUntil(selector, filter, options)
.prevUntil(element)
.prevUntil(element, filter)
.prevUntil(element, filter, options)

Usage

cy.get('p').prevUntil('.intro') // Yield siblings before 'p' until '.intro'

二十四、root

获取根结点

Syntax

cy.root()
cy.root(options)

Usage

cy.root()   // Yield root element <html>
cy.get('nav').within(($nav) => {
  cy.root()  // Yield root element <nav>
})

二十五、siblings

获取同级DOM元素

Syntax

.siblings()
.siblings(selector)
.siblings(options)
.siblings(selector, options)

Usage

cy.get('td').siblings()           // Yield all td's siblings
cy.get('li').siblings('.active')  // Yield all li's siblings with class '.active'

二十六、title

获取title

Syntax

cy.title()
cy.title(options)

Usage

cy.title()    // Yields the documents title as a string

二十七、url

获取url

Syntax

cy.url()
cy.url(options)

Usage

cy.url()    // Yields the current URL as a string

二十八、shadow

获取shadow节点

Syntax

.shadow(selector)
.shadow(selector, options)

Usage

cy.get('.shadow-host').shadow()