Skip to main content
Cypress应用

document

获取当前活动页面的 window.document 对象。

语法

cy.document()
cy.document(options)

用法

正确用法

cy.document() // 返回 window.document 对象

参数

options (Object)

传入一个选项对象来改变 cy.document() 的默认行为。

选项默认值描述
logtrue命令日志中显示该命令
timeoutdefaultCommandTimeout等待 cy.document() 解析的超时时间,超过则超时

生成结果 了解主题管理

  • cy.document() 返回 window.document 对象。
  • cy.document() 是一个查询命令,可以安全地链式调用其他命令。

示例

无参数

获取 document 并执行操作

cy.document().then((doc) => {
// 操作 document 元素
})

对 document 进行断言

cy.document().its('contentType').should('eq', 'text/html')

规则

要求 了解命令链

  • cy.document() 必须通过 cy 链式调用。

断言 了解断言

  • cy.document() 会自动 重试 直到所有链式断言通过

超时设置 了解超时机制

  • cy.document() 可能会因等待添加的断言通过而超时。

命令日志

获取 document

cy.document()

上述命令在命令日志中显示为:

命令日志中的 document

当点击命令日志中的 document 时,控制台会输出以下内容:

console.log 输出的 document

另请参阅