Web Dev Solutions

Catalin Mititiuc

From 163e3a9de59f5d6a8df38fa32a1e7dc4db1ad024 Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Sat, 1 Jun 2024 17:50:58 -0700 Subject: Fix/refactor tests --- test/integration/helpers.cjs | 5 ++ test/integration/page.test.js | 8 +-- test/integration/select.test.js | 127 +++++++++++++--------------------------- test/integration/setup.cjs | 4 +- 4 files changed, 52 insertions(+), 92 deletions(-) (limited to 'test/integration') diff --git a/test/integration/helpers.cjs b/test/integration/helpers.cjs index 5643bfd..7e3e0fd 100644 --- a/test/integration/helpers.cjs +++ b/test/integration/helpers.cjs @@ -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; +} diff --git a/test/integration/page.test.js b/test/integration/page.test.js index 8bb703e..f06a7d1 100644 --- a/test/integration/page.test.js +++ b/test/integration/page.test.js @@ -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'); }); diff --git a/test/integration/select.test.js b/test/integration/select.test.js index 2594179..d9b632e 100644 --- a/test/integration/select.test.js +++ b/test/integration/select.test.js @@ -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'; -beforeEach(async () => { - driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build(); -}); +const selected = expect.stringContaining('selected'); +const notSelected = expect.not.stringContaining('selected'); -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'); +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('http://localhost:3005'); - await driver.wait(until.elementLocated(By.css('#dice')), 1000); + 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 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); diff --git a/test/integration/setup.cjs b/test/integration/setup.cjs index 2ea9e63..cc68a59 100644 --- a/test/integration/setup.cjs +++ b/test/integration/setup.cjs @@ -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); } }); -- cgit v1.2.3