javascript-TestCafé:验证是否显示了所有类别以及总分

我刚刚开始学习TestCafé.我有一个网页,我想使用TestCafé对它进行测试以:

>确认显示所有类别及其报价,没有多余的报价,没有丢失的报价.
>确认“总分:”是所有报价分的总和.

HTML

<html>
  <head>
    <Meta charset="utf-8">
    <title>QA Engineer</title>
    <link href="../css/main.css" rel="stylesheet" type="text/css">
  </head>

  <body>
    <h1>The Best QA Engineer Ever</h1>
    <ul>
      <li>
        <strong>Awesome Quotes</strong>
        <ul>
          <li><span>Excellent time to become a missing person.</span> (<span class="score">63</span>)</li>
          <li><span>Beware of low-flying butterflies.</span> (<span class="score">36</span>)</li>
          <li><span>I love deadlines. I love the whooshing sound they make as they fly by.</span> (<span class="score">89</span>)</li>
          <li><span>nothing so needs reforming as other people&#39;s habits.</span> (<span class="score">93</span>)</li>
          <li><span>Do something unusual today.  Pay a bill.</span> (<span class="score">91</span>)</li>
        </ul>
      </li>
      <li>
        <strong>Famous Quotes</strong>
        <ul>
          <li><span>If your life was a horse,you&#39;d have to shoot it.</span> (<span class="score">83</span>)</li>
          <li><span>You have taken yourself too serIoUsly.</span> (<span class="score">37</span>)</li>
          <li><span>You have the capacity to learn from mistakes.  You&#39;ll learn a lot today.</span> (<span class="score">83</span>)</li>
          <li><span>A classic is something that everyone wants to have read and nobody wants to read.</span> (<span class="score">89</span>)</li>
          <li><span>Yes there is a lot of people doing a great job out there.</span> (<span class="score">44</span>)</li>
        </ul>
      </li>
    </ul>
    Total score: 708
  </body>
</html>
最佳答案
import { Selector,ClientFunction } from 'testcafe';

fixture`Login`.page('http://localhost:8080');

 const scoresSelector = Selector('.score');

 test('test',async t => { 
   let calculatedTotal      = 0;
   let currentscore         = 0;
   let currentscoreSelector = null;
   const scoreElementCount  = await scoresSelector.count;   

   for(let i = 0; i < scoreElementCount; i++) {
       currentscoreSelector = scoresSelector.nth(i);
       currentscore         = parseInt(await currentscoreSelector.textContent);

       calculatedTotal += currentscore;
   }

   const expectedTotal = await ClientFunction(() => {
       debugger;
       var text = document.body.lastChild.nodeValue;
       var value = text.substring(text.indexOf(':') + 1);

       return parseInt(value);
   })();

   await t.expect(calculatedTotal).eql(expectedTotal);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据复制操作的两种方式。‌在聊深浅拷贝之前咱得了解一下js中的两种数据类型:
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:重建二叉树、反向输出链表每个节点 题目 重建二叉树: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列 {1,2,4,7,3,5,6,8} 和中序遍历序列 {
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须了解的知识点,现在迎来了ES6+的时代,因为箭头函数的出现,所以感觉有必要对 this 问题梳理一下,所以刚好总结一下JavaScript中this指向的问题。
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)