Skip to content

Commit 70a7a39

Browse files
committed
Add small updates to part5d
1 parent 1ff42a0 commit 70a7a39

File tree

2 files changed

+54
-90
lines changed

2 files changed

+54
-90
lines changed

src/content/5/en/part5d.md

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,12 @@ Let's make an npm script for the <i>backend</i>, which will enable it to be star
176176
{
177177
// ...
178178
"scripts": {
179-
"start": "NODE_ENV=production node index.js",
180-
"dev": "NODE_ENV=development nodemon index.js",
181-
"build:ui": "rm -rf build && cd ../frontend/ && npm run build && cp -r build ../backend",
182-
"deploy": "fly deploy",
183-
"deploy:full": "npm run build:ui && npm run deploy",
184-
"logs:prod": "fly logs",
179+
"start": "cross-env NODE_ENV=production node index.js",
180+
"dev": "cross-env NODE_ENV=development node --watch index.js",
181+
"test": "cross-env NODE_ENV=test node --test",
185182
"lint": "eslint .",
186-
"test": "NODE_ENV=test node --test",
187-
"start:test": "NODE_ENV=test node index.js" // highlight-line
183+
// ...
184+
"start:test": "cross-env NODE_ENV=test node --watch index.js" // highlight-line
188185
},
189186
// ...
190187
}
@@ -200,7 +197,7 @@ test('front page can be opened', async ({ page }) => {
200197

201198
const locator = await page.getByText('Notes')
202199
await expect(locator).toBeVisible()
203-
await expect(page.getByText('Note app, Department of Computer Science, University of Helsinki 2023')).toBeVisible()
200+
await expect(page.getByText('Note app, Department of Computer Science, University of Helsinki 2024')).toBeVisible()
204201
})
205202
```
206203

@@ -210,21 +207,7 @@ The method [toBeVisible](https://playwright.dev/docs/api/class-locatorassertions
210207

211208
The second check is done without using the auxiliary variable.
212209

213-
We notice that the year has changed. Let's change the test as follows:
214-
215-
```js
216-
const { test, expect } = require('@playwright/test')
217-
218-
test('front page can be opened', async ({ page }) => {
219-
await page.goto('http://localhost:5173')
220-
221-
const locator = await page.getByText('Notes')
222-
await expect(locator).toBeVisible()
223-
await expect(page.getByText('Note app, Department of Computer Science, University of Helsinki 2024')).toBeVisible() // highlight-line
224-
})
225-
```
226-
227-
As expected, the test fails. Playwright opens the test report in the browser and it becomes clear that Playwright has actually performed the tests with three different browsers: Chrome, Firefox and Webkit, i.e. the browser engine used by Safari:
210+
The test fails because an old year ended up in the test. Playwright opens the test report in the browser and it becomes clear that Playwright has actually performed the tests with three different browsers: Chrome, Firefox and Webkit, i.e. the browser engine used by Safari:
228211

229212
![test report showing the test failing in three different browsers](../../images/5/play2.png)
230213

@@ -238,9 +221,7 @@ In the big picture, it is of course a very good thing that the testing takes pla
238221
npm test -- --project chromium
239222
```
240223

241-
Now let's correct the outdated year in the frontend code that caused the error.
242-
243-
Before we continue, let's add a _describe_ block to the tests:
224+
Now let's fix the test with the correct year and let's add a _describe_ block to the tests:
244225

245226
```js
246227
const { test, describe, expect } = require('@playwright/test')
@@ -251,7 +232,7 @@ describe('Note app', () => { // highlight-line
251232

252233
const locator = await page.getByText('Notes')
253234
await expect(locator).toBeVisible()
254-
await expect(page.getByText('Note app, Department of Computer Science, University of Helsinki 2024')).toBeVisible()
235+
await expect(page.getByText('Note app, Department of Computer Science, University of Helsinki 2025')).toBeVisible()
255236
})
256237
})
257238
```
@@ -262,14 +243,15 @@ When developing tests, it may be wiser to reduce the waiting time to a few secon
262243

263244
```js
264245
module.exports = defineConfig({
265-
timeout: 3000,
246+
// ...
247+
timeout: 3000, // highlight-line
266248
fullyParallel: false, // highlight-line
267249
workers: 1, // highlight-line
268250
// ...
269251
})
270252
```
271253

