Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: e888b6d6ac3a6197e50d568f062d77896b568947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { Builder, By } = require('selenium-webdriver'),
  chrome = require('selenium-webdriver/chrome.js'),
  { expect, it } = require('@jest/globals'),
  chromeOptions = new chrome.Options(),
  { mkdir, writeFile, symlink, unlink } = require('node:fs/promises'),
  path = require('path');

chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');

let driver;

beforeAll(async () => {
  await symlink('../../../test/map1.svg', 'public/assets/images/test_map.svg');
});

beforeEach(async () => {
  driver = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();

  console.log('manage', await driver.manage());

  await driver.get('http://localhost:3005');
});

it.only('loads the page', async () => {
  expect(await driver.getTitle()).toEqual('Infantry Combat Solo Basic');

  // console.log('window', window);

  // takeScreenshot(driver);

  // var start = new Date().getTime();
  // await driver.executeAsyncScript(
  //     'window.setTimeout(arguments[arguments.length - 1], 500);').
  //     then(function() {
  //       console.log(
  //           'Elapsed time: ' + (new Date().getTime() - start) + ' ms');
  //     });

  // await driver.executeScript(`return __dirname`).then(function (e) {
  //   console.log('e', e);
  // });
  // console.log('client', await driver.executeScript(`return document.querySelect('object');`));
});

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();
});

afterAll(async () => {
  await unlink('public/assets/images/test_map.svg');
});

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');
}