testcafe中是否可以获取特定页面元素的加载时间?

问题描述 投票:0回答:1

假设我导航到页面,并且我希望某些页面元素在特定持续时间内显示。如何获取特定页面元素的加载时间?

我已经尝试在命令行参数中将visibleCheckCheck选项与选择器超时一起使用。还尝试了超时超时。这些都没有按预期工作。

try {
        await loginPage.signInToPortal()
        await loginPage.login( 'xxxx','yyyy')
        await Selector('div').withText('Something').with({ visibilityCheck: true });
    } catch (e) {
        logger.error("Testcase C111111 failed...")
        throw(e)
    }

OR

try {
        await loginPage.signInToPortal()
        await loginPage.login( 'xxxx','yyyy')
        const appLabel = Selector('div').withText('Something').with({ visibilityCheck: true });
        await t.expect(appLabel.innerText).contains('Something', { timeout: 5000 });
    } catch (e) {
        logger.error("Testcase C111111 failed...")
        throw(e)
    }
testcafe
1个回答
0
投票

据我了解,您希望在一段时间后检查该元素是否存在并具有一些内部文本。如果经过了时间,但是元素不存在或没有一些文本,则您希望测试失败。

您的方法通常是正确的,但我认为您在这里不需要visibilityCheck选项。

由于我不知道您的项目的工作原理,因此我创建了一个示例。让我展示一下并解释超时在TestCafe中的工作方式。

页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>

    setTimeout(() => {
        var div = document.createElement('div');

        document.body.appendChild(div);

        setTimeout(() => {
            div.innerHTML = 'test';
        }, 6000);
    }, 6000);

</script>
</body>
</html>

测试代码:

import { Selector } from 'testcafe';

fixture `fixture`
    .page `../pages/index.html`;

const selector = Selector('div', { timeout: 4000 });

test(`Recreate invisible element and click`, async t => {
    await t.expect(selector.innerText).eql('test', 'error message', { timeout: 13000 });
});

[这里,我有一个div元素,仅在6s之后出现。在此期间,断言检查元素是否存在。执行此代码:Selector('div', { timeout: 4000 });。由于4s小于6s,因此测试失败,因为它在超时期间找不到元素。

但是,如果我将超时更改为7s,则Selector('div', { timeout: 7000}); TestCafe会找到div并开始等待,直到div的内部文本正确为止。

现在断言超时为13秒。 13s大于6s(元素显示所需的时间)+ 6s(元素具有正确的文本所需的时间),因此断言将成功。但是,如果我将声明超时从13s更改为11s,它将失败。

© www.soinside.com 2019 - 2024. All rights reserved.