我刚刚开始学习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>
答案 0 :(得分:2)
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);
});