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

前端之家收集整理的这篇文章主要介绍了javascript-TestCafé:验证是否显示了所有类别以及总分 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

HTML

  1. <html>
  2. <head>
  3. <Meta charset="utf-8">
  4. <title>QA Engineer</title>
  5. <link href="../css/main.css" rel="stylesheet" type="text/css">
  6. </head>
  7. <body>
  8. <h1>The Best QA Engineer Ever</h1>
  9. <ul>
  10. <li>
  11. <strong>Awesome Quotes</strong>
  12. <ul>
  13. <li><span>Excellent time to become a missing person.</span> (<span class="score">63</span>)</li>
  14. <li><span>Beware of low-flying butterflies.</span> (<span class="score">36</span>)</li>
  15. <li><span>I love deadlines. I love the whooshing sound they make as they fly by.</span> (<span class="score">89</span>)</li>
  16. <li><span>Nothing so needs reforming as other people&#39;s habits.</span> (<span class="score">93</span>)</li>
  17. <li><span>Do something unusual today. Pay a bill.</span> (<span class="score">91</span>)</li>
  18. </ul>
  19. </li>
  20. <li>
  21. <strong>Famous Quotes</strong>
  22. <ul>
  23. <li><span>If your life was a horse,you&#39;d have to shoot it.</span> (<span class="score">83</span>)</li>
  24. <li><span>You have taken yourself too serIoUsly.</span> (<span class="score">37</span>)</li>
  25. <li><span>You have the capacity to learn from mistakes. You&#39;ll learn a lot today.</span> (<span class="score">83</span>)</li>
  26. <li><span>A classic is something that everyone wants to have read and nobody wants to read.</span> (<span class="score">89</span>)</li>
  27. <li><span>Yes there is a lot of people doing a great job out there.</span> (<span class="score">44</span>)</li>
  28. </ul>
  29. </li>
  30. </ul>
  31. Total score: 708
  32. </body>
  33. </html>
最佳答案
  1. import { Selector,ClientFunction } from 'testcafe';
  2. fixture`Login`.page('http://localhost:8080');
  3. const scoresSelector = Selector('.score');
  4. test('test',async t => {
  5. let calculatedTotal = 0;
  6. let currentscore = 0;
  7. let currentscoreSelector = null;
  8. const scoreElementCount = await scoresSelector.count;
  9. for(let i = 0; i < scoreElementCount; i++) {
  10. currentscoreSelector = scoresSelector.nth(i);
  11. currentscore = parseInt(await currentscoreSelector.textContent);
  12. calculatedTotal += currentscore;
  13. }
  14. const expectedTotal = await ClientFunction(() => {
  15. debugger;
  16. var text = document.body.lastChild.nodeValue;
  17. var value = text.substring(text.indexOf(':') + 1);
  18. return parseInt(value);
  19. })();
  20. await t.expect(calculatedTotal).eql(expectedTotal);
  21. });

猜你在找的JavaScript相关文章