test(e2e): playwright - two browsers play online, server shop, offline campaign (P4-2)
This commit is contained in:
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();
|
||||
});
|
||||
Reference in New Issue
Block a user