【问题标题】:WebdriverIO can't click on (Ionic) tabWebdriverIO 无法点击(离子)选项卡
【发布时间】:2019-07-12 22:20:07
【问题描述】:

我正在使用 WebdriverIO 在 Ionic (+ Angular) 应用程序上测试一些基本功能。框架设置工作正常,但我无法在某些 HTML 元素上找到click 的方法。例如,这是一些 HTML 示例:

<div class="tabbar show-tabbar" role="tablist" style="top: 166px; display: flex;">
  <a class="tab-button has-title has-title-only" href="#" role="tab" id="tab-t0-0" aria-controls="tabpanel-t0-0"
      aria-selected="true">
    <span class="tab-button-text">Blah</span>
    <div class="button-effect"></div>
  </a>
  <a class="tab-button has-title has-title-only" href="#" role="tab" id="tab-t0-1" aria-controls="tabpanel-t0-1"
      aria-selected="false">
    <span class="tab-button-text">Foo</span>
    <div class="button-effect"
        style="transform: translate3d(83px, -103px, 0px) scale(1); height: 240px; width: 240px; opacity: 0; transition: transform 395ms ease 0s, opacity 277ms ease 118ms;"></div>
  </a>
  <a class="tab-button has-title has-title-only" href="#" role="tab" id="tab-t0-2" aria-controls="tabpanel-t0-2"
      aria-selected="false">
    <span class="tab-button-text">Bar</span>
    <div class="button-effect"
        style="transform: translate3d(3px, -99px, 0px) scale(1); height: 240px; width: 240px; opacity: 0; transition: transform 395ms ease 0s, opacity 277ms ease 118ms;"></div>
  </a>
  <div class="tab-highlight animate" style="transform: translate3d(570px, 0px, 0px) scaleX(285);"></div>
</div>

这是一个超级简单的测试用例,我正在做它来测试功能:

import { expect } from "chai";

describe("Some Test", () => {
  const logingUrl: string = "url0";

  const appUrl: string = "url1";

  it("Some Test Again", () => {
    browser.url(logingUrl);
    browser.url(appUrl);
    const tab = $("#tab-t0-2");
    tab.click();
    expect(tab.getAttribute("aria-selected")).to.equal("true");
  });
});

..但是每次我运行它时,我都会收到一些奇怪的错误消息,指出该元素在某些时候是不可点击的。有什么线索吗?

[0-0] element click intercepted in "Some Test Again"
element click intercepted: Element <a class="tab-button has-title has-title-only" href="#" role="tab" id="tab-t0-2" aria-controls="tabpanel-t0-2" aria-selected="false">...</a> is not clickable at point (666, 170). Other element would receive the click: <ion-backdrop disable-activated="" role="presentation" tappable="" class="backdrop-no-tappable" style="opacity: 0.5;"></ion-backdrop>
  (Session info: headless chrome=75.0.3770.100)

【问题讨论】:

    标签: ionic-framework selenium-webdriver selenium-chromedriver webdriver-io


    【解决方案1】:

    这是经典的 Angular/Ionic 背景陷阱之一。

    让我们从错误消息开始:元素#tab-t0-2 在点(坐标)处不可点击,另一个元素会收到点击:ion-backdrop

    这告诉我们您的目标元素无法被点击,因为ion-backdrop 标签呈现在它上面。 ion-backdrop 组件是一个短动画(通常用于模态框),在这种情况下,背景的半暗化(opacity: 0.5)。

    ✖ 解决方案 1: 简单的反击方法?明确期望它消失,然后单击目标元素。

    it("Some Test Again", () => {
      browser.url(logingUrl);
      browser.url(appUrl);
    
      // Explicitly wait for the backdrop animation to disappear:
      const backdrop = $('.backdrop-no-tappable');
      backdrop.waitForExist(5000, true, 'Backdrop still present');
    
      const tab = $("#tab-t0-2");
      tab.click();
      expect(tab.getAttribute("aria-selected")).to.equal("true");
    });
    

    ✖ 解决方案 2:您可以尝试的另一件事是单击 tab 元素,一旦它在 DOM 中完全可见且难以处理(这是一种最佳实践):

      const tab = $("#tab-t0-2");
      tab.waitForDisplayed(5000);
      // For 'wdio-v4' users: 
      // tab.waitForVisible(5000);
      tab.click();
      expect(tab.getAttribute("aria-selected")).to.equal("true");
    

    【讨论】:

    • 谢谢,@iamdanchiv!这对我有帮助。我最终使用了“合并”版本:$("ion-backdrop.backdrop-no-tappable").waitForExist(undefined, true) ;)
    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多