Page login

๐Ÿ‘จโ€๐Ÿ’ผ We want to test the user 2FA functionality. Go ahead and follow the flow to get a sense for what we're trying to test:
  1. (username: kody, password: kodylovesyou)
  2. Go to
  3. On the settings page,
  4. On the 2FA page,
  5. Scan the QR code with an authentication app (1Password/Google Authenticator). Or, you can check below for an alternative way to get the code.
  6. Paste the code into the input field and click "Submit"
  7. Go back to the page
  8. Click Logout
  9. You should be shown the verification page. Get the latest code and paste it into the input field.
  10. You should be successfully logged in!
Alternative way to get the code
If you can't scan the QR code, create a temporary file and stick this in it:
import { generateTOTP } from '@epic-web/totp'

// put the URL in this string:
const otpUriString = `PASTE_THE_URL_HERE`
// it should start with otpauth://totp/...

const otpUri = new URL(otpUriString)
const options = Object.fromEntries(otpUri.searchParams.entries())

console.log(generateTOTP(options).otp)
Phew! That's a lot of steps! Yeah, but E2E tests are intended to test common user flows. They're less valuable when they're split up into tiny pieces. And luckily for you, we've got Kellie ๐Ÿงโ€โ™‚๏ธ who will do most of this for you if you don't want to do it yourself. That way you can focus most of your attention on the bits you're trying to learn.
Before we can do any of that though, we don't want the test to actually go through the entire onboarding flow. That would be wasteful since we already have a test that verifies that flow. So instead, we're going to programmatically create a user and then log in with that user.
The key bits of this involve:
  1. Create a new user (using your insertNewUser utility)
  2. Create a session for that user (prisma.session.create)
  3. Create a cookie for that session (sessionStorage.getSession)
  4. Get that cookie value from the set-cookie header
  5. Put that cookie value into the page via page.context().addCookies.
Let's talk about step 5 first...
Playwright has an API for setting browser cookies. Here's a quick example:
await page.context().addCookies([
	{
		name: 'theme',
		sameSite: 'Lax',
		domain: 'localhost',
		path: '/',
		value: 'dark',
	},
])
Most of that should look relatively familiar. There are more configuration options you can find in the docs. You want this configuration to match the configuration you have for the cookie in the app. Remember, this is a bit like a mock, so you want to make sure it resembles the real deal as much as possible.
You can set multiple cookies (just more objects in that array).

Parsing the set-cookie header

Alright, let's talk about step 4. Check this out:
const result = await sessionStorage.commitSession(cookieSession)
// result looks something like this: en_session=eyJ2ZXJpZmllZC10aW1lIjoxNjkxNDQ1NDI1NjcwLCJzZXNzaW9uSWQiOiJjbGwxZzF2emIwMDAxcGNqNjkyNTB1ZnRuIn0%3D.%2Fzc%2BhFmBg1wPNiYE5nqS1hcXsZJFZwPrLQUf7BGZD1Q; Path=/; HttpOnly; SameSite=Lax
Then the browser turns that into a Cookie header for its requests. So the browser will just send this bit in the headers:
Cookie: en_session=eyJ2ZXJpZmllZC10aW1lIjoxNjkxNDQ1NDI1NjcwLCJzZXNzaW9uSWQiOiJjbGwxZzF2emIwMDAxcGNqNjkyNTB1ZnRuIn0%3D.%2Fzc%2BhFmBg1wPNiYE5nqS1hcXsZJFZwPrLQUf7BGZD1Q;
So we need to extract that value from the set-cookie header and put it into the browser's Cookie header. There's a library that can help us with this called set-cookie-parser (as the author of a library called "React Testing Library," I must say I approve of this obvious name ๐Ÿ˜†).
import * as setCookieParser from 'set-cookie-parser'

