-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[scout] An abstraction for extending pageObjects #243111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { ScoutPage } from '../../../..'; | ||
|
|
||
| export class AbstractPageObject { | ||
| constructor(public readonly page: ScoutPage) {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { test as base } from '../../../..'; | ||
| import type { ScoutPage, ScoutTestFixtures, ScoutWorkerFixtures } from '../../../..'; | ||
| import type { PageObjects } from '../../../..'; | ||
| import { createLazyPageObject } from '../../../..'; | ||
| import type { AbstractPageObject } from './abstract_page_object'; | ||
|
|
||
| type PageObjectClass = new (page: ScoutPage) => AbstractPageObject; | ||
|
|
||
| export const createTest = function <PageObjectsExtensions = Record<string, AbstractPageObject>>( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
See some examples of Scout packages and plugins extending multiple fixtures (not just
What I appreciate about the current architecture is how flexible it is when it comes to extending any Scout core fixture (test-scoped and worker-scoped) to expose additional methods. While your method is convenient I'm afraid it will limit what plugins (but most especially Scout packages) can expose to other plugins. What do you think? |
||
| pageObjectClassMap: Record<string, PageObjectClass> | ||
| ) { | ||
| type PageObjectsExtended = PageObjectsExtensions & PageObjects; | ||
|
|
||
| function extendPOs(pageObjects: PageObjects, page: ScoutPage): PageObjectsExtended { | ||
| const initedLazyPageObjects = Object.keys(pageObjectClassMap).reduce< | ||
| Record<string, AbstractPageObject> | ||
| >((col, value) => { | ||
| col[value] = createLazyPageObject(pageObjectClassMap[value], page); | ||
| return col; | ||
| }, {}); | ||
|
|
||
| return { | ||
| ...initedLazyPageObjects, | ||
| ...pageObjects, | ||
| } as PageObjectsExtended; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would prefer a better solution than using Also, I bet a number of these things could be better named. |
||
| } | ||
|
|
||
| return base.extend< | ||
| ScoutTestFixtures & { | ||
| pageObjects: PageObjectsExtended; | ||
| }, | ||
| ScoutWorkerFixtures | ||
| >({ | ||
| pageObjects: async ( | ||
| { | ||
| pageObjects, | ||
| page, | ||
| }: { | ||
| pageObjects: PageObjectsExtended; | ||
| page: ScoutPage; | ||
| }, | ||
| use: (pageObjects: PageObjectsExtended) => Promise<void> | ||
| ) => { | ||
| const extendedPageObjects = extendPOs(pageObjects, page); | ||
| await use(extendedPageObjects); | ||
| }, | ||
| }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| export { createTest } from './create_test'; | ||
| export { AbstractPageObject } from './abstract_page_object'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,11 @@ | |
| import type { | ||
| PageObjects, | ||
| ScoutTestFixtures, | ||
| ScoutWorkerFixtures, | ||
| ScoutParallelTestFixtures, | ||
| ScoutParallelWorkerFixtures, | ||
| } from '@kbn/scout'; | ||
| import { test as baseTest, spaceTest as spaceBaseTest, createLazyPageObject } from '@kbn/scout'; | ||
| import { spaceTest as spaceBaseTest, createLazyPageObject } from '@kbn/scout'; | ||
| import { createTest } from '@kbn/scout'; | ||
| import { DemoPage } from './page_objects'; | ||
|
|
||
| export interface ExtScoutTestFixtures extends ScoutTestFixtures { | ||
|
|
@@ -21,24 +21,10 @@ export interface ExtScoutTestFixtures extends ScoutTestFixtures { | |
| }; | ||
| } | ||
|
|
||
| export const test = baseTest.extend<ExtScoutTestFixtures, ScoutWorkerFixtures>({ | ||
| pageObjects: async ( | ||
| { | ||
| pageObjects, | ||
| page, | ||
| }: { | ||
| pageObjects: ExtScoutTestFixtures['pageObjects']; | ||
| page: ExtScoutTestFixtures['page']; | ||
| }, | ||
| use: (pageObjects: ExtScoutTestFixtures['pageObjects']) => Promise<void> | ||
| ) => { | ||
| const extendedPageObjects = { | ||
| ...pageObjects, | ||
| demo: createLazyPageObject(DemoPage, page), | ||
| }; | ||
|
|
||
| await use(extendedPageObjects); | ||
| }, | ||
| export const test = createTest<{ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the result I desired, a simple and terse way to extend the set of page objects. The createTest function is dense and difficult to read but the code it replaces didn't have any more meaning as best I could tell. |
||
| demo: DemoPage; | ||
| }>({ | ||
| demo: DemoPage, | ||
| }); | ||
|
|
||
| export interface ExtParallelRunTestFixtures extends ScoutParallelTestFixtures { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this could be done for the lighthouseTest and spaceTest as well