不管是Selenium还是Cypress,元素定位是做UI自动化的基础操作。这次我们来了解下Cypress元素定位选择器。
01
专有选择器
如果我的应用程序使用动态类或动态id,我如何选择或查询元素?此处就可以使用此类选择器,Cypressr提供了data-*属性,包含下面三个定位器:
- data-cy
- data-test
- data-testid
以上三个定位器仅用于测试,如下
#为email输入框添加data-cy属性
type="email" class="form-control action-email" id="email1" placeholder="Email",data-cy="email">
在测试用例中,采用如下方法与元素交互
cy.get('[data-cy=email]').type('mrjade@email.com')
02
id选择器
"query-btn" class="query-btn btn btn-primary">
Button
cy.get('#query-btn').ckick
03
class选择器
cy.get('.query-btn').click
04
attribute属性选择器
"main" class="btn btn-large" name="submission" role="button" data-cy="submit">
Submit
cy.get('#main[role=button]').click()
05
:nth-chile(n)选择器
说明:匹配属于其父元素的第N个子元素
"traversal-list">mrjadecnblogsTesterRoadcypress
cy.get('li:nth-child(1)').click
06
Cypress.$选择器
说明:Cypress允许使用jQuery选择器Cypress.$(selector)直接定位。
- 语法
Cypress.$(selector)
// other proxied jQuery methods
Cypress.$.Event
Cypress.$.Deferred
Cypress.$.ajax
Cypress.$.get
Cypress.$.getJSON
Cypress.$.getScript
Cypress.$.post
- 用法
Cypress.$('#query-btn')
往期推荐
前端自动化测试框架Cypress入门
前端自动化测试框架Cypress入门(二)
前端自动化测试框架Cypress入门(三)
![bffc8c199be4329df8519112ccb8b699.png](https://i-blog.csdnimg.cn/blog_migrate/cc11ebebaa2af88240eaf366c4f2c038.png)
![68b637cd0fbf27322ab2e227292705fe.png](https://i-blog.csdnimg.cn/blog_migrate/3fde54fe350e4efa82b8c7f44ce9567b.png)
![3b4be1c9ecfe12e0f6fbe7b0f2a797b4.png](https://i-blog.csdnimg.cn/blog_migrate/1161717cfa700bd3021540a35c986825.png)
![45963be107ad13d2c49cd69643f8a940.png](https://i-blog.csdnimg.cn/blog_migrate/6831e2753c0051bf46ecf8fff24f6834.png)