From c5eb0742346f033a273351d649ab36d9846b8aa8 Mon Sep 17 00:00:00 2001 From: hayzam Date: Wed, 7 Feb 2024 05:59:45 +0530 Subject: [PATCH] clean: use best practices --- .husky/pre-commit | 1 + package-lock.json | 3 ++- package.json | 5 +++-- tests/auth.setup.js | 6 +++--- tests/dashboards/router.spec.js | 18 ++++++++---------- tests/dashboards/telephony.spec.js | 20 +++++++++----------- tests/network/guest-lan.spec.js | 3 +-- 7 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..3867a0f --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1 @@ +npm run lint diff --git a/package-lock.json b/package-lock.json index 9114617..1822fc0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,13 +10,13 @@ "license": "ISC", "dependencies": { "dotenv": "^16.4.1", - "husky": "^9.0.10", "ip-address": "^9.0.5", "ip-cidr": "^4.0.0" }, "devDependencies": { "@playwright/test": "^1.41.2", "@types/node": "^20.11.16", + "husky": "^9.0.10", "semistandard": "^17.0.0" } }, @@ -1613,6 +1613,7 @@ "version": "9.0.10", "resolved": "https://registry.npmjs.org/husky/-/husky-9.0.10.tgz", "integrity": "sha512-TQGNknoiy6bURzIO77pPRu+XHi6zI7T93rX+QnJsoYFf3xdjKOur+IlfqzJGMHIK/wXrLg+GsvMs8Op7vI2jVA==", + "dev": true, "bin": { "husky": "bin.mjs" }, diff --git a/package.json b/package.json index de88467..acc7dc1 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "type": "module", "scripts": { "lint": "semistandard --fix tests/**/*.js", - "test": "playwright test" + "test": "playwright test", + "prepare": "npm run lint" }, "husky": { "hooks": { @@ -19,11 +20,11 @@ "devDependencies": { "@playwright/test": "^1.41.2", "@types/node": "^20.11.16", + "husky": "^9.0.10", "semistandard": "^17.0.0" }, "dependencies": { "dotenv": "^16.4.1", - "husky": "^9.0.10", "ip-address": "^9.0.5", "ip-cidr": "^4.0.0" } diff --git a/tests/auth.setup.js b/tests/auth.setup.js index f545059..c0efc6b 100644 --- a/tests/auth.setup.js +++ b/tests/auth.setup.js @@ -5,9 +5,9 @@ const authFile = 'playwright/.auth/user.json'; test('Authenticate', async ({ page }) => { await page.goto('/login'); await page.waitForSelector('input[name="user"]', { timeout: 3000 }); - await page.fill('input[name="user"]', process.env.ADMIN_USER); - await page.fill('input[name="password"]', process.env.ADMIN_PASS); - await page.click('#subBut'); + await page.getByPlaceholder('Username').fill( process.env.ADMIN_USER); + await page.getByPlaceholder('Password').fill(process.env.ADMIN_PASS); + await page.getByRole('link', { name: 'Login' }).click(); await page.waitForSelector('span:has-text("Quick Actions")', { timeout: 3000 }); expect(await page.title()).toBe('Difuse - Dashboard - Router'); await page.context().storageState({ path: authFile }); diff --git a/tests/dashboards/router.spec.js b/tests/dashboards/router.spec.js index f19452a..500c281 100644 --- a/tests/dashboards/router.spec.js +++ b/tests/dashboards/router.spec.js @@ -1,23 +1,23 @@ import { test, expect } from '@playwright/test'; +test.beforeEach(async ({ page }) => { + await page.goto('/', { waitUntil: 'networkidle' }); +}); + test('Title', async ({ page }) => { - await page.goto('/'); const title = await page.title(); expect(title).toBe('Difuse - Dashboard - Router'); }); test('Top Cards', async ({ page }) => { - await page.goto('/'); - const lanClients = await page.$('h4:has-text("LAN Clients")'); - const wifiClients = await page.$('h4:has-text("5 GHz Clients")'); - const wgEndpoints = await page.$('h4:has-text("Peers")'); + const lanClients = await page.getByRole('heading', { name: 'LAN Clients' }); + const wifiClients = await page.getByRole('heading', { name: 'GHz & 5 GHz Clients' }); + const wgEndpoints = await page.getByRole('heading', { name: 'Peers & Clients' }); expect(lanClients && wifiClients && wgEndpoints).toBeTruthy(); }); test('Basic System Information', async ({ page }) => { - await page.goto('/'); - const cells = (await page.$$('.tabulator-cell')).filter(async (cell) => { const field = await cell.getAttribute('tabulator-field'); return field === 'value'; @@ -41,7 +41,5 @@ test('Basic System Information', async ({ page }) => { }); test('Network Information', async ({ page }) => { - await page.goto('/'); - const networkInfo = await page.$('th:has-text("Network Information")'); - expect(networkInfo).toBeTruthy(); + expect(await page.$('th:has-text("Network Information")')).toBeTruthy(); }); diff --git a/tests/dashboards/telephony.spec.js b/tests/dashboards/telephony.spec.js index 6d12886..826032a 100644 --- a/tests/dashboards/telephony.spec.js +++ b/tests/dashboards/telephony.spec.js @@ -1,30 +1,28 @@ import { test, expect } from '@playwright/test'; +test.beforeEach(async ({ page }) => { + await page.goto('/telephony', { waitUntil: 'networkidle' }); +}); + test('Title', async ({ page }) => { - await page.goto('/telephony'); const title = await page.title(); expect(title).toBe('Difuse - Dashboard - Telephony'); }); test('Top Cards', async ({ page }) => { - await page.goto('/telephony'); - const sipExtensions = await page.$('h3:has-text("SIP Extensions")'); - const sipTrunks = await page.$('h3:has-text("SIP Trunks")'); - const currentChannels = await page.$('h3:has-text("Current Channels")'); - const completedCalls = await page.$('h3:has-text("Completed Calls")'); + const sipExtensions = await page.getByRole('heading', { name: 'SIP Extensions' }); + const sipTrunks = await page.getByRole('heading', { name: 'SIP Trunks' }); + const currentChannels = await page.getByRole('heading', { name: 'Current Channels' }); + const completedCalls = await page.getByRole('heading', { name: 'Completed Calls' }); expect(sipExtensions && sipTrunks && currentChannels && completedCalls).toBeTruthy(); }); test('Endpoint Table', async ({ page }) => { - await page.goto('/telephony'); - await page.waitForSelector('.tabulator-header', { timeout: 5000 }); - expect(await page.$('.tabulator-header')).toBeTruthy(); + expect(await page.getByText('Page Size')).toBeTruthy(); }); test('PBX Information', async ({ page }) => { - await page.goto('/telephony'); - const yearRegex = /\b\d{4}\b/; const coreReloadText = await page.textContent('td:has-text("Core Last Reload") + td'); diff --git a/tests/network/guest-lan.spec.js b/tests/network/guest-lan.spec.js index 2f393c3..0fc9e62 100644 --- a/tests/network/guest-lan.spec.js +++ b/tests/network/guest-lan.spec.js @@ -2,8 +2,7 @@ import { test, expect } from '@playwright/test'; import { isValidMacAddress, isValidCidr } from '../../utils/utils.js'; test.beforeEach(async ({ page }) => { - await page.goto('/network/guest-lan'); - await page.waitForTimeout(2500); + await page.goto('/network/guest-lan', { waitUntil: 'networkidle' }); }); test('Title', async ({ page }) => {