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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
const { ProvideResponseParameters } = require('selenium-webdriver/bidi/provideResponseParameters');
const { Builder, By, until } = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome.js'),
getNetworkInstance = require('selenium-webdriver/bidi/network.js'),
{ AddInterceptParameters } = require('selenium-webdriver/bidi/addInterceptParameters'),
{ InterceptPhase } = require('selenium-webdriver/bidi/interceptPhase'),
{ expect, it } = require('@jest/globals'),
chromeOptions = new chrome.Options(),
{ mkdir, writeFile, symlink, unlink } = require('node:fs/promises'),
path = require('path'),
{ tmpdir } = require('os'),
puppeteer = require('puppeteer');
const { HttpResponse } = require('selenium-webdriver/devtools/networkinterceptor');
const fs = require('node:fs/promises');
const DIR = path.resolve(tmpdir(), 'test-dir');
chromeOptions.addArguments('--headless', '--disable-gpu', '--no-sandbox');
chromeOptions.enableBidi();
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());
});
it('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.only('selects an off-board soldier', async () => {
// it.only.each(Array(10).fill(null))('selects an off-board soldier', async () => {
// const id = await driver.getWindowHandle();
// const network = await getNetworkInstance(driver, id);
// const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
// await network.beforeRequestSent(async event => {
// console.log('request url', event.request.url);
// await network.provideResponse(new ProvideResponseParameters(event.request));
// });
// await network.responseStarted(async event => {
// if (event.response.url.includes('scenario'))
// console.log('response', event.response);
// });
// await network.responseStarted(async (event) => {
// if (event.response.url.includes('scenario')) {
// console.log('event', event);
// console.log('request', event.request);
// console.log('response', event.response.result);
// }
// });
// await network.beforeRequestSent(async (event) => {
// if (event.request.url.includes('scenario')) {
// console.log('event', event);
// console.log('request', event.request.request);
// await network.provideResponse(new ProvideResponseParameters(event.request.request));
// // await network.failRequest(event.request.request);
// }
// });
const connection = await driver.createCDPConnection('page')
const url = 'http://localhost:3005/assets/images/scenario-side_show.svg';
const httpResponse = new HttpResponse(url);
// httpResponse.body = `<?xml version="1.0" standalone="no"?>
// <svg viewBox="-150 -150 300 300" xmlns="http://www.w3.org/2000/svg">
// <circle cx="0" cy="0" r="50" fill="violet" />
// </svg>`;
httpResponse.body = await fs.readFile('./test/integration/fixtures/scenario-test.svg', 'utf8');
httpResponse.addHeaders('Content-Type', 'image/svg+xml');
await driver.onIntercept(connection, httpResponse, async function () {
console.log('intercepted');
// await driver.switchTo().frame(await driver.findElement(By.css('object')));
// console.log(await driver.getPageSource());
});
await driver.get('http://localhost:3005');
// expect(await driver.getTitle()).toEqual('Infantry Combat Solo Basic');
await driver.wait(until.elementIsVisible(driver.findElement(By.css('object'))), 1000);
// await driver.wait(until.elementLocated(By.css('#dice')), 1000);
takeScreenshot(driver);
// await driver.switchTo().frame(await driver.findElement(By.css('object')));
// console.log(await driver.getPageSource());
// const testDir = path.dirname(expect.getState().testPath);
// await writeFile(path.join(testDir, `scenario.svg`), `
// <?xml version="1.0" standalone="no"?>
// <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
// </svg>
// `);
// const browser = await puppeteer.launch();
// // Create a page
// const page = await browser.newPage();
// await page.setRequestInterception(true);
// page.on('request', interceptedRequest => {
// console.log('intercept req url', interceptedRequest.url());
// interceptedRequest.continue();
// });
// // Go to your site
// await page.goto('http://localhost:3005');
// await browser.close();
// console.log('test dir', testDir);
// 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 driver.findElement(By.css('#dice'));
// await counter.click();
// expect(await counter.getAttribute('class')).toEqual(expect.stringContaining('selected'));
// await takeScreenshot(driver);
});
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');
}
|