-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselenium-generate-images.js
75 lines (63 loc) · 2.43 KB
/
selenium-generate-images.js
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
const { By, Builder, Browser, Key, WebDriver, until } = require("selenium-webdriver");
const fs = require("fs");
const path = require("path");
const baseUrl = "http://localhost:8080/";
const downloadsDir = "/Users/gary/Downloads";
const testDir = "./docs/examples";
const outDir = "./docs/generatedImages";
const delay = ms => new Promise(r => setTimeout(r, ms));
/**
* @param {WebDriver} driver
* @param {string} json
*/
async function downloadOne(driver, json, outfileName) {
await driver.executeAsyncScript(`
window._hasGfontConsent = "true"; // string return value of window.confirm()
const delay = (ms) => new Promise(r => setTimeout(r, ms));
const callback = arguments[arguments.length - 1];
let attempts = 10;
while (!window._debugGlobalMonacoEditor && attempts --> 0) {
await delay(1000);
}
window._debugGlobalMonacoEditor.getModel().setValue('${JSON.stringify(json)}');
callback();
`);
const statusField = await driver.findElement(By.id("status-field"));
async function waitForRender() {
do {
await driver.wait(until.elementTextIs(statusField, "Rendered"));
await delay(3000);
} while ((await statusField.getText()) !== "Rendered");
}
await waitForRender();
const downloadButton = await driver.findElement(By.id("download-button"));
await downloadButton.click();
await delay(1000);
const files = fs.readdirSync(downloadsDir);
const match = files
.filter(fname => fname.startsWith(json.title))
.map(fname => [fname, fs.statSync(path.join(downloadsDir, fname)).ctimeMs])
.sort((fa, fb) => fb[1] - fa[1])
.map(([fname, _ctime]) => fname)[0];
fs.copyFileSync(path.join(downloadsDir, match), path.join(outDir, outfileName));
fs.rmSync(path.join(downloadsDir, match));
}
async function main() {
const driver = await new Builder().forBrowser(Browser.CHROME).build();
await driver.get(baseUrl);
for (const fname of fs.readdirSync(testDir)) {
if (!fname.endsWith(".json")) {
continue;
}
console.log(`Rendering ${fname}`);
const json = fs.readFileSync(path.join(testDir, fname));
try {
await downloadOne(driver, JSON.parse(json), `${fname.split(".json")[0]}.rendered.png`);
} catch (e) {
console.error("failed to render", e);
await delay(10000);
}
}
await driver.close();
}
main();