In this task, you will practice building the page classes hierarchy and apply the OOP principles.
-
Open the forked repo in VSCode.
-
Create a new branch by running git checkout -b task_solution.
-
Run the installation commands:
npm ci
npx playwright install
- Create
BasePage.js
file under the folder./src/pages/
. - Create a new class
BasePage
. - Add a constructor to the created
BasePage
class:
constructor(page) {
this.page = page;
}
- Open the
CartPage.js
file. - Import
BasePage
class to it. - Inherit the
BasePage
by theCartPage
:
export class CartPage extend BasePage {...}
- Update
CartPage
constructor method to initialize parent class constructor:
- 7.1 Remove the line
this.page = page;
- this will now be done in theBasePage
class. - 7.2 Add as a first line method
super(page);
. This method will initialize the parent constructor.
- Repeat the steps 4-7 for the
MenuPage.js
class. - Run all the tests to make sure nothing is broken.
- Move the
open()
method to theBasePage
:
- 10.1 Add the
_url
protected property to theBasePage
:
export class BasePage {
_url;
constructor(page) {...}
}
- 10.2 Add the public
url()
method to theBasePage
class:
url() {
if (this._url) {
return this._url;
} else {
throw Error(`The property '_url' must be implemented`);
}
}
- 10.3 Add the
open()
method to theBasePage
:
async open() {
await this.step(`Open page`, async () => {
await this.page.goto(this.url());
});
}
- 10.4 Add the protected
_pageName()
method:
_pageName() {
return this.constructor.name.replace('Page', '');
}
- 10.5 Update the
open()
method step to incorporate a page name:
... this.step(`Open ${this._pageName()} page`, ...)
- Assign value to
this._url =
in constructor for theMenuPage
andCartPage
classes. - Remove the
open()
methods fromMenuPage
andCartPage
classes. - Run all the tests to make sure nothing is broken.
- Move the
reload()
method from theMenuPage
andCartPage
classes to theBasePage
class. - Move the
waitForLoading()
method from theCartPage
class to theBasePage
class. - Run all the tests to make sure nothing is broken.
- Add and commit all your updates.
- Push the code to the origin.
- Create a PR for your changes.
- Keep implementing suggestions from code review until your PR is approved.