使用Typescript使用自己的属性扩展Cypress.config

u3r8eeie  于 2023-05-19  发布在  TypeScript
关注(0)|答案(1)|浏览(208)

我尝试扩展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。

h4cxqtbf

h4cxqtbf1#

你不需要使用Cypress.config(),你应该使用Cypress.env(),它可以自由扩展。
在测试过程中向config添加项没有任何效果,因为Cypress内部代码都不知道您的自定义属性。
因此,您必须在测试中明确使用它,Cypress.env()是最佳选择。
另一个问题是Cypress.config({...})将完全替换所有现有的配置,即使它工作。
正确的语法是指定键和值:Cypress.config('key', value)
同样适用于Cypress.env('key', value)

Cypress.Commands.overwrite('getUser', (originalFn: any) => {
  const overwriteOptions = {
    accountPath: `accounts/${options.accountPath}`,
  };

  return originalFn(overwriteOptions).then(response =>
    Cypress.env('authUserString', generateAuthUserString(response.email, response.password))
    })
  );
})

it('uses authUser value', () => {
  cy.getUser({accountPath: 'admin'})
    .then(() => {
      expect(Cypress.env('authUserString')).to.eq('authorised')
    })
})

相关问题