Page Object Manager in Cucumber-js with Typescript

huangapple go评论135阅读模式
英文:

Page Object Manager in Cucumber-js with Typescript

问题

I am writing Playwright Cucumber TypeScript framework (stack must be like this, even though we don't do BDD at all).
我正在编写Playwright Cucumber TypeScript框架(尽管我们根本不使用BDD)。

I created POManager to manage all Page Object classes (what do you think about this idea).
我创建了POManager来管理所有的页面对象类(你对这个想法有什么看法)。

POManager is part of interface CustomWorld
POManagerCustomWorld接口的一部分。

  1. export interface CustomWorld extends World {
  2. //...
  3. context?: BrowserContext;
  4. page?: Page;
  5. poManager?: POManager;
  6. //...
  7. }

I have to declare it with ?: since it is defined some time in the future in the before hook
我必须使用声明它,因为它是在before hook中的将来某个时间定义的。

  1. Before(async function (this: CustomWorld, { pickle }: ITestCaseHookParameter) {
  2. //...
  3. this.context = await browser.newContext({
  4. //...
  5. });
  6. //...
  7. this.page = await this.context.newPage();
  8. //...
  9. this.poManager = new POManager(this.page);
  10. });

POManager itself is just a class like that
POManager本身就是这样的一个类

  1. export class POManager {
  2. private readonly _myPage: MyPage;
  3. constructor(private readonly page: Page) {
  4. this._myPage = new MyPage(page);
  5. }
  6. get myPage(): MyPage {
  7. return this._myPage;
  8. }
  9. }

Obviously, because of being defined with ?:, this.poManager type is POManager | undefined.
显然,由于使用了进行定义,this.poManager的类型是POManager | undefined

Is there anything I could do to narrow this type, should I just live with that, or maybe try another approach?
有没有办法缩小这个类型,我应该接受这个,还是尝试另一种方法?

In feature files, it looks like that:
在功能文件中,看起来是这样的:

  1. Given(/^Blah Blah Blah$/, function (this: CustomWorld) {
  2. const myPage: MyPage | undefined = this.poManager?.myPage;
  3. // if I grab it with just a . (dot) it underlines this.poManager as being possibly undefined.
  4. // 如果我只用一个.(点)来抓取它,它会将this.poManager下划线为可能未定义。
  5. const myPage2: MyPage = this.poManager.myPage;
  6. });

I know I could write //@ts-ignore or change tsconfig.json settings, but is there any other way? I do care about type correctness.
我知道我可以写//@ts-ignore或更改tsconfig.json的设置,但还有其他方法吗?我确实关心类型的正确性。

Thank you!
谢谢!

英文:

I am writing Playwright Cucumber TypeScript framework (stack must be like this, even though we don't do BDD at all).
I created POManager to manage all Page Object classes (what do you think about this idea).

POManager is part of interface CustomWorld

  1. export interface CustomWorld extends World {
  2. //...
  3. context?: BrowserContext;
  4. page?: Page;
  5. poManager?: POManager;
  6. //...
  7. }

I have to declare it with ?: since it is defined some time in future in before hook

  1. Before(async function (this: CustomWorld, {pickle}: ITestCaseHookParameter) {
  2. //...
  3. this.context = await browser.newContext({
  4. //...
  5. });
  6. //...
  7. this.page = await this.context.newPage();
  8. //...
  9. this.poManager = new POManager(this.page);
  10. });

POManager itself is just class like that

  1. export class POManager {
  2. private readonly _myPage: MyPage;
  3. constructor(private readonly page: Page) {
  4. this._myPage= new MyPage(page);
  5. }
  6. get myPage(): MyPage{
  7. return this._myPage;
  8. }
  9. }

Obviously, because of being defined with ?:, this.poManager type is POmanager | undefined.
Is there anything I could do to narrow this type, should I just live with that, or maybe try another approch?

In feature files it looks like that:

  1. Given(/^Blah Blah Blah$/, function (this: CustomWorld) {
  2. const myPage: MyPage | undefinded = this.poManager?.myPage;
  3. // if I grab it with just a . (dot) it underlines this.poManager as being possibly undefined.
  4. const myPage2: MyPage = this.poManager.myPage;
  5. });

I know I could write //@ts-ignore or change tsconfig.json settings, but is there any another way? I do care about type correctness.

Thank you!

答案1

得分: 0

基本上,我需要在声明常量时使用类型断言。

即:

  1. const myPage: MyPage = this.poManager?.myPage as MyPage;

现在没有错误问题。

英文:

Uh, basically I need to use type assertion as I delcare consts.

That is:

  1. const myPage: MyPage = this.poManager?.myPage as MyPage;

No problems with errors now.

huangapple
  • 本文由 发表于 2023年7月3日 01:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定