sinpro.dev
Docs
Git
Git Hooks
  • Company Information
    • Portfolio
    • About
    • Services
    • Events
  • Dev Environment
    • macOS Settings
    • Chrome Extensions
    • VSCode Workspace Settings
    • VSCode Workspace Extensions
    • Global NPM Packages
    • npm-check-updates
    • SvelteKit
    • Customize Zsh
    • ChatGPT Prompts
    • Tailwind CSS
    • Warp
    • Keyboard Shortcuts
  • Assets
    • Assets
    • JPG/PNG to AVIF
    • TTF to WOFF2
  • Clean Code
    • Clean Code
    • Format
    • Quality
    • Variables
    • Functions
    • Objects and Data Structures
    • Classes
    • Concurrency
    • Error handling
    • Comments
  • Code Style
    • TypeScript Config
    • Prettier
    • ESLint
    • Stylelint
  • Testing
    • Vitest
    • Playwright
  • Git
    • Git Branches and Commits
    • Git Hooks
    • Git User Profiles
    • Git for Windows
  • GitHub
    • GitHub Issues
    • GitHub Pull Requests
    • GitHub Repository Settings
    • GitHub Branch Protection
    • GitHub Actions
  • Code Quality
    • SonarCloud Coverage
  • Server
    • SSH
    • PM2
    • Caddy
    • Updating the server
    • ngrok
  • Team sinProject
    • Our Team Policy
    • Equipment and Supplies
    • Books
    • Slack
    • Locales
    • Funny Apps
    • Docs History
  • Talk
    • Talk
    • Creating a Project
    • App Structure

Testing

Playwright

Edit this page

How we automate our tests using Playwright.

Playwright is an E2E (end-to-end) testing framework.

Installation

Shell Session
$ npm init playwright@latest

Getting started with writing end-to-end tests with Playwright:
Initializing project in '.'
✔ Where to put your end-to-end tests? · tests
✔ Add a GitHub Actions workflow? (y/N) · N
✔ Install Playwright browsers (can be done manually via 'npx playwright install')? (Y/n) · Y

File Name

Create a tests directory, and name the files as *.spec.ts.

Configuration

Basic changes

Change the test directory, timeout duration, and other settings as needed.

Configure the reporter, and output videos when an error occurs.

playwright.config.ts
const config: PlaywrightTestConfig = {
	reporter: 'html',
	use: {
		video: 'retain-on-failure',
	},
}

Playwright - TestConfig >

Target Browsers

Ensure that tests are not run on browsers where testing is not necessary.

[talk]playwright.config.ts
const config: PlaywrightTestConfig = {
	projects: [
		{
			name: 'chromium',
			use: {
				...devices['Desktop Chrome'],
			},
		},

		// {
		//   name: 'firefox',
		//   use: {
		//     ...devices['Desktop Firefox'],
		//   },
		// },

		// ...
	],
}

Web Server

To perform tests quickly, use a development server.

playwright.config.ts
const config: PlaywrightTestConfig = {
	webServer: {
		command: 'npm run dev',
		port: 5173,
		reuseExistingServer: !process.env.CI,
	},
	// webServer: {
	// 	command: 'npm run build && npm run preview',
	// 	port: 4173,
	// },
}

More information >

Setup

Specify processes to be executed beforehand, such as logging in. Add dependencies to the browser settings.

[talk]playwright.config.ts
const config: PlaywrightTestConfig = {
	projects: [
		{ name: 'setup', testMatch: /.*\.setup\.ts/ },

		{
			name: 'chromium',
			use: {
				...devices['Desktop Chrome'],
			},
			dependencies: ['setup'],
		},
	],
}

Scripts

We have prepared the following scripts to execute Playwright.

package.json
{
	"scripts": {
		"test:integration": "playwright test",
	}
}

VSCode Extension

Use VSCode Extension for testing through VSCode.

Running tests

  • Running all tests
Bash
npx playwright test
  • Running a single test file
Bash
npx playwright test <filename>

More Info >

Running Codegen

Use the codegen command to run the test generator followed by the URL of the website you want to generate tests for. The URL is optional and you can always run the command without it and then add the URL directly into the browser window instead.

Bash
npx playwright codegen localhost:5173

More Info >

Reporters

A quick way of opening the last test run report is:

Bash
npx playwright show-report

More Info >

Locate by test id

To make it easier to identify an Element from Playwright, “data-testid” can be used.

src/routes/docs/[slug]/+page.svelte
<a data-testid="next-page">Next Page</a>
e2e/docs.spec.ts
await page.getByTestId('next-page').click()

More Information >

Sample Code

[talk]e2e/chat.spec.ts
import { Page, expect, test } from '@playwright/test'

test.beforeEach(async ({ page }) => {
	await page.goto('./chat', { waitUntil: 'networkidle' })
})

test('before sign in', async ({ page }) => {
	await expect(page).toHaveTitle('Talk - Sign in')
})

In cases where common setup is needed, such as for login processes:

[talk]e2e/chat.spec.ts
import { Page, expect, test } from '@playwright/test'
import { auth_file_path } from './lib/setup.js'

test.beforeEach(async ({ page }) => {
	await page.goto('./chat', { waitUntil: 'networkidle' })
})

test.describe('after sign in', () => {
	test.use({ storageState: auth_file_path })

	test('has title', async ({ page }) => {
		await expect(page).toHaveTitle('Talk - Chat')
	})

	test('init focus', async ({ page }) => {
		const name = page.getByPlaceholder('Name')
		await expect(name).toBeFocused()
	})
})
Vitest Git Branches and Commits
© sinProject. v0.73.0
On this page
  • Playwright
  • Installation
  • File Name
  • Configuration
  • Basic changes
  • Target Browsers
  • Web Server
  • Setup
  • Scripts
  • VSCode Extension
  • Running tests
  • Running Codegen
  • Reporters
  • Locate by test id
  • Sample Code