Web Dev Solutions

Catalin Mititiuc

From 2df718998a89cb622bb9a76655abdeafc33d9aa5 Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Tue, 28 May 2024 17:13:56 -0700 Subject: Add some select tests --- test/integration/select.test.js | 192 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 test/integration/select.test.js (limited to 'test/integration/select.test.js') diff --git a/test/integration/select.test.js b/test/integration/select.test.js new file mode 100644 index 0000000..69eea00 --- /dev/null +++ b/test/integration/select.test.js @@ -0,0 +1,192 @@ +const { Builder, By, until } = require('selenium-webdriver'), + chrome = require('selenium-webdriver/chrome.js'), + chromeOptions = new chrome.Options(), + { expect, it } = require('@jest/globals'), + { mkdir, writeFile, readFile } = require('node:fs/promises'), + path = require('path'), + { HttpResponse } = require('selenium-webdriver/devtools/networkinterceptor'); + +chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox'); +chromeOptions.enableBidi(); + +let driver; + +beforeEach(async () => { + driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build(); +}); + +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'); + + 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'); + + expect(await counter.getAttribute('class')).toEqual(notSelected); + await counter.click(); + + expect(await counter.getAttribute('class')).toEqual(selected); + await driver.switchTo().defaultContent(); + expect(await record.getAttribute('class')).toEqual(selected); + + await driver.switchTo().frame(objectEl); + await counter.click(); + + expect(await counter.getAttribute('class')).toEqual(notSelected); + await driver.switchTo().defaultContent(); + expect(await record.getAttribute('class')).toEqual(notSelected); +}); + +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'); + + 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); + + 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); + + await driver.switchTo().defaultContent(); + 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'); + + 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'); + + expect(await counter.getAttribute('class')).toEqual(notSelected); + await counter.click(); + + expect(await counter.getAttribute('class')).toEqual(selected); + await driver.switchTo().defaultContent(); + expect(await record.getAttribute('class')).toEqual(selected); + + 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'); + + 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); + + 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); + + await counter.click(); + + expect(await counter.getAttribute('class')).toEqual(notSelected); + await driver.switchTo().defaultContent(); + expect(await record.getAttribute('class')).toEqual(notSelected); +}); + +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'); +} -- cgit v1.2.3