You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both this and the previous version of the test work. However, both are problematic to the extent that if the registration form is changed, the tests may break, as they rely on the fields to be on the page in a certain order.
360
360
361
-
A better solution is to define unique test id attributes for the fields, to search for them in the tests using the method [getByTestId](https://playwright.dev/docs/api/class-page#page-get-by-test-id).
361
+
If an element is difficult to locate in tests, you can assign it a separate <i>test-id</i> attribute and find the element in tests using the [getByTestId](https://playwright.dev/docs/api/class-page#page-get-by-test-id) method.
362
362
363
-
Let's expand the login form as follows
363
+
Let's now take advantage of the existing elements of the login form. The input fields of the login form have been assigned unique <i>labels</i>:
364
364
365
365
```js
366
-
constLoginForm= ({ ... }) => {
367
-
return (
368
-
<div>
369
-
<h2>Login</h2>
370
-
<form onSubmit={handleSubmit}>
371
-
<div>
372
-
username
373
-
<input
374
-
data-testid='username'// highlight-line
375
-
value={username}
376
-
onChange={handleUsernameChange}
377
-
/>
378
-
</div>
379
-
<div>
380
-
password
381
-
<input
382
-
data-testid='password'// highlight-line
383
-
type="password"
384
-
value={password}
385
-
onChange={handlePasswordChange}
386
-
/>
387
-
</div>
388
-
<button type="submit">
389
-
login
390
-
</button>
391
-
</form>
392
-
</div>
393
-
)
394
-
}
366
+
// ...
367
+
<form onSubmit={handleSubmit}>
368
+
<div>
369
+
<label>// highlight-line
370
+
username // highlight-line
371
+
<input
372
+
type="text"
373
+
value={username}
374
+
onChange={handleUsernameChange}
375
+
/>
376
+
</label>// highlight-line
377
+
</div>
378
+
<div>
379
+
<label>// highlight-line
380
+
password // highlight-line
381
+
<input
382
+
type="password"
383
+
value={password}
384
+
onChange={handlePasswordChange}
385
+
/>
386
+
</label>// highlight-line
387
+
</div>
388
+
<button type="submit">login</button>
389
+
</form>
390
+
// ...
395
391
```
396
392
397
-
Test changes as follows:
393
+
Input fields can and should be located in tests using <i>labels</i> with the [getByLabel](https://playwright.dev/docs/api/class-page#page-get-by-label) method:
When locating elements, it makes sense to aim to utilize the content visible to the user in the interface, as this best simulates how a user would actually find the desired input field while navigating the application.
414
+
417
415
Note that passing the test at this stage requires that there is a user in the <i>test</i> database of the backend with username <i>mluukkai</i> and password <i>salainen</i>. Create a user if needed!
418
416
417
+
### Test Initialization
418
+
419
419
Since both tests start in the same way, i.e. by opening the page <i>http://localhost:5173</i>, it is recommended to isolate the common part in the <i>beforeEach</i> block that is executed before each test:
420
420
421
421
```js
@@ -436,8 +436,8 @@ describe('Note app', () => {
436
436
437
437
test('login form can be opened', async ({ page }) => {
0 commit comments