Skip to content

Commit 83e6884

Browse files
author
Myron van Velsen
committed
Merge branch '204-add-tsconfig-path-aliases-for-utils-poms-and-more-folders' into 'main'
Resolve "Add tsconfig path aliases for utils, poms and more folders" See merge request elgentos/magento2-playwright!18
2 parents 4f3bfcb + 7b1d31c commit 83e6884

40 files changed

+161
-160
lines changed

README.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
This package contains an end-to-end (E2E) testing suite for Magento 2, powered by [Playwright](https://playwright.dev/). It enables you to quickly set up, run, and extend automated browser tests for your Magento 2 store. Installation is simple via npm, allowing you to seamlessly integrate robust testing into your development workflow.
44

5-
65
<mark>⚠️ Please note: if you’re not sure what each test does, **then you should only run this in a testing environment**! Some tests involve the database, and for the suite to run `setup.spec.ts` will disable the CATPCHA of your webshop.</mark>
76

8-
97
🏃**Just want to install and get going?**
108

119
If you’re simply looking to install, check the [prerequisites](#prerequisites) and then go to [🧪 Installing the suite](#-installing-the-suite).
1210

13-
14-
1511
---
1612

1713
## Table of contents
@@ -35,8 +31,6 @@ If you’re simply looking to install, check the [prerequisites](#prerequisites)
3531
* This testing suite has been designed to work within a Hÿva theme in Magento 2, but can work with other themes.
3632
* **Magento 2 instance:** A running instance of Magento 2 for testing purposes. Elgentos sponsors a [Hyvä demo website](https://hyva-demo.elgentos.io/) for this project.
3733

38-
39-
4034
---
4135

4236
## 🧪 Installing the suite
@@ -45,45 +39,34 @@ If you’re simply looking to install, check the [prerequisites](#prerequisites)
4539

4640
Navigate to the `web` folder of your theme. This is usually located in `app/design/frontend/{vendor}/{theme}/web`. Within this folder, create a `playwright` folder, then navigate to it:
4741

48-
4942
```bash
5043
cd app/design/frontend/demo-store/demo-theme/web
5144
mkdir playwright
5245
cd playwright
5346
```
5447

55-
5648
2. **Initialize an npm project:**
5749

58-
5950
```bash
6051
npm init -y
6152
```
6253

63-
6454
3. **Install the test suite package**
6555

6656
Lastly, simply run the command to install the elgentos Magento2 Playwright package, and the installation script will set things up for you! You will be prompted to input values for the `.env` variables, but these also come with default values.
6757

68-
6958
```bash
7059
npm install @elgentos/magento2-playwright
7160
```
7261

73-
74-
7562
---
7663

7764
## ⏸️ Before you run
7865

79-
After the installation, a variety of folders will have been created. Most notable in these are `base-tests`, which contain the tests without alteration, and `tests`. **You should never make changes directly to the base-tests folder, as this may break functionality. However, note that the** `base-tests` **can be updated when you upgrade the package, so always review any changes after an update.**
80-
81-
66+
After the installation, a variety of folders will have been created. Most notable in these are `base-tests`, which contain the tests without alteration, and `tests`. **You should never make changes directly to the base-tests folder, as thisx may break functionality. However, note that the** `base-tests` **can be updated when you upgrade the package, so always review any changes after an update.**
8267

8368
> If you want to make changes to your iteration of the testing suite such as making changes to how the test works, updating element identifiers etc., see the section ‘Customizing the testing suite’ below.
8469
85-
86-
8770
---
8871

8972
## 🤖 Run setup… then you can run the suite!
@@ -95,22 +78,18 @@ Finally, before running the testing suite, you must run `setup.spec.ts`. This mu
9578
npx playwright test --grep "@setup" --trace on
9679
```
9780

98-
9981
After that - you’re all set! 🥳 You can run the testing suite - feel free to skip the setup at this point:
10082

10183
```bash
10284
npx playrwight test --grep-invert "@setup" --trace on
10385
```
10486

105-
106-
10787
---
10888

10989
## 🚀 How to use the testing suite
11090

11191
The Testing Suite offers a variety of tests for your Magento 2 application in Chromium, Firefox, and Webkit.
11292

113-
11493
### Running tests
11594

11695
To run all tests, run the following command:
@@ -119,37 +98,29 @@ To run all tests, run the following command:
11998
npx playwright test --grep-invert "@setup"
12099
```
121100

122-
123-
This command will run all tests located in the `base-tests` directory. If you have custom tests in the `test` folder, these will be used instead of their `base-tests` counterpart.
124-
101+
This command will run all tests located in the `base-tests` directory. If you have custom tests in the `tests` folder, these will be used instead of their `base-tests` counterpart.
125102

126103
You can also run a specific test file:
127104

128105
```bash
129-
npx playwright test tests/example.test.ts
106+
npx playwright test example.spec.ts
130107
```
131108

132-
133109
The above commands will run your tests, then offer a report. You can also use [the UI mode](https://playwright.dev/docs/running-tests#debug-tests-in-ui-mode) to see what the tests are doing, which is helpful for debugging. To open up UI mode, run this command:
134110

135-
136111
```bash
137112
npx playwright test --ui
138113
```
139114

140-
141115
Playwright also offers a trace view. While using the UI mode is seen as the default for developing and debugging tests, you may want to run the tests and collect a trace instead. This can be done with the following command:
142116

143-
144117
```bash
145118
npx playwright test --trace on
146119
```
147120

148-
149121
### Skipping specific tests
150122

151-
Certain `spec` files and specific tests are used as a setup. For example, all setup tests (such as creating an account and setting a coupon code in your Magento 2 environment) have the tag ‘@setup’. Since these only have to be used once (or in the case of our demo website every 24 hours), most of the time you can skip these. These means most of the time, using the following command is best. This command skips both the `user can register an account` test, as well as the whole of `base/setup.spec.ts`.
152-
123+
Certain `spec` files and specific tests are used as a setup. For example, all setup tests (such as creating an account and setting a coupon code in your Magento 2 environment) have the tag ‘@setup’. Since these only have to be used once (or in the case of our demo website every 24 hours), most of the time you can skip these. These means most of the time, using the following command is best. These tests, including `user_can_register_an_account` and all tests in `base-tests/setup.spec.ts` (or any custom setup in `tests/setup.spec.ts`), can be skipped most of the time.
153124

154125
```bash
155126
npx playwright test –-grep-invert @setup
@@ -159,45 +130,72 @@ npx playwright test –-grep-invert @setup
159130

160131
Most tests have been provided with a tag. This allows the user to run specific groups of tests, or skip specific tests. For example, tests that check the functionality of coupon codes are provided with the tag ‘@coupon-code’. To run only these tests, use:
161132

162-
163133
```bash
134+
164135
npx playwright test –-grep @coupon-code
165136
```
166137

167-
168138
You can also run multiple tags with logic operators:
169139

170-
171140
```bash
141+
172142
npx playwright test –-grep ”@coupon-code|@cart”
173143
```
174144

175-
176145
Use `--grep-invert` to run all tests **except** the tests with the specified test. Playwright docs offer more information: [Playwright: Tag Annotations](https://playwright.dev/docs/test-annotations#tag-tests). The following command, for example, skips all tests with the tag ‘@coupon-code’.
177146

178-
179147
```bash
148+
180149
npx playwright test –-grep-invert @coupon-code
181150
```
182151

152+
### Customizing the testing suite
183153

154+
The newly created `tests` folder will become your base of operations. In here, you should use the same folder structure that you see in `base-tests`. For example, if your login page works slightly differently from the demo website version, create a copy of `login.page.ts` and place it `tests/config/poms/frontend/` and make your edits in this file. The next time you run the testing suite, it will automatically use these custom files.
184155

185-
---
156+
#### Module Imports
157+
158+
To keep the project structure clean and maintainable, we use **TypeScript path aliases** via `tsconfig.json`. This allows you to use the `@` prefix in imports instead of relative paths like `../../../`.
186159

187-
## ✏️ Customizing the testing suite
160+
#### Guidelines
188161

189-
The newly created `tests` folder will become your base of operations. In here, you should use the same folder structure that you see in `base-tests`. For example, if your login page works slightly differently from the demo website version, create a copy of `login.page.ts` and place it `tests/config/poms/frontend/` and make your edits in this file. **You will also have to copy the corresponding** `.spec.ts` **file**. The next time you run the testing suite, it will automatically use these custom files.
162+
**Always use `@` imports** when importing from one of the core module folders, such as:
190163

164+
- `@poms` – Page Object Models
165+
- `@config` – Test configuration and data
166+
- `@utils` – Shared utility functions
167+
- `@steps` – Common step definitions
168+
- `@features` – (Optional) Gherkin feature files
169+
170+
**Correct Usage**
171+
172+
```ts
173+
import { UIReference } from '@config';
174+
import { requireEnv } from '@utils/env.utils';
175+
176+
import HomePage from '@poms/frontend/home.page';
177+
```
178+
179+
**Wrong Usage**
180+
181+
```ts
182+
// ❌ Don't use relative paths
183+
import { UIReference } from '../config';
184+
import { requireEnv } from '../utils/env.utils';
185+
186+
import HomePage from '../poms/frontend/home.page';
187+
```
188+
189+
---
191190

192191
### Examples
193192

194193
Below are some example tests to illustrate how to write and structure your tests.
195194

196-
197195
**User registration test:**
198196

199-
200197
```javascript
198+
201199
/**
202200
* @feature User Registration
203201
* @scenario User successfully registers on the website
@@ -206,15 +204,13 @@ Below are some example tests to illustrate how to write and structure your tests
206204
* @and I submit the form
207205
* @then I should see a confirmation message
208206
*/
209-
test('User can register an account', async ({ page }) => {
207+
test('user_can_register_an_account', async ({ page }) => {
210208
// Implementation details
211209
});
212210
```
213211

214-
215212
**Checkout process test:**
216213

217-
218214
```javascript
219215
/**
220216
* @feature Product Checkout
@@ -229,7 +225,11 @@ test('User can complete the checkout process', async ({ page }) => {
229225
});
230226
```
231227

228+
---
229+
230+
## Troubleshooting
232231

232+
If an `@` import doesn’t work, make sure your local `tsconfig.json` matches the one provided by the npm package.
233233

234234
---
235235

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elgentos/magento2-playwright",
3-
"version": "2.0.1-alpha",
3+
"version": "2.1.0-alpha",
44
"author": "elgentos",
55
"license": "ISC",
66
"description": "A Playwright End-To-End (E2E) testing suite for Magento 2 with Hyva that helps you find (potential) issues on your webshop.",

tests.config.example.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/account.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import { test, expect } from '@playwright/test';
44
import { faker } from '@faker-js/faker';
5-
import { UIReference, outcomeMarker, slugs, inputValues } from 'config';
6-
import { requireEnv } from './utils/env.utils';
7-
8-
import AccountPage from './poms/frontend/account.page';
9-
import LoginPage from './poms/frontend/login.page';
10-
import MainMenuPage from './poms/frontend/mainmenu.page';
11-
import NewsletterSubscriptionPage from './poms/frontend/newsletter.page';
12-
import RegisterPage from './poms/frontend/register.page';
5+
import { UIReference, outcomeMarker, slugs, inputValues} from '@config';
6+
import { requireEnv } from '@utils/env.utils';
7+
8+
import AccountPage from '@poms/frontend/account.page';
9+
import LoginPage from '@poms/frontend/login.page';
10+
import MainMenuPage from '@poms/frontend/mainmenu.page';
11+
import NewsletterSubscriptionPage from '@poms/frontend/newsletter.page';
12+
import RegisterPage from '@poms/frontend/register.page';
1313

1414
// Before each test, log in
1515
test.beforeEach(async ({ page, browserName }) => {

tests/auth.setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { test as setup, expect } from '@playwright/test';
44
import path from 'path';
5-
import { UIReference, slugs } from 'config';
6-
import { requireEnv } from './utils/env.utils';
5+
import { UIReference, slugs } from '@config';
6+
import { requireEnv } from '@utils/env.utils';
77

88
const authFile = path.join(__dirname, '../playwright/.auth/user.json');
99

tests/cart.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @ts-check
22

33
import { test, expect } from '@playwright/test';
4-
import { UIReference, slugs, outcomeMarker } from 'config';
4+
import { UIReference, slugs, outcomeMarker } from '@config';
55

6-
import CartPage from './poms/frontend/cart.page';
7-
import LoginPage from './poms/frontend/login.page';
8-
import ProductPage from './poms/frontend/product.page';
9-
import { requireEnv } from './utils/env.utils';
10-
import NotificationValidator from './utils/notification.validator';
6+
import CartPage from '@poms/frontend/cart.page';
7+
import LoginPage from '@poms/frontend/login.page';
8+
import ProductPage from '@poms/frontend/product.page';
9+
import { requireEnv } from '@utils/env.utils';
10+
import NotificationValidator from '@utils/notification.validator';
1111

1212
test.describe('Cart functionalities (guest)', () => {
1313
/**

tests/category.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { test } from '@playwright/test';
44

5-
import CategoryPage from './poms/frontend/category.page';
5+
import CategoryPage from '@poms/frontend/category.page';
66

77
test('Filter_category_on_size',{ tag: ['@category', '@cold']}, async ({page, browserName}) => {
88
const categoryPage = new CategoryPage(page);

tests/checkout.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @ts-check
22

33
import { test, expect } from '@playwright/test';
4-
import { UIReference, slugs } from 'config';
4+
import { UIReference, slugs } from '@config';
55

6-
import LoginPage from './poms/frontend/login.page';
7-
import ProductPage from './poms/frontend/product.page';
8-
import AccountPage from './poms/frontend/account.page';
9-
import { requireEnv } from './utils/env.utils';
10-
import CheckoutPage from './poms/frontend/checkout.page';
6+
import LoginPage from '@poms/frontend/login.page';
7+
import ProductPage from '@poms/frontend/product.page';
8+
import AccountPage from '@poms/frontend/account.page';
9+
import { requireEnv } from '@utils/env.utils';
10+
import CheckoutPage from '@poms/frontend/checkout.page';
1111

1212

1313
/**

0 commit comments

Comments
 (0)