简体   繁体   English

Cypress invoke 不执行隐藏元素交互的“show”方法

[英]Cypress invoke doesn't execute the "show" method for hidden elements interaction

I have a situation where I want to open the sub-submenu of the main menu which has layout build using nested "ul-li ".我有一种情况,我想打开主菜单的子菜单,该子菜单使用嵌套的“ul-li”构建布局。

sub-menu gets open when mouse is hover to the li which has Children (ul-li) as below当鼠标悬停到具有子项 (ul-li) 的 li 时,子菜单会打开,如下所示

<ul class="dropdown"> 
  <li>/<li>
  <li>/<li>
  <li class="dropdown-menu">
      <a> Target Menu </a>
      <ul class="dropdown"> 
        <li class="dropdown-sub-menu"><a> Sub Menu 1</a></li>
        <li class="dropdown-sub-menu"><a> Sub Menu 2</a></li>
       <li class="dropdown-sub-menu"> <a> Sub Menu 3</a></li>
     </ul>
  </<li>
</ul>

Here, My requirement is to mouse over to "Target Menu" which will open its sub-menu and then want to trigger the click the event of "Sub Menu 1/2/3" respectively.在这里,我的要求是将鼠标悬停在“目标菜单”上,这将打开其子菜单,然后分别触发“子菜单 1/2/3”的单击事件。

I have gone through the documents of cypress for handling this feature.我已经浏览了 cypress 的文档来处理这个功能。 As .hover() feature is not available with cypress.由于 .hover() 功能在 cypress 中不可用。

https://docs.cypress.io/api/commands/hover.html# https://docs.cypress.io/api/commands/trigger.html#Syntax https://docs.cypress.io/guides/core-concepts/interacting-with-elements.html#Actionability https://docs.cypress.io/api/commands/hover.html# https://docs.cypress.io/api/commands/trigger.html#Syntax https://docs.cypress.io/guides/core -concepts/interacting-with-elements.html#Actionability

I have also tried the below commands but it fails too.我也试过下面的命令,但它也失败了。 Nothing hover or mouseover effect occurs and due to which automation breaks when it tries to click the "hidden Li (sub Menu 1/2/3)".没有任何悬停或鼠标悬停效果发生,因此当它尝试单击“隐藏的 Li(子菜单 1/2/3)”时自动化会中断。

cy.get("target the li/a").trigger("mouseover") cy.get("瞄准li/a").trigger("mouseover")

One of the blogs while searching I found that to Interact with hidden elements which gets visible on mouse hovering effect on some button or link, you have to use the cy.invoke as below which will execute the jquery "show" event and makes the hidden element visible and then you can click on the hidden elements.搜索时的博客之一我发现要与在某些按钮或链接上的鼠标悬停效果上可见的隐藏元素进行交互,您必须使用 cy.invoke 如下,它将执行 jquery“显示”事件并使隐藏元素可见,然后您可以单击隐藏的元素。 Unfortunately, that method too is not working as in Typescript when I write the below command it doesn't allow me to compile as "show" is not the valid function name.不幸的是,当我编写以下命令时,该方法也不能像在 Typescript 中那样工作,它不允许我编译,因为“show”不是有效的函数名称。

cy.get("li.dropdown-menu ul.dropdown").invoke("show") cy.get("li.dropdown-menu ul.dropdown").invoke("show")

Please guide for the possible solution related to this.请指导与此相关的可能解决方案。 Executing click command with { force: true } of the hidden element is the work around for this but is not a valid one though.使用隐藏元素的 { force: true } 执行单击命令是解决此问题的方法,但不是有效的。

Friends朋友们

Firstly, Thanks for all your time & suggestions.首先,感谢您的所有时间和建议。

I have found the work around to trigger the mouseover event and show the bootstrap submenu.我找到了触发鼠标悬停事件并显示引导程序子菜单的解决方法。 without using the { force: true }.不使用 { force: true }。

Solution that I implemented.我实施的解决方案。 Appreciated if anyone finds different and better solution.如果有人找到不同的更好的解决方案,我们将不胜感激。 Please do share.请分享。

Cypress.$($elem[0]).siblings("ul").show();

Using this I have manually makes the ul to show first and then execute the click event of the submenu.使用这个我手动让 ul 先显示,然后执行子菜单的点击事件。 For this I have tried为此,我已经尝试过

Cypress.$($elem[0]).hover(false, false);
Cypress.$($elem[0]).mouseover();
Cypress.$($elem[0]).trigger("hover");
Cypress.$($elem[0]).trigger("mouseover");

but above commands didn't work.但上面的命令不起作用。 So I move to manually trigger the hide/show event of jquery.所以我开始手动触发 jquery 的隐藏/显示事件。

Based on the comments, your issue seems to be with how menus are handled in Bootstrap.根据评论,您的问题似乎与 Bootstrap 中菜单的处理方式有关。

Bootstrap menus can be triggered with a special .dropdown() function added to jQuery. Bootstrap 菜单可以通过添加到 jQuery 的特殊.dropdown()函数来触发。 Since Cypress gives you native access to the DOM, you can just call that function from within your test.由于 Cypress 为您提供对 DOM 的本机访问,因此您只需从测试中调用该函数即可。 Here's how this would look in Cypress:这是 Cypress 中的样子:

cy.get('li.dropdown-menu a').then((elem) => {
    $(elem).dropdown('toggle');
});

hidden elements are not shown through invoke('show') because, you are using the locator of the grand parent of the hidden elements.隐藏元素不会通过 invoke('show') 显示,因为您正在使用隐藏元素的祖父的定位器。 Try using the immediate parent of the Hidden elements.尝试使用 Hidden 元素的直接父元素。 Then invoke('show') will do the mouse hover function.然后 invoke('show') 将执行鼠标悬停功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM