【问题标题】:Testcafe Selector does not get element in React ModalTestcafe Selector 没有在 React Modal 中获取元素
【发布时间】:2019-03-07 16:22:22
【问题描述】:

我正在使用 testcafe 来驱动一个网站。我创建了一个页面模型,我可以点击我已经创建的按钮。

但是,我的页面使用 React 创建了一个模态对话框,该对话框只有在我单击按钮后才可见。我可以使用带有document.querySelectorAll('.modal-footer > button')[1] 的浏览器控制台获取元素。

所以在我的页面模型中我使用了Selector('.modal-footer > button').nth(1);,并且我还尝试使用here 中的语法创建一个选择器。 在这两种情况下,testcafe 都无法找到该元素,我最终得到了 TypeError: Cannot read property 'createElement' of null 错误。

这是我的代码:

// page model
import { Selector } from 'testcafe';

export class PageModel {
  constructor () {
    ... // a bunch of buttons
    this.modalButton = Selector('.modal-footer > button').nth(1);
  }
}

和我的测试

// test script
import { PageModel } from 'page_model'

const pagemodel = new PageModel();

fixture ...

test('My Test', async t => {
  await t.click(pagemodel.abutton);
  await t.click(pagemodel.openDialog); // the modal dialog opens

  await t.click(pagemodel.modalButton) // <-- Here's where I get the error
});

使用仅在按钮可见时单击按钮的 If 语句 (if (modalButton.exists)) 似乎有效。但是当我单击后该按钮消失时,我仍然会收到错误消息。

有什么建议吗?

【问题讨论】:

    标签: javascript reactjs automated-tests e2e-testing testcafe


    【解决方案1】:

    我能够用 try-catch 块解决。我认为浏览器中存在一些导致测试失败的竞争条件。

    const max_retries = 3
    let count = 0;
      while (count < max_retries) {
        try {
          await t
            .click(pagemodel.modalButton);
          break;
        }
        catch (e) {
          console.error('An error occurred, trying again...');
          count++;
          if (count === retries) {
            throw e;
          }
        }
      }
    

    【讨论】:

    • 您提供的错误原因尚不清楚。我想更深入地研究它。然而。没有样本很难确定问题的原因。请使用以下表单 github.com/DevExpress/testcafe/issues/… 在 TestCafe GitHub 存储库中创建一个单独的问题。如果您创建一个示例示例来演示并分享它,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2012-07-23
    • 2013-08-03
    • 1970-01-01
    • 2019-12-25
    • 2011-08-28
    • 1970-01-01
    相关资源
    最近更新 更多