如果想从头学起Cypress,可以看下面的系列文章哦

 

 

作用

  • 获取页面当前的 url
  • 等价于 cy.location('href') 

 

语法格式

cy.url()
cy.url(options)

options:只有 timeout 和 log,不再展开讲了

 

正确写法

cy.url()

 

实际栗子

测试文件代码

Cypress系列(32)- url() 命令详解_Cypress框架

 

测试结果

Cypress系列(32)- url() 命令详解_Cypress框架_02

 

url() 命令在 console 会输出啥

Cypress系列(32)- url() 命令详解_Cypress框架_03

 

location(href) 命令在 console 会输出啥

Cypress系列(32)- url() 命令详解_Cypress教程_04

 

结论

  • 可以看到 cy.url() 和 cy.location('href') 是等价效果的
  • 其实还有更多等价的写法,看下面

 

三种等价的断言方式

测试文件代码

Cypress系列(32)- url() 命令详解_Cypress教程_05

 

测试结果

Cypress系列(32)- url() 命令详解_前端_06

 

结尾

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=12vd92hxgwgj1