test(e2e): playwright - two browsers play online, server shop, offline campaign (P4-2)
This commit is contained in:
31
tests/e2e/helpers.js
Normal file
31
tests/e2e/helpers.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/* Shared steps for the browser tests: log in through the real UI, get from the workshop into the lobby. */
|
||||
'use strict';
|
||||
const { expect } = require('@playwright/test');
|
||||
|
||||
const mode = page => page.locator('body').getAttribute('data-mode');
|
||||
|
||||
// open the game in online mode and log in with name + password through the login card
|
||||
async function login(page, name, pass) {
|
||||
await page.goto('/?online');
|
||||
await page.locator('#oLogin').fill(name);
|
||||
await page.locator('#oPass').fill(pass);
|
||||
await page.locator('#oGo').click();
|
||||
await chooseCountry(page);
|
||||
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
|
||||
await expect(page.locator('#hUser')).toContainText(name);
|
||||
}
|
||||
|
||||
// first start asks for a country - take the default
|
||||
async function chooseCountry(page) {
|
||||
const go = page.locator('#cfGo');
|
||||
try { await go.waitFor({ timeout: 4000 }); await go.click(); } catch (e) { /* already chosen */ }
|
||||
}
|
||||
|
||||
async function toLobby(page) {
|
||||
await page.locator('#bLeague').click();
|
||||
await expect(page.locator('body')).toHaveAttribute('data-mode', 'lobby');
|
||||
}
|
||||
|
||||
const credits = async page => Number((await page.locator('#hMoney').textContent()).replace(/[^\d]/g, ''));
|
||||
|
||||
module.exports = { mode, login, chooseCountry, toLobby, credits };
|
||||
28
tests/e2e/offline.spec.js
Normal file
28
tests/e2e/offline.spec.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// The offline campaign must keep working without any server (double-click on game/index.html = file://).
|
||||
'use strict';
|
||||
const { test, expect } = require('@playwright/test');
|
||||
const path = require('path');
|
||||
const { pathToFileURL } = require('url');
|
||||
|
||||
const file = pathToFileURL(path.join(__dirname, '..', '..', 'game', 'index.html')).href;
|
||||
|
||||
test('campaign runs from file:// : training fight with the AI pilot, layout check passes', async ({ page }) => {
|
||||
const errors = [];
|
||||
page.on('pageerror', e => errors.push(e.message));
|
||||
await page.goto(file + '?play&fight=2&auto&ff=20&uicheck');
|
||||
await expect(page.locator('body')).toHaveAttribute('data-gmode', 'campaign');
|
||||
await expect(page.locator('body')).toHaveAttribute('data-mode', /fight|result/);
|
||||
await expect(page.locator('#uicheck')).toContainText('UI ok');
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('campaign workshop: buying a part works offline', async ({ page }) => {
|
||||
await page.goto(file + '?play');
|
||||
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
|
||||
const money = async () => Number((await page.locator('#hMoney').textContent()).replace(/[^\d]/g, ''));
|
||||
const before = await money();
|
||||
await page.locator('.tab[data-slot="armour"]').click();
|
||||
await page.locator('#cards .item:not(:has(.badge)):not(:has(.pr.no))').first().click();
|
||||
await page.locator('#detail button[data-act="buy"]').click();
|
||||
expect(await money()).toBeLessThan(before);
|
||||
});
|
||||
90
tests/e2e/online.spec.js
Normal file
90
tests/e2e/online.spec.js
Normal file
@@ -0,0 +1,90 @@
|
||||
// Two real browsers play a live fight through the server: login, lobby, challenge, accept, fight, give up, result.
|
||||
'use strict';
|
||||
const { test, expect } = require('@playwright/test');
|
||||
const { login, toLobby, credits, chooseCountry } = require('./helpers');
|
||||
|
||||
test('michi challenges benni, benni gives up: mirrored result and server-booked rewards', async ({ browser }) => {
|
||||
const ctxA = await browser.newContext(), ctxB = await browser.newContext();
|
||||
const a = await ctxA.newPage(), b = await ctxB.newPage();
|
||||
const errors = [];
|
||||
for (const p of [a, b]) p.on('pageerror', e => errors.push(e.message));
|
||||
|
||||
await login(a, 'michi', 'Michi2000!');
|
||||
await login(b, 'benni', 'Benni2000!');
|
||||
const moneyA = await credits(a), moneyB = await credits(b);
|
||||
await toLobby(a);
|
||||
await toLobby(b);
|
||||
|
||||
// each sees the other in the lobby
|
||||
await expect(a.locator('.prow', { hasText: 'benni' })).toBeVisible();
|
||||
await expect(b.locator('.prow', { hasText: 'michi' })).toBeVisible();
|
||||
|
||||
// challenge -> incoming card -> accept -> both are in the fight
|
||||
await a.locator('.prow', { hasText: 'benni' }).locator('.chal').click();
|
||||
await expect(a.locator('#lCancel')).toBeVisible();
|
||||
await expect(b.locator('#cYes')).toBeVisible();
|
||||
await b.locator('#cYes').click();
|
||||
await expect(a.locator('body')).toHaveAttribute('data-mode', 'fight');
|
||||
await expect(b.locator('body')).toHaveAttribute('data-mode', 'fight');
|
||||
await expect(a.locator('#fN1')).toHaveText('benni');
|
||||
await expect(b.locator('#fN1')).toHaveText('michi');
|
||||
|
||||
// the fight runs on the server: after the countdown michi drives - the clock must tick down
|
||||
await a.waitForTimeout(4500);
|
||||
await a.keyboard.down('w');
|
||||
await a.waitForTimeout(1500);
|
||||
await a.keyboard.up('w');
|
||||
await expect(a.locator('#fightHud')).not.toContainText('2:00');
|
||||
|
||||
// benni gives up through the pause card (live: no pause, the fight keeps running)
|
||||
await b.keyboard.press('p');
|
||||
await expect(b.locator('#bGiveUp')).toBeVisible();
|
||||
await b.locator('#bGiveUp').click();
|
||||
|
||||
// result: michi wins, benni loses - never both "victory" (the old mirrored-result bug)
|
||||
await expect(a.locator('body')).toHaveAttribute('data-mode', 'result', { timeout: 20000 });
|
||||
await expect(b.locator('body')).toHaveAttribute('data-mode', 'result', { timeout: 20000 });
|
||||
await expect(a.locator('#overlay h1')).toHaveText(/victory/i);
|
||||
await expect(b.locator('#overlay h1')).toHaveText(/defeat/i);
|
||||
|
||||
// rewards came from the server and survive a reload (server is the authority)
|
||||
const afterA = await credits(a), afterB = await credits(b);
|
||||
expect(afterA).toBeGreaterThan(moneyA);
|
||||
expect(afterB).toBeGreaterThanOrEqual(moneyB);
|
||||
await a.reload();
|
||||
await chooseCountry(a);
|
||||
await expect(a.locator('body')).toHaveAttribute('data-mode', 'garage');
|
||||
await expect.poll(() => credits(a)).toBe(afterA);
|
||||
|
||||
expect(errors, 'no JavaScript errors in either browser').toEqual([]);
|
||||
await ctxA.close(); await ctxB.close();
|
||||
});
|
||||
|
||||
test('shop purchase is checked and stored by the server', async ({ browser }) => {
|
||||
const ctx = await browser.newContext();
|
||||
const page = await ctx.newPage();
|
||||
await page.goto('/?online');
|
||||
await page.locator('#oAnon').click(); // guest account on the server
|
||||
await chooseCountry(page);
|
||||
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
|
||||
const before = await credits(page);
|
||||
|
||||
await page.locator('.tab[data-slot="armour"]').click();
|
||||
const item = page.locator('#cards .item:not(:has(.badge)):not(:has(.pr.no))').first();
|
||||
await item.click();
|
||||
await page.locator('#detail button[data-act="buy"]').click();
|
||||
await expect.poll(() => credits(page)).toBeLessThan(before);
|
||||
const after = await credits(page);
|
||||
|
||||
// what the server stored
|
||||
const token = await page.evaluate(() => JSON.parse(localStorage.getItem('robotclash.net.session')).idToken);
|
||||
const res = await page.request.get('/api/online', { headers: { Authorization: 'Bearer ' + token } });
|
||||
const srv = await res.json();
|
||||
expect(srv.money).toBe(after);
|
||||
expect(srv.build.armour).toBeTruthy();
|
||||
|
||||
// the client cannot simply give itself money
|
||||
const cheat = await page.request.put('/api/online/save', { headers: { Authorization: 'Bearer ' + token }, data: { money: 999999 } });
|
||||
expect((await cheat.json()).money).toBe(after);
|
||||
await ctx.close();
|
||||
});
|
||||
17
tests/e2e/server.js
Normal file
17
tests/e2e/server.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/* Server for the browser tests: fresh database file every run, seed accounts michi / benni, then the real app. */
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const file = path.join(os.tmpdir(), 'robotclash-e2e.sqlite');
|
||||
for (const ext of ['', '-wal', '-shm']) fs.rmSync(file + ext, { force: true });
|
||||
process.env.DB_FILE = file;
|
||||
process.env.NODE_ENV = 'test'; // high auth rate limit, no secret warning
|
||||
process.env.PORT = process.env.PORT || '8099';
|
||||
|
||||
const { createUser } = require('../../server/auth');
|
||||
const economy = require('../../server/economy');
|
||||
for (const [name, pass] of [['michi', 'Michi2000!'], ['benni', 'Benni2000!']]) economy.load(createUser({ name, pass, role: 'admin' }).uid);
|
||||
|
||||
require('../../server/app').listen().then(srv => console.log('e2e server on :' + srv.address().port));
|
||||
Reference in New Issue
Block a user