前端自动化测试框架Cypress(二十)–聚焦元素focus、focused

.focus()—元素聚焦
语法

1
2
.focus()
.focus(options)

参数

1
2
log true    在命令日志中显示命令
timeout defaultCommandTimeout   超时时间

实例一

1
cy.get('input').first().focus() // 聚焦到第一个input元素

实例二

1
cy.get('[type="input"]').focus()

实例三

1
cy.get('[name="comment"]').focus()

.focused()—获取当前聚焦的元素
语法

1
2
cy.focused()
cy.focused(options)

参数

1
2
log true    在命令日志中显示命令
timeout defaultCommandTimeout   超时时间

实例一

1
2
3
cy.focused().then(($el) => {
  // do something with $el
})

实例二

1
cy.focused().should('have.attr', 'name', 'username')

实例三

1
cy.focused().should('have.attr', 'name').and('eq', 'num')