Cypress系列(35)- root() 命令详解

简介: Cypress系列(35)- root() 命令详解

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

https://www.cnblogs.com/poloyy/category/1768839.html

 

作用


获取当根元素

 

语法格式


cy.root()
cy.root(options)


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

 

正确写法


// 根元素是<html>
cy.root()
// 根元素是<nav>
cy.get('nav').within(($nav) => {
  cy.root() 
})


实际栗子


html 代码

<form id="contact">
    <input type="text" name="email">
    <input type="text" name="password">
    <button type="submit">Send</button>
</form>


image.png

这里调用了两次 root()

  1. 直接通过 cy调用
  2. .within() 回调函数中获取根元素

 

测试结果

image.png

而这里返回的是 form 元素

 

结尾

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

相关文章
|
缓存
Cypress系列(39)- reload() 命令详解
Cypress系列(39)- reload() 命令详解
391 0
Cypress系列(39)- reload() 命令详解
|
测试技术
Cypress系列(81)- clearCookie() 命令详解
Cypress系列(81)- clearCookie() 命令详解
150 0
Cypress系列(81)- clearCookie() 命令详解
|
JavaScript
Cypress系列(76)- cloest() 命令详解
Cypress系列(76)- cloest() 命令详解
274 0
Cypress系列(76)- cloest() 命令详解
|
索引
Cypress系列(74)- each() 命令详解
Cypress系列(74)- each() 命令详解
282 0
Cypress系列(74)- each() 命令详解
|
JSON 数据格式
Cypress系列(95)- writeFile() 命令详解
Cypress系列(95)- writeFile() 命令详解
208 0
Cypress系列(95)- writeFile() 命令详解
Cypress系列(73)- within() 命令详解
Cypress系列(73)- within() 命令详解
239 0
Cypress系列(73)- within() 命令详解
|
测试技术
Cypress系列(82)- clearCookies() 命令详解
Cypress系列(82)- clearCookies() 命令详解
134 0
Cypress系列(82)- clearCookies() 命令详解
Cypress系列(80)- setCookie() 命令详解
Cypress系列(80)- setCookie() 命令详解
117 0
Cypress系列(80)- setCookie() 命令详解
Cypress系列(79)- getCookies() 命令详解
Cypress系列(79)- getCookies() 命令详解
200 0
Cypress系列(79)- getCookies() 命令详解
Cypress系列(78)- getCookie() 命令详解
Cypress系列(78)- getCookie() 命令详解
261 0
Cypress系列(78)- getCookie() 命令详解