选择器找不到测试控制器

时间:2019-07-27 01:59:52

标签: typescript testcafe

我需要将测试控制器传递给我的选择器。我是Typescript和Testcafe的新手。以下是我的文件:

Locator.ts:

import {Selector, t} from 'testcafe';

export default class Locators {
    elementWithId(id: string) {
        const element = Selector(id => {
            return document.getElementById(id);
        }, {
            boundTestRun: t
        });
        const boundelement = element(id)
        return boundelement      
    };


};

LoginFlow.ts:

import {t} from 'testcafe';
import Locators from './Locators';

const locate = new Locators()

export default class LoginFlow {
    loginEmail : any;
    loginPassword: any;
    loginButton: any;
    searchBar: any;

    constructor(){
        this.loginEmail = locate.elementWithId('email'); 
        this.loginPassword = locate.elementWithId('password');
        this.loginButton = locate.elementWithId('login');
        this.searchBar = locate.elementWithId('searchinput');
    }

    async loginDispatch() {
        await t
        .setPageLoadTimeout(10000)  // 5 seconds
        .typeText(this.loginEmail, 'email')
        .typeText(this.loginPassword, 'password')
        .click(this.loginButton)
        .expect(this.searchBar)
        .ok()
    }
}

Test.ts:

import {t} from 'testcafe';
import LoginFlow from "./PageObjects/LoginFlow";

const lf = new LoginFlow()
fixture('First UI Test')
    .page('<page_url>');

test("Get Social Compose Page", async (t) => {

    await lf.loginDispatch()

});

我当前遇到的错误是: “ boundTestRun”选项值应该是测试控制器。

我尝试使用.with({boundTestRun:t})在Locators.ts中声明边界元素,但抱怨element(id)不是函数。

1 个答案:

答案 0 :(得分:2)

boundTestRun选项不适用于导入的测试控制器。它需要一个测试控制器实例,该实例作为参数传递给测试声明中使用的函数。如果要将这个测试控制器实例传递给另一个模块或类中声明的函数,最好的方法是将其作为函数的附加参数传递

Test.ts:

test("Get Social Compose Page", async (t) => {
    const lf = new LoginFlow(t);
    await lf.loginDispatch();
});

LoginFlow.ts:

constructor(t){
        this.loginEmail = locate.elementWithId('email', t); 
        this.loginPassword = locate.elementWithId('password', t);
        this.loginButton = locate.elementWithId('login', t);
        this.searchBar = locate.elementWithId('searchinput', t);
}

Locator.ts:

export default class Locators {
    elementWithId (id: string, t: any) {
        const element = Selector(id => {
            return document.getElementById(id);
        }, {
                boundTestRun: t
            });
        const boundelement = element(id)
        return boundelement
    };
};

但是,boundTestRun选项的用例是calling a Selector from Node.js callback,因此您可以通过如下修改示例来使其工作:

constructor(){
        this.loginEmail = Selector('#email'); 
        this.loginPassword = Selector('#password');
        this.loginButton = Selector('#login');
        this.searchBar = Selector('#searchinput');
}