const { Builder, By } = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome.js'),
{ expect, it } = require('@jest/globals'),
chromeOptions = new chrome.Options(),
{ mkdir, writeFile } = require('node:fs/promises'),
path = require('path');
chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');
let driver;
beforeEach(async () => {
driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
await driver.get('http://localhost:3005');
});
it('loads the page', async () => {
expect(await driver.getTitle()).toEqual('Infantry Combat Solo Basic');
});
it('selects an off-board soldier', async () => {
await driver.switchTo().frame(await driver.findElement(By.css('object')));
const selector = '.counter[data-allegiance="attacker"][data-number="1"]',
svg = await driver.findElement(By.css('svg')),
counter = await driver.findElement(By.css(selector), svg);
await counter.click();
expect(await counter.getAttribute('class')).toEqual(expect.stringContaining('selected'));
});
afterEach(async () => {
await driver.quit();
});
async function takeScreenshot(driver) {
const dir = './test/screenshots';
const fileName = path.relative(process.cwd(), __filename) + ' "' + expect.getState().currentTestName + `" ${new Date().toISOString()}.png`;
const image = await driver.takeScreenshot();
await mkdir(dir, { recursive: true });
await writeFile(`${dir}/${fileName.replaceAll('/', '-')}`, image, 'base64');
}