选择器找不到测试控制器
Selector cannot find Test Controller
我需要将测试控制器传递给我的选择器。我是 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 中声明了 boundelement,但它抱怨说 element(id) 不是一个函数。
boundTestRun 选项不适用于导入的测试控制器。它需要一个测试控制器实例,该实例作为参数传递给测试声明中使用的函数。如果您想将此测试控制器实例传递给另一个模块或 class 中声明的函数,最好的方法是将其作为函数的附加参数传递:
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');
}
我需要将测试控制器传递给我的选择器。我是 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 中声明了 boundelement,但它抱怨说 element(id) 不是一个函数。
boundTestRun 选项不适用于导入的测试控制器。它需要一个测试控制器实例,该实例作为参数传递给测试声明中使用的函数。如果您想将此测试控制器实例传递给另一个模块或 class 中声明的函数,最好的方法是将其作为函数的附加参数传递:
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');
}