我尝试扩展Cypress配置,以这种方式添加我自己的属性:
Cypress.Commands.overwrite('getUser', (originalFn: any) => {
const overwriteOptions = {
accountPath: `accounts/${options.accountPath}`,
};
return originalFn(overwriteOptions).then(response =>
Cypress.config({
authUserString: generateAuthUserString(response.email, response.password),
})
);
});
但我得到了这个错误:
No overload matches this call.
Overload 1 of 4, '(key: "url" | "browser" | "autoOpen" | "browserUrl" | "clientRoute" | "cypressEnv" | "isNewProject" | "isTextTerminal" | "morgan" | "parentTestsFolder" | "parentTestsFolderDisplay" | ... 84 more ... | "cypressBinaryRoot"): string | ... 16 more ... | { ...; }', gave the following error.
Argument of type '{ authUserString: any; }' is not assignable to parameter of type '"url" | "browser" | "autoOpen" | "browserUrl" | "clientRoute" | "cypressEnv" | "isNewProject" | "isTextTerminal" | "morgan" | "parentTestsFolder" | "parentTestsFolderDisplay" | ... 84 more ... | "cypressBinaryRoot"'.
Overload 2 of 4, '(Object: TestConfigOverrides): void', gave the following error.
Argument of type '{ authUserString: any; }' is not assignable to parameter of type 'TestConfigOverrides'.
Object literal may only specify known properties, and 'authUserString' does not exist in type 'TestConfigOverrides'.ts(2769)
如何将自己的属性添加到Cypress配置中?我使用Typescript。
1条答案
按热度按时间h4cxqtbf1#
你不需要使用
Cypress.config()
,你应该使用Cypress.env()
,它可以自由扩展。在测试过程中向config添加项没有任何效果,因为Cypress内部代码都不知道您的自定义属性。
因此,您必须在测试中明确使用它,
Cypress.env()
是最佳选择。另一个问题是
Cypress.config({...})
将完全替换所有现有的配置,即使它工作。正确的语法是指定键和值:
Cypress.config('key', value)
。同样适用于
Cypress.env('key', value)