javascript - 类型错误 : Cannot read property 'createElement' of null

标签 javascript testing automated-tests e2e-testing testcafe

我正在尝试单击具有以下结构的侧边栏:

<div class=“sidebar”>
  <nav class=“subnav”>
    <ul>
      <li>
    <a class=“subnav-toggle”></a>
    <div>
      <ul>
        <li class=subnav-item>…</li>
        <li class=subnav-item>…</li>
        <li class=subnav-item>…</li>
      </ul>
    </div>
      </li>
      <li>…</li>
      <li>…</li>
      <li>…</li>
      <li>…</li>
    </ul>
  </nav>
</div>

我创建了以下页面模型:

(注意:我想获取导航栏上更高级别的菜单项,而不是第二级 <li> )

import { Selector } from 'testcafe';

export class PageModel {
  constructor () {
    this.firstItem = Selector('.subnav > ul > li').nth(0);
    this.secondItem = Selector('.subnav > ul > li').nth(1);
    ...
  }
}

最后,我的测试脚本如下:

import { PageModel } from 'myclass';

const a_item = new PageModel();
...
test ('Sample test', async t => {
  await t
    .hover(a_item.firstItem);
});

Testcafe 成功地将鼠标悬停在我选择的元素上。但是,我收到一条错误消息 TypeError: Cannot read property 'createElement' of null .

如何解决这个问题?

** 更新 **

我根据在控制台上输入的查询更新了查询。

document.querySelectorAll('.subnav > ul > li')
NodeList(8) [li, li, li, li, li, li, li, li]

最佳答案

使用您的选择器,

Selector('subnav').find('li > a').nth(0);

您选择的第一个 A 是“subnav”下某个 LI 的直接子级。

我认为您真正想要做的是找到“外部”列表的 li 元素,如下所示:

 Selector('.subnav > ul > li').nth(0);

关于javascript - 类型错误 : Cannot read property 'createElement' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55027064/

相关文章:

javascript - Ajax 函数用图像填充表格但不能用实时选择

python - python cv2 中 VideoCapture 的多个实例的问题

testing - 基于 Web 的项目的自动测试

selenium - [Ashot][Selenium] - 为 iOS 和 Android 截图

groovy - groovy 脚本中的测试步骤结果列表

Javascript 转换真/假值字符串文本

javascript - 匹配字符串的起始字符

javascript - 设置状态后无法编辑 inputText [react-native]

python - 单元测试 - 导入错误

testing - Gradle:如何在控制台实时显示测试结果?