Skip to main content

hash

获取当前活动页面的 URL 哈希值。

info

这是 cy.location('hash') 的别名

语法

cy.hash()
cy.hash(options)

用法

正确用法

cy.hash() // 获取 URL 哈希值

参数

options (Object)

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

cy.hash( options )

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

生成结果 了解主题管理

  • 当当前 URL 包含哈希值时,cy.hash() 返回当前 URL 的哈希值(包括 # 字符)。
  • 当当前 URL 不包含哈希值时,cy.hash() 返回空字符串。
  • cy.hash() 是一个查询命令,可以安全地链式调用其他命令。

示例

无参数

断言哈希值为 #/users/1,给定远程 URL: http://localhost:8000/app/#/users/1

// 返回 #/users/1
cy.hash().should('eq', '#/users/1') // => true

通过正则表达式断言哈希值匹配

<ul id="users">
<li>
<a href="#/users/8fc45b67-d2e5-465a-b822-b281d9c8e4d1">Fred</a>
</li>
</ul>
cy.get('#users li').find('a').click()
cy.hash().should('match', /users\/.+$/) // => true

规则

要求 了解命令链

  • cy.hash() 需要链式调用 cy

断言 了解断言

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

超时设置 了解超时机制

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

命令日志

断言哈希值匹配 #users/new

cy.hash().should('eq', '#users/new')

以上命令将在命令日志中显示为:

hash 命令日志

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

hash 控制台日志

另请参阅