|
| 1 | +<!-- .slide: class="transition" --> |
| 2 | + |
| 3 | +# Make API call |
| 4 | + |
| 5 | +##==## |
| 6 | + |
| 7 | +<!-- .slide: class="with-code" --> |
| 8 | + |
| 9 | +# Access the page... |
| 10 | + |
| 11 | +```TypeScript |
| 12 | +test('ui test', ({ page }) => { |
| 13 | + await page.goto('/'); |
| 14 | +}); |
| 15 | +``` |
| 16 | + |
| 17 | +<!-- .element: class="big-code" --> |
| 18 | + |
| 19 | +Notes: |
| 20 | + |
| 21 | +- as we see before |
| 22 | + |
| 23 | +##==## |
| 24 | + |
| 25 | +<!-- .slide: class="with-code" --> |
| 26 | + |
| 27 | +# Access the API! |
| 28 | + |
| 29 | +```TypeScript |
| 30 | +test('ui test', ({ page }) => { |
| 31 | + await page.goto('/'); |
| 32 | +}); |
| 33 | + |
| 34 | +test('api test', ({ request }) => { |
| 35 | + await request.get('/api/health'); |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +<!-- .element: class="big-code" --> |
| 40 | + |
| 41 | +Notes: |
| 42 | + |
| 43 | +- as simple as before! |
| 44 | +- Playwright offer the request fixture built-in |
| 45 | +- it gives method to make any api calls |
| 46 | + |
| 47 | +##==## |
| 48 | + |
| 49 | +<!-- .slide: class="with-code" --> |
| 50 | + |
| 51 | +# Exemple 1: simulate submitting a login form |
| 52 | + |
| 53 | +```TypeScript |
| 54 | +const res = await request.post('/api/login', { |
| 55 | + form: { email: '[email protected]', password: 'SuperSecure1' } |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +<!-- .element: class="big-code" --> |
| 60 | + |
| 61 | +Notes: |
| 62 | + |
| 63 | +- to speed up auth, we can use api call to simulate login |
| 64 | +- pay attention, it's not how the user will authenticate, so keep some test using the UI! |
| 65 | + |
| 66 | +##==## |
| 67 | + |
| 68 | +<!-- .slide: class="with-code" --> |
| 69 | + |
| 70 | +# Exemple 1: simulate submitting a login form (FormData version) |
| 71 | + |
| 72 | +```TypeScript |
| 73 | +const form = new FormData(); |
| 74 | +form. set( 'email', '[email protected]') |
| 75 | +form.set('password', 'SuperSecure1') |
| 76 | +const res = await request.post('/api/login', { form }); |
| 77 | +``` |
| 78 | + |
| 79 | +<!-- .element: class="big-code" --> |
| 80 | + |
| 81 | +##==## |
| 82 | + |
| 83 | +<!-- .slide: class="with-code" --> |
| 84 | + |
| 85 | +# Exemple 2: make an authenticate POST call |
| 86 | + |
| 87 | +```TypeScript |
| 88 | +const res = await request.post('/api/v1/secret-user-data', { |
| 89 | + headers: { 'Authorization': 'Bearer A.Pretty-Legit-Jwt.Token' } |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +<!-- .element: class="big-code" --> |
| 94 | + |
| 95 | +##==## |
| 96 | + |
| 97 | +<!-- .slide: class="with-code" --> |
| 98 | + |
| 99 | +# Exemple 3: cleanup data after running a UI test |
| 100 | + |
| 101 | +```TypeScript |
| 102 | +const res = await request.delete(`/api/v1/messages/${messageIdCreatedDuringTheTest}`); |
| 103 | +``` |
| 104 | + |
| 105 | +<!-- .element: class="big-code" --> |
| 106 | + |
| 107 | +Notes: |
| 108 | + |
| 109 | +- some times we want to cleanup data we created/updated during the test |
| 110 | +- using API call it can be easier to make this cleanup! |
0 commit comments