【问题标题】:Passing arguments to TestCafe Selector in page model在页面模型中将参数传递给 TestCafe 选择器
【发布时间】:2019-12-03 17:49:58
【问题描述】:

我想将字符串传递给页面模型中的对象以供.withText 选择使用。我可以使用该页面上的函数来执行此操作,它可以工作,但我必须以不同于页面上定义的对象的方式调用它。

我尝试使用FilterFn.filter(和.find),但没有成功。也许这正是我需要的,但需要帮助,或者我只是想多了。

示例页面模型摘录

export class AreaPage {
    public pageTitle = Selector("#container h1").withText("My Area Title");

    public async pageSubtitle(areaName: string) {
        return await Selector("#container h2").withText(areaName);
    }
}

示例测试摘录

test("test some code", async (t) => {
    await t
    .click(areaPage.pageTitle)
    .click(await areaPage.PageSubtitle("my subtitle"));
}

当然,我可能不想“单击”这些标题,但它展示了调用这些页面组件的方式的不同之处。我也知道我可以在测试中捕获对象并进行验证,但这会破坏我正在尝试做的部分事情。

如上所述,它可以工作,但我希望我们的 QA 人员能够以相同的方式使用它们(即,在测试的最后一行中没有嵌入“await”)。如果我删除等待,我会收到错误:

Argument of type 'Promise<Selector>' is not assignable to parameter of type 'string | Selector | NodeSnapshot | SelectorPromise | ((...args: any[]) => Node | Node[] | NodeList | HTMLCollection)'.
  Type 'Promise<Selector>' is missing the following properties from type 'SelectorPromise': childElementCount, childNodeCount, hasChildElements, hasChildNodes, and 51 more.ts(2345)

(......这只是让我的大脑抽筋)

我如何编写页面对象 pageSubtitle 以接受参数但在其他方面像 pageTitle 对象一样发挥作用?

【问题讨论】:

    标签: testing automated-tests parameter-passing testcafe custom-selectors


    【解决方案1】:

    您正在等待 Selector 承诺两次 - 这会导致错误。 尝试如下修改您的测试和页面模型:

    页面模型:

    export class AreaPage {
        public pageTitle = Selector("#container h1").withText("My Area Title");
    
        public pageSubtitle(areaName: string) {
            return Selector("#container h2").withText(areaName);
        }
    }
    

    测试:

    test("test some code", async (t) => {
        await t
            .click(areaPage.pageTitle)
            .click(pageModel.pageSubtitle("my subtitle"));
    }
    

    【讨论】:

    • 谢谢。做到了。我必须在某处添加 async/await 以尝试解决另一个不再是问题的问题......然后在我的代码中传播它。 :-\
    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多