我刚刚开始学习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'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'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'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);
});