272-
We also made two other changes to the file, and specified that all tests [be executed one at a time](https://playwright.dev/docs/test-parallel). With the default configuration, the execution happens in parallel, and since our tests use a database, parallel execution causes problems.
254+
We also made two other changes to the file, specifying that all tests [be executed one at a time](https://playwright.dev/docs/test-parallel). With the default configuration, the execution happens in parallel, and since our tests use a database, parallel execution causes problems.
273255

274256
### Writing on the form
275257

@@ -284,7 +266,7 @@ describe('Note app', () => {
284266
test('login form can be opened', async ({ page }) => {
285267
await page.goto('http://localhost:5173')
286268

287-
await page.getByRole('button', { name: 'log in' }).click()
269+
await page.getByRole('button', { name: 'login' }).click()
288270
})
289271
})
290272
```
@@ -314,7 +296,7 @@ describe('Note app', () => {
314296
test('login form can be opened', async ({ page }) => {
315297
await page.goto('http://localhost:5173')
316298

317-
await page.getByRole('button', { name: 'log in' }).click()
299+
await page.getByRole('button', { name: 'login' }).click()
318300
await page.getByRole('textbox').fill('mluukkai') // highlight-line
319301
})
320302
})
@@ -337,7 +319,7 @@ describe('Note app', () => {
337319
test('login form can be opened', async ({ page }) => {
338320
await page.goto('http://localhost:5173')
339321

340-
await page.getByRole('button', { name: 'log in' }).click()
322+
await page.getByRole('button', { name: 'login' }).click()
341323
// highlight-start
342324
await page.getByRole('textbox').first().fill('mluukkai')
343325
await page.getByRole('textbox').last().fill('salainen')
@@ -359,7 +341,7 @@ describe('Note app', () => {
359341
test('login form can be opened', async ({ page }) => {
360342
await page.goto('http://localhost:5173')
361343

362-
await page.getByRole('button', { name: 'log in' }).click()
344+
await page.getByRole('button', { name: 'login' }).click()
363345
// highlight-start
364346
const textboxes = await page.getByRole('textbox').all()
365347

@@ -421,7 +403,7 @@ describe('Note app', () => {
421403
test('login form can be opened', async ({ page }) => {
422404
await page.goto('http://localhost:5173')
423405

424-
await page.getByRole('button', { name: 'log in' }).click()
406+
await page.getByRole('button', { name: 'login' }).click()
425407
await page.getByTestId('username').fill('mluukkai') // highlight-line
426408
await page.getByTestId('password').fill('salainen') // highlight-line
427409

@@ -453,7 +435,7 @@ describe('Note app', () => {
453435
})
454436

455437
test('login form can be opened', async ({ page }) => {
456-
await page.getByRole('button', { name: 'log in' }).click()
438+
await page.getByRole('button', { name: 'login' }).click()
457439
await page.getByTestId('username').fill('mluukkai')
458440
await page.getByTestId('password').fill('salainen')
459441
await page.getByRole('button', { name: 'login' }).click()
@@ -474,7 +456,7 @@ describe('Note app', () => {
474456

475457
describe('when logged in', () => {
476458
beforeEach(async ({ page }) => {
477-
await page.getByRole('button', { name: 'log in' }).click()
459+
await page.getByRole('button', { name: 'login' }).click()
478460
await page.getByTestId('username').fill('mluukkai')
479461
await page.getByTestId('password').fill('salainen')
480462
await page.getByRole('button', { name: 'login' }).click()
@@ -517,7 +499,7 @@ describe('Note app', () => {
517499
// ....
518500

519501
test('user can log in', async ({ page }) => {
520-
await page.getByRole('button', { name: 'log in' }).click()
502+
await page.getByRole('button', { name: 'login' }).click()
521503
await page.getByTestId('username').fill('mluukkai')
522504
await page.getByTestId('password').fill('salainen')
523505
await page.getByRole('button', { name: 'login' }).click()
@@ -526,7 +508,7 @@ describe('Note app', () => {
526508

527509
describe('when logged in', () => {
528510
beforeEach(async ({ page }) => {
529-
await page.getByRole('button', { name: 'log in' }).click()
511+
await page.getByRole('button', { name: 'login' }).click()
530512
await page.getByTestId('username').fill('mluukkai')
531513
await page.getByTestId('password').fill('salainen')
532514
await page.getByRole('button', { name: 'login' }).click()
@@ -684,7 +666,7 @@ describe('Note app', () => {
684666
// ...
685667

686668
test('login fails with wrong password', async ({ page }) => {
687-
await page.getByRole('button', { name: 'log in' }).click()
669+
await page.getByRole('button', { name: 'login' }).click()
688670
await page.getByTestId('username').fill('mluukkai')
689671
await page.getByTestId('password').fill('wrong')
690672
await page.getByRole('button', { name: 'login' }).click()
@@ -746,7 +728,7 @@ Let's finalize the test so that it also ensures that the application **does not
746728
747729
```js
748730
test('login fails with wrong password', async ({ page }) =>{
749-
await page.getByRole('button', { name: 'log in' }).click()
731+
await page.getByRole('button', { name: 'login' }).click()
750732
await page.getByTestId('username').fill('mluukkai')
751733
await page.getByTestId('password').fill('wrong')
752734
await page.getByRole('button', { name: 'login' }).click()
@@ -799,7 +781,7 @@ describe('Note app', () => {
799781
// ...
800782

801783
test('user can login with correct credentials', async ({ page }) => {
802-
await page.getByRole('button', { name: 'log in' }).click()
784+
await page.getByRole('button', { name: 'login' }).click()
803785
await page.getByTestId('username').fill('mluukkai')
804786
await page.getByTestId('password').fill('salainen')
805787
await page.getByRole('button', { name: 'login' }).click()
@@ -812,7 +794,7 @@ describe('Note app', () => {
812794

813795
describe('when logged in', () => {
814796
beforeEach(async ({ page, request }) => {
815-
await page.getByRole('button', { name: 'log in' }).click()
797+
await page.getByRole('button', { name: 'login' }).click()
816798
await page.getByTestId('username').fill('mluukkai')
817799
await page.getByTestId('password').fill('salainen')
818800
await page.getByRole('button', { name: 'login' }).click()
@@ -835,7 +817,7 @@ It is also worth striving for having non-repetitive code in tests. Let's isolate
835817
836818
```js
837819
const loginWith = async (page, username, password) => {
838-
await page.getByRole('button', { name: 'log in' }).click()
820+
await page.getByRole('button', { name: 'login' }).click()
839821
await page.getByTestId('username').fill(username)
840822
await page.getByTestId('password').fill(password)
841823
await page.getByRole('button', { name: 'login' }).click()
@@ -903,7 +885,7 @@ Creation of a note is also isolated as its helper function. The file _tests/help
903885
904886
```js
905887
const loginWith = async (page, username, password) => {
906-
await page.getByRole('button', { name: 'log in' }).click()
888+
await page.getByRole('button', { name: 'login' }).click()
907889
await page.getByTestId('username').fill(username)
908890
await page.getByTestId('password').fill(password)
909891
await page.getByRole('button', { name: 'login' }).click()

0 commit comments

Comments
 (0)