作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在两个单独的测试类中运行 TestCafe,在两个单独的装置中,在两个单独的测试中,测试两个不同的应用程序页面。
我注意到,在这些测试中,当我通过 ClientFunction
询问 window.document
对象时,根据运行顺序,我会得到不同的值。
例如我的test1.js
import { Selector, ClientFunction } from 'testcafe';
fixture `Homepage`
.page `http://mypage.com`;
test('Test 1', async t => {
const getBodyHeight = ClientFunction(() => window.document.body.scrollHeight);
console.log(await getBodyHeight()) // 800px
});
mytest2.js
import { Selector, ClientFunction } from 'testcafe';
fixture `Dashboard`
.page `http://mypage.com/dashboard`;
test('Test 2', async t => {
const getBodyHeight = ClientFunction(() => window.document.body.scrollHeight);
console.log(await getBodyHeight()) // 1200px
});
如果我使用 npm run testcafe -- firefox:headless mytest*.js
运行它们并且顺序是从小高度到大高度,控制台将记录:
...
800
...
1200
但是,如果我以相反的方式运行它们(从较大的高度到较小的高度),我会得到:
...
1200
...
1200
就好像 document.body
被拉伸(stretch)到最大值并且没有返回。
有没有办法使用 ClientFunction(..)
或其他一些方法来正确重置这些值?
最佳答案
这个带有 ClientFunction(() => window.document.body.scrollHeight)
的测试场景看起来是正确的。我准备了一个小例子,我无法重现这种行为。以下示例是否按您的预期工作?
index1.html
<html>
<head></head>
<body>
</body>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
test
</html>
index2.html
<html>
<head></head>
<body>
</body>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
test
</html>
test1.js
import { Selector, ClientFunction } from 'testcafe';
fixture `My Fixture`
.page `./index1.html`;
test('test 1', async (t) => {
const getBodyHeight = ClientFunction(() => window.document.body.scrollHeight);
console.log('test 1 body.scrollHeight', await getBodyHeight());
});
test2.js
import { Selector, ClientFunction } from 'testcafe';
fixture `My Fixture`
.page `./index2.html`;
test('test 2', async (t) => {
const getBodyHeight = ClientFunction(() => window.document.body.scrollHeight);
console.log('test 2 body.scrollHeight', await getBodyHeight());
});
结果:
testcafe "firefox:headless"测试/test1.js 测试/test2.js
My Fixture
test 1 body.scrollHeight 932
√ test 1
My Fixture
test 2 body.scrollHeight 1762
√ test 2
2 passed (0s)
testcafe "firefox:headless"测试/test2.js 测试/test1.js
My Fixture
test 2 body.scrollHeight 1762
√ test 2
My Fixture
test 1 body.scrollHeight 932
√ test 1
2 passed (0s)
关于javascript - 如何重置使用 ClientFunction 检索到的 DOM 元素的 scrollHeight 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520225/
我是一名优秀的程序员,十分优秀!