页面标题使用 TestCafe

问题描述

我通过 testcafe 创建了一个简单的测试,用于检查 Google 主页是否具有适当的页面标题。这里的页面标题是指位于 <head><title>Google</title></head> 中的标题文本 但是当我通过使用 t.debug() 在本地运行它时,我看到页面标题显示随机自动生成的文本而不是真实的页面标题

这是我的测试:

fixture("firstTest")
    .page("https://www.google.com")

test("home page should have a title",async t => {
    await t.debug()
        expect(await t.title).toEqual('Google')
    });

错误信息是:ReferenceError: expect is not defined

分享为什么会发生这种情况的任何想法。

Google page title during the test

解决方法

这种行为是有效的。为了运行测试,TestCafe 使用代理重写 URL (https://testcafe.io/documentation/402631/why-testcafe#page-proxying)。使用 JavaScript 获取页面标题将返回实际值。

请看下面的例子:

import from 'testcafe';

fixture("firstTest")
   .page("https://www.google.com")

test("home page should have a title",async t => {
   await t.expect(Selector("title").innerText).eql('Google')
});