Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: dc03aba808bace56e459168c208a7c01df66f639 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const { mkdir, writeFile } = require('node:fs/promises')
  , path = require('path')
  , { By, until } = require('selenium-webdriver')
  , { JSDOM } = require('jsdom')
  , { HttpResponse } = require('selenium-webdriver/devtools/networkinterceptor')
  ;

global.takeScreenshot = async (driver) => {
  const dir = './test/screenshots';
  const fileName = path.relative(process.cwd(), __filename) + ' "' + global.expect.getState().currentTestName + `" ${new Date().toISOString()}.png`;
  const image = await driver.takeScreenshot();
  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;
}

global.page = {
  waitUntilMapLoaded: async function (driver) {
    const mapPlaceholderEl = await driver.findElement(By.css('.map-placeholder'));
    await driver.wait(until.elementIsNotVisible(mapPlaceholderEl), 1000);
  },

  findElInFrame: async function (frame, locator) {
    const driver = frame.getDriver();
    await driver.switchTo().frame(frame);
    return driver.findElement(locator);
  },

  expectSelected: async function (el) {
    expect(await el.getAttribute('class')).toEqual(expect.stringContaining('selected'));
  },

  expectNotSelected: async function (el) {
    expect(await el.getAttribute('class')).toEqual(expect.not.stringContaining('selected'));
  },

  troopCounterSelector: function ({ allegiance = 'attacker', troopNumber = 1 } = {}) {
    return `.counter[data-allegiance="${allegiance}"][data-number="${troopNumber}"]`;
  },

  findMapResourceEl: function (driver) {
    return driver.findElement(By.css('object'));
  },

  findTroopRecordEl: function (driver) {
    return driver.findElement(By.css('.soldier-record'));
  }
};

global.mockResponse = async function mockResponse(driver, urlPath, template, buildFn) {
  const connection = driver.createCDPConnection('page');
  const dom = new JSDOM(template);
  const contents = buildFn(dom.window.document);

  const httpResponse = new HttpResponse(url(urlPath));
  httpResponse.body = contents;
  httpResponse.addHeaders('Content-Type', 'image/svg+xml');

  await driver.onIntercept(await connection, httpResponse, async () => {});
}

global.createTroopCounter = function (
  allegiance = 'attacker',
  troopNumber = 1,
  squadNumber = 1,
  weapon = 'rifle'
) {
  return JSDOM.fragment(`
    <g class="counter" data-allegiance="${allegiance}" data-number="${troopNumber}">
      <use class="primary-weapon" href="counters.svg#${weapon}"></use>
      <use class="troop-number" href="counters.svg#number-${troopNumber}"></use>
      <use class="squad-number" href="counters.svg#number-${squadNumber}"></use>
    </g>
  `);
}

global.placeCounter = function (document, counter, { q = 0, r = 0, s = 0, t = 0 } = {}) {
  document.querySelector(`[data-q="${q}"][data-r="${r}"][data-s="${s}"][data-t="${t}"]`).append(counter);
}

global.selectCounter = function (counter) {
  counter.querySelector('.counter').classList.add('selected');
  return counter;
}

global.svgDocument = function (document) {
  return `<?xml version="1.0" standalone="yes"?>\n` + document.querySelector('svg').outerHTML;
}