gpt4 book ai didi

javascript - 如何重置使用 ClientFunction 检索到的 DOM 元素的 scrollHeight 值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:25 28 4
gpt4 key购买 nike

我在两个单独的测试类中运行 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());
});

结果:

  1. 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)
  1. 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)

另请参阅:ClientFunction Object

关于javascript - 如何重置使用 ClientFunction 检索到的 DOM 元素的 scrollHeight 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com