const cookie = setCookieParser.parseString(
	await sessionStorage.commitSession(cookieSession),
)
cookie.name // => en_session
cookie.value // => eyJ2ZXJpZmllZC10aW1lIjoxNjkxNDQ1NDI1NjcwLCJzZXNzaW9uSWQiOiJjbGwxZzF2emIwMDAxcGNqNjkyNTB1ZnRuIn0%3D.%2Fzc%2BhFmBg1wPNiYE5nqS1hcXsZJFZwPrLQUf7BGZD1Q
cookie.sameSite // => Lax
// etc.
๐Ÿ‘จโ€๐Ÿ’ผ Alright, enough with that! Let's go!
You'll just be working in , but you may want to reference for the cookie config and for how to create the session and working with the cookie.
npx playwright test --ui
Login to get access to the exclusive discord channel.
  • general
    Migration to Vite: Server-only module referenced by client
    Fabian ๐ŸŒŒ:
    Hi, I'm working on migrating to Vite following the remix docs (https://remix.run/docs/en/main/guides...
    1 ยท 17 days ago
  • general
    ๐Ÿ”ญfoundations
    Solutions video on localhost:5639 ?
    quang ๐Ÿš€ ๐ŸŒŒ:
    Hi, so I'm having a hard time navigating (hopefully will be better with time) The nav on epicweb.de...
    • โœ…1
    9 ยท 10 months ago
  • general
    Remix Vite Plugin
    Binalfew ๐Ÿš€ ๐ŸŒŒ:
    <@105755735731781632> Now that remix officially supports vite (though not stable) what does it mean...
    • โœ…1
    3 ยท 10 months ago
  • general
    Welcome to EpicWeb.dev! Say Hello ๐Ÿ‘‹
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒ:
    This is the first post of many hopefully!
    • 17
    78 ยท a year ago
  • ๐Ÿงชfull stack testing
    Failing tests in full-stack-testing workshop
    Szabolcs ๐ŸŒŒ:
    I am getting this warning in the testing workshop. ``` Warning: `ReactDOMTestUtils.act` is deprecate...
    • โœ…1
    2 ยท 2 months ago
  • general
    Epicshop is now social and mobile friendly!
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒ:
    I'm excited to announce that now the Epic Web workshops are mobile friendly! https://foundations.ep...
    • ๐ŸŽ‰2
    0 ยท 3 months ago
  • ๐Ÿ’พdata
    general
    ๐Ÿ“forms
    ๐Ÿ”ญfoundations
    double underscore?
    trendaaang ๐ŸŒŒ:
    What with the `__note-editor.tsx`? I don't see that in the Remix docs and I don't remember Kent talk...
    • โœ…1
    2 ยท 4 months ago
  • ๐Ÿ”ญfoundations
    ๐Ÿ’พdata
    general
    ๐Ÿ“forms
    ๐Ÿ”auth
    Native Logging
    trendaaang ๐ŸŒŒ:
    I was thinking that it could be useful to log every CRUD operation to help track down errors. Is tha...
    • โœ…1
    6 ยท 5 months ago
  • general
    The video play is pretty laggy currently
    QzCurious ๐ŸŒŒ:
    I thought I should tag you for this <@105755735731781632>. Please take a look if something wrong.
    • โœ…2
    9 ยท 6 months ago
  • general
    New Workshop Scheduled
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒ:
    Hey Epic Web devs! I wanted to let you know before everyone else on here: https://www.epicweb.dev/ev...
    • 2
    0 ยท 6 months ago
  • general
    Deploying an exercise
    Khoi ๐Ÿš€ ๐ŸŒŒ:
    Dear <@105755735731781632> , First of all, I really appreciate your effort in building this EPIC cou...
    • โœ…1
    1 ยท 6 months ago
  • general
    "Start App" throws error: Error: Cannot add empty string to PrefixLookupTrie
    Martin ๐ŸŒŒ:
    โœ— npm run start > start > kcdshop start [playground:4000] [playground:4000] > dev [playground:4000...
    • โœ…1
    7 ยท 10 months ago
  • ๐Ÿงชfull stack testing
    Can't start Playwright ?
    Khoi ๐Ÿš€ ๐ŸŒŒ:
    After running the `npx playwright test --ui` command I got this: I tried to re-install the project f...
    • โœ…2
    6 ยท 6 months ago
  • general
    ๐Ÿ“forms
    Can't start the playground
    trendaaang ๐ŸŒŒ:
    Been a minute since I last worked on this course. Just tried running the app and was notified that t...
    • โœ…1
    3 ยท 7 months ago
  • general
    Question about the Workshop App tabs
    sjollivier ๐ŸŒŒ:
    Just started the course. I might have missed this in the Getting Started video, but how should I be ...
    • โœ…1
    1 ยท 7 months ago
  • general
    "Start App" throws TypeError: Cannot read properties of undefined (reading 'toLowerCase')
    ntin89 ๐ŸŒŒ:
    ```TypeError: Cannot read properties of undefined (reading 'toLowerCase') at userAgentContains (...
    • โœ…1
    4 ยท 7 months ago