javascript - 如何使用 cypress.io 检查嵌套的阴影元素

标签 javascript automated-tests cypress shadow-dom

如何定位位于嵌套 shadow DOM 内的搜索框?
enter image description here
到目前为止,我已经尝试了几种不同的方法来定位,下面是其中之一,但没有奏效:
定位器:

//Shadow roots
const SDW_MAINAPP_G1 = "main-app"
const SDW_VOYAGETOPBAR_G2A = "voyage-topbar"
const SDW_VOYAGEPANEL_G2B = "voyage-float-panel"
const SDW_VESSELLIST_G3B = "voyage-vessel-list"
const SDW_VOYAGEFILTER_G4B1 = "voyage-filter"
const SDW_LISTSORT_G4B2 = "voyage-vessel-list-sort"

//Left Panel - Search Box
const INP_SEARCH_VESSEL = "#filter"
实际代码:
class SearchComponents {
    static validateSearchBar() {
         cy.get(SDW_MAINAPP_G1)
        .shadow()
        .find(SDW_VOYAGEPANEL_G2B)
        .find(SDW_VESSELLIST_G3B)
        .find(SDW_VOYAGEFILTER_G4B1)
        .find(INP_SEARCH_VESSEL)
        .should('be.visible')
        .should('be.enabled')
    }
   //...
   }
测试运行器中的错误:
enter image description here

最佳答案

嵌套的 shadow-root 使得很难确定应该添加 .shadow() 命令的位置,但是您可以在 config (cypress.json) 中全局启用 shadow DOM 搜索

includeShadowDom

Whether to traverse shadow DOM boundaries and include elements within the shadow DOM in the results of query commands (e.g. cy.get())


cypress.json
{
  ...
  includeShadowDom: true
}

关于javascript - 如何使用 cypress.io 检查嵌套的阴影元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67157491/

相关文章:

selenium - 如果单元格的 xpath 不匹配,如何忽略从单元格获取表格文本

javascript - 使用 before 钩子(Hook)时 cy.get() 找不到注册的别名

jquery - TS2339 : Property 'username' does not exist on type 'JQuery<HTMLElement>' . 使用 Cypress 夹具时

javascript - 无限生成幻灯片但不循环

javascript - 动态创建多个具有唯一名称的 wavesurfer 对象

javascript - 从 javaScript 访问 native windows api

node.js - Cypress : how to forward response body JSON to be used on headers

javascript - 从外部 iframe 启动全屏

java - 如何使用 selenium webdriver(Java) 从从谷歌地图 api 获取结果的字段中选择位置

selenium - 寻找开源网络测试自动化框架