|
| 1 | +# Database Testing with Playwright (SQLite Example) |
| 2 | + |
| 3 | +This guide explains how to perform database integration testing in the K11TechLab Playwright JavaScript framework using a sample SQLite database. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Why DB Testing? |
| 8 | +- Validate backend data consistency after UI/API actions |
| 9 | +- Ensure end-to-end flows update the database as expected |
| 10 | +- Enable data-driven test scenarios |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Sample Setup: SQLite |
| 15 | + |
| 16 | +### 1. Sample Database |
| 17 | +- The file `testdata/sample_db.sqlite` is a SQLite database with a `users` table and sample data. |
| 18 | +- The schema and data are defined in `testdata/sample_db.sql`. |
| 19 | +- To recreate the database, run: |
| 20 | + ```bash |
| 21 | + node testdata/create_sample_db.js |
| 22 | + ``` |
| 23 | + |
| 24 | +### 2. DB Test Example |
| 25 | +File: `tests/db/SampleSQLiteDB.spec.js` |
| 26 | +```js |
| 27 | +const { test, expect } = require('@playwright/test'); |
| 28 | +const sqlite3 = require('sqlite3').verbose(); |
| 29 | +const path = require('path'); |
| 30 | + |
| 31 | +const dbPath = path.join(__dirname, '../../testdata/sample_db.sqlite'); |
| 32 | + |
| 33 | +function runQuery(db, query) { |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + db.all(query, (err, rows) => { |
| 36 | + if (err) reject(err); |
| 37 | + else resolve(rows); |
| 38 | + }); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +test.describe('Sample SQLite DB Integration', () => { |
| 43 | + let db; |
| 44 | + test.beforeAll(async () => { |
| 45 | + db = new sqlite3.Database(dbPath); |
| 46 | + }); |
| 47 | + test.afterAll(async () => { |
| 48 | + db.close(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should fetch all users from the users table', async () => { |
| 52 | + const rows = await runQuery(db, 'SELECT * FROM users;'); |
| 53 | + expect(rows.length).toBeGreaterThan(0); |
| 54 | + expect(rows[0]).toHaveProperty('username'); |
| 55 | + expect(rows[0]).toHaveProperty('email'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should fetch admin user by username', async () => { |
| 59 | + const rows = await runQuery(db, "SELECT * FROM users WHERE username = 'admin';"); |
| 60 | + expect(rows.length).toBe(1); |
| 61 | + expect(rows[0].email).toBe('admin@example.com'); |
| 62 | + }); |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Best Practices |
| 69 | +- Use a dedicated test database (never production!) |
| 70 | +- Clean up or reset data between tests if needed |
| 71 | +- Use environment variables for DB credentials in real DBs |
| 72 | +- For other DBs (Postgres, SQL Server), see `utils/DBActions.js` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## References |
| 77 | +- [sqlite3 Node.js package](https://www.npmjs.com/package/sqlite3) |
| 78 | +- [Playwright Test Docs](https://playwright.dev/docs/test-intro) |
| 79 | +- [DBActions Utility](../utils/DBActions.js) |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +**This example can be extended for other databases and more advanced scenarios.** |
0 commit comments