Fix/refactor tests

This commit is contained in:
2025-06-16 22:41:31 -07:00
parent dac61e6121
commit c46c361217
15 changed files with 160 additions and 135 deletions

View File

@@ -9,3 +9,8 @@ global.takeScreenshot = async (driver) => {
await mkdir(dir, { recursive: true });
await writeFile(`${dir}/${fileName.replaceAll('/', '-')}`, image, 'base64');
};
global.url = (relativeOrAbsolute) => {
const location = new URL(relativeOrAbsolute, global.testServerUrl);
return location.href;
}

View File

@@ -1,7 +1,6 @@
const { Builder } = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome.js'),
chromeOptions = new chrome.Options(),
{ expect, it } = require('@jest/globals');
chromeOptions = new chrome.Options();
chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');
chromeOptions.enableBidi();
@@ -9,11 +8,12 @@ chromeOptions.enableBidi();
let driver;
beforeEach(async () => {
driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
const builder = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions);
driver = builder.build();
});
it('loads the page', async () => {
await driver.get('http://localhost:3005');
await driver.get(url('/'));
expect(await driver.getTitle()).toEqual('Infantry Combat Solo Basic');
});

View File

@@ -1,44 +1,49 @@
const { Builder, By, until } = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome.js'),
chromeOptions = new chrome.Options(),
{ expect, it } = require('@jest/globals'),
{ readFile } = require('node:fs/promises'),
{ readFile, readdir } = require('node:fs/promises'),
{ HttpResponse } = require('selenium-webdriver/devtools/networkinterceptor');
chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');
chromeOptions.enableBidi();
let driver;
const buildPath = 'build/assets/images';
const defaultScenario = 'scenario-side_show';
const fixtureFilePath = './test/integration/fixtures/scenario-test.svg';
const selected = expect.stringContaining('selected');
const notSelected = expect.not.stringContaining('selected');
let driver, httpResponse;
beforeAll(async () => {
const filenames = await readdir(buildPath);
const scenario = filenames.find(filename => filename.includes(defaultScenario));
httpResponse = new HttpResponse(url(`/assets/images/${scenario}`));
httpResponse.body = await readFile(fixtureFilePath, 'utf8');
httpResponse.addHeaders('Content-Type', 'image/svg+xml');
});
beforeEach(async () => {
driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
const connection = await driver.createCDPConnection('page')
await driver.onIntercept(connection, httpResponse, async () => {});
await driver.get(url('/'));
const mapPlaceholderEl = await driver.findElement(By.css('.map-placeholder'));
await driver.wait(until.elementIsNotVisible(mapPlaceholderEl), 1000);
});
it('selects and deselects a trooper by clicking on its counter', async () => {
const connection = await driver.createCDPConnection('page')
const url = 'http://localhost:3005/assets/images/scenario-side_show.svg';
const httpResponse = new HttpResponse(url);
httpResponse.body = await readFile('./test/integration/fixtures/scenario-test.svg', 'utf8');
httpResponse.addHeaders('Content-Type', 'image/svg+xml');
await driver.onIntercept(connection, httpResponse, async () => {});
await driver.get('http://localhost:3005');
await driver.wait(until.elementLocated(By.css('#dice')), 1000);
const record = await driver.findElement(By.css('.soldier-record'));
const notSelected = expect.not.stringContaining('selected');
const objectEl = await driver.findElement(By.css('object'));
const selector = '.counter[data-allegiance="attacker"][data-number="1"]';
expect(await record.getAttribute('class')).toEqual(notSelected);
const objectEl = await driver.findElement(By.css('object'));
await driver.switchTo().frame(objectEl);
const selector = '.counter[data-allegiance="attacker"][data-number="1"]',
svg = await driver.findElement(By.css('svg')),
counter = await driver.findElement(By.css(selector), svg),
selected = expect.stringContaining('selected');
const svg = await driver.findElement(By.css('svg'));
const counter = await driver.findElement(By.css(selector), svg);
expect(await counter.getAttribute('class')).toEqual(notSelected);
await counter.click();
expect(await counter.getAttribute('class')).toEqual(selected);
@@ -54,37 +59,20 @@ it('selects and deselects a trooper by clicking on its counter', async () => {
});
it('selects and deselects trooper by clicking on its record', async () => {
const connection = await driver.createCDPConnection('page')
const url = 'http://localhost:3005/assets/images/scenario-side_show.svg';
const httpResponse = new HttpResponse(url);
httpResponse.body = await readFile('./test/integration/fixtures/scenario-test.svg', 'utf8');
httpResponse.addHeaders('Content-Type', 'image/svg+xml');
await driver.onIntercept(connection, httpResponse, async () => {});
await driver.get('http://localhost:3005');
await driver.wait(until.elementLocated(By.css('#dice')), 1000);
const record = await driver.findElement(By.css('.soldier-record'));
const notSelected = expect.not.stringContaining('selected');
const objectEl = await driver.findElement(By.css('object'));
const selector = '.counter[data-allegiance="attacker"][data-number="1"]';
expect(await record.getAttribute('class')).toEqual(notSelected);
const objectEl = await driver.findElement(By.css('object'));
await driver.switchTo().frame(objectEl);
const selector = '.counter[data-allegiance="attacker"][data-number="1"]',
svg = await driver.findElement(By.css('svg')),
counter = await driver.findElement(By.css(selector), svg);
const svg = await driver.findElement(By.css('svg'));
const counter = await driver.findElement(By.css(selector), svg);
expect(await counter.getAttribute('class')).toEqual(notSelected);
await driver.switchTo().defaultContent();
await record.click();
const selected = expect.stringContaining('selected');
expect(await record.getAttribute('class')).toEqual(selected);
await driver.switchTo().frame(objectEl);
expect(await counter.getAttribute('class')).toEqual(selected);
@@ -92,36 +80,21 @@ it('selects and deselects trooper by clicking on its record', async () => {
await record.click();
expect(await record.getAttribute('class')).toEqual(notSelected);
await driver.switchTo().frame(objectEl);
expect(await counter.getAttribute('class')).toEqual(notSelected);
});
it('selects a trooper by clicking on its counter and deselects it by clicking on its record', async () => {
const connection = await driver.createCDPConnection('page')
const url = 'http://localhost:3005/assets/images/scenario-side_show.svg';
const httpResponse = new HttpResponse(url);
httpResponse.body = await readFile('./test/integration/fixtures/scenario-test.svg', 'utf8');
httpResponse.addHeaders('Content-Type', 'image/svg+xml');
await driver.onIntercept(connection, httpResponse, async () => {});
await driver.get('http://localhost:3005');
await driver.wait(until.elementLocated(By.css('#dice')), 1000);
const record = await driver.findElement(By.css('.soldier-record'));
const notSelected = expect.not.stringContaining('selected');
const objectEl = await driver.findElement(By.css('object'));
const selector = '.counter[data-allegiance="attacker"][data-number="1"]';
expect(await record.getAttribute('class')).toEqual(notSelected);
const objectEl = await driver.findElement(By.css('object'));
await driver.switchTo().frame(objectEl);
const selector = '.counter[data-allegiance="attacker"][data-number="1"]',
svg = await driver.findElement(By.css('svg')),
counter = await driver.findElement(By.css(selector), svg),
selected = expect.stringContaining('selected');
const svg = await driver.findElement(By.css('svg'));
const counter = await driver.findElement(By.css(selector), svg);
expect(await counter.getAttribute('class')).toEqual(notSelected);
await counter.click();
expect(await counter.getAttribute('class')).toEqual(selected);
@@ -131,43 +104,25 @@ it('selects a trooper by clicking on its counter and deselects it by clicking on
await record.click();
expect(await record.getAttribute('class')).toEqual(notSelected);
await driver.switchTo().frame(objectEl);
expect(await counter.getAttribute('class')).toEqual(notSelected);
});
it('selects a trooper by clicking on its record and deselects it by clicking on its counter', async () => {
const connection = await driver.createCDPConnection('page')
const url = 'http://localhost:3005/assets/images/scenario-side_show.svg';
const httpResponse = new HttpResponse(url);
httpResponse.body = await readFile('./test/integration/fixtures/scenario-test.svg', 'utf8');
httpResponse.addHeaders('Content-Type', 'image/svg+xml');
await driver.onIntercept(connection, httpResponse, async () => {});
await driver.get('http://localhost:3005');
await driver.wait(until.elementLocated(By.css('#dice')), 1000);
const record = await driver.findElement(By.css('.soldier-record'));
const notSelected = expect.not.stringContaining('selected');
const objectEl = await driver.findElement(By.css('object'));
const selector = '.counter[data-allegiance="attacker"][data-number="1"]';
expect(await record.getAttribute('class')).toEqual(notSelected);
const objectEl = await driver.findElement(By.css('object'));
await driver.switchTo().frame(objectEl);
const selector = '.counter[data-allegiance="attacker"][data-number="1"]',
svg = await driver.findElement(By.css('svg')),
counter = await driver.findElement(By.css(selector), svg);
const svg = await driver.findElement(By.css('svg'));
const counter = await driver.findElement(By.css(selector), svg);
expect(await counter.getAttribute('class')).toEqual(notSelected);
await driver.switchTo().defaultContent();
await record.click();
const selected = expect.stringContaining('selected');
expect(await record.getAttribute('class')).toEqual(selected);
await driver.switchTo().frame(objectEl);
expect(await counter.getAttribute('class')).toEqual(selected);

View File

@@ -1,7 +1,7 @@
console.log('\nSpawning server process...');
const { spawn } = require('child_process');
module.exports = async function () {
module.exports = async function (globalConfig, projectConfig) {
const child = spawn('node', ['server.cjs']);
child.stdout.on('data', (data) => {
@@ -13,7 +13,7 @@ module.exports = async function () {
child.stderr.on('data', (data) => {
const str = data.toString();
console.log('[server]', str);
if (str.includes('localhost:3005')) {
if (str.includes(projectConfig.globals.testServerUrl)) {
setTimeout(resolve, 200);
}
});