Move tests from Selenium & Mocha to Playwright

This commit is contained in:
varjolintu 2022-03-18 16:52:02 +02:00
parent 97e07b5a62
commit ecae40a6b6
4 changed files with 48 additions and 76 deletions

View file

@ -16,7 +16,7 @@
},
"scripts": {
"build": "node build.js",
"tests": "npx playwright test tests/tests.js"
"tests": "npx playwright test"
},
"repository": {
"type": "git",

View file

@ -30,6 +30,7 @@ const config = {
},
},
],
testMatch: 'content-script-tests.js',
};
module.exports = config;

View file

@ -0,0 +1,46 @@
'use strict';
const { test, expect } = require('@playwright/test');
const fileUrl = require('file-url');
const DEST = 'keepassxc-browser/tests';
let page;
test.describe('Content script tests', () => {
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto(fileUrl(`${DEST}/tests.html`));
});
test('General tests', async () => {
await verifyResults('general-results');
});
test('Input field matching tests', async() => {
await verifyResults('input-field-results');
});
test('Search field tests', async () => {
await verifyResults('search-field-results');
});
test('TOTP field tests', async () => {
await verifyResults('totp-field-results');
});
test('Password change tests', async () => {
await verifyResults('password-change-results');
});
});
const verifyResults = async(selector) => {
const resultCount = await page.locator(`css=#${selector} >> css=.fa`).count();
await expect.soft(resultCount).toBeGreaterThan(0);
for (var i = 0; i < resultCount; i++) {
const elem = await page.locator(`css=#${selector} >> css=.fa`).nth(i);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
}
};

View file

@ -1,75 +0,0 @@
'use strict';
const { test, expect } = require('@playwright/test');
const fileUrl = require('file-url');
const DEST = 'keepassxc-browser/tests';
test.beforeEach(async ({ page }) => {
await page.goto(fileUrl(`${DEST}/tests.html`));
});
test.describe('Content script tests', () => {
test('General tests', async ({ page }) => {
const resultCount = await page.locator('css=#general-results >> css=.fa').count();
await expect.soft(resultCount).toBeGreaterThan(0);
for (var i = 0; i < resultCount; i++) {
const elem = await page.locator('css=#general-results >> css=.fa').nth(i);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
}
});
test('Input field matching tests', async ({ page }) => {
const resultCount = await page.locator('css=#input-field-results >> css=.fa').count();
await expect.soft(resultCount).toBeGreaterThan(0);
for (var i = 0; i < resultCount; i++) {
const elem = await page.locator('css=#input-field-results >> css=.fa').nth(i);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
}
});
test('Search field tests', async ({ page }) => {
const resultCount = await page.locator('css=#search-field-results >> css=.fa').count();
await expect.soft(resultCount).toBeGreaterThan(0);
for (var i = 0; i < resultCount; i++) {
const elem = await page.locator('css=#search-field-results >> css=.fa').nth(i);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
}
});
test('TOTP field tests', async ({ page }) => {
const resultCount = await page.locator('css=#totp-field-results >> css=.fa').count();
await expect.soft(resultCount).toBeGreaterThan(0);
for (var i = 0; i < resultCount; i++) {
const elem = await page.locator('css=#totp-field-results >> css=.fa').nth(i);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
}
});
test('Password change tests', async ({ page }) => {
const resultCount = await page.locator('css=#password-change-results >> css=.fa').count();
await expect.soft(resultCount).toBeGreaterThan(0);
for (var i = 0; i < resultCount; i++) {
const elem = await page.locator('css=#password-change-results >> css=.fa').nth(i);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
}
});
});
const verifyResults = async(page, selector) => {
const resultCount = await page.locator(`css=#${selector} >> css=.fa`).count();
const elem = await page.locator(`css=#${selector} >> css=.fa`).nth(0);
const id = await elem.getAttribute('id');
await expect.soft(elem, id).toHaveClass('fa fa-check');
};