-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
168 lines (147 loc) · 5.25 KB
/
index.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
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
const fs = require('fs');
const readline = require('readline');
const puppeteer = require('puppeteer');
const commandLineArgs = require('command-line-args');
const HtmlEntities = require('html-entities').AllHtmlEntities;
const htmlEntities = new HtmlEntities();
const COOKIE_PATH = './cookies.json';
var SPEC_TEST = false
Reset = "\x1b[0m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
const visit = async (page, url, waitForSelector) => {
await page.goto(url, { waitUntil: 'domcontentloaded' });
if (waitForSelector) {
await page.waitForSelector(waitForSelector,
{ visible: true, timeout: 10000 });
}
};
const saveCookies = async (page) => {
const cookies = await page.cookies();
return fs.promises.writeFile(COOKIE_PATH, JSON.stringify(cookies, null, 2));
};
const loadCookies = async (page) => {
try {
const cookiesString = await fs.promises.readFile(COOKIE_PATH);
const cookies = JSON.parse(cookiesString);
await page.setCookie(...cookies);
} catch (e) {
if (e.code === 'ENOENT') {
// cookies.json not present, just continue
} else {
console.error(e);
}
}
};
const signIn = async (page) => {
await visit(page, 'https://weblab.tudelft.nl/samlsignin');
console.log("Not logged in yet, please login to WebLab.");
await page.waitForResponse('https://weblab.tudelft.nl/', { timeout: 0 });
console.log("Logged in!");
await page.waitFor(3000);
await saveCookies(page);
};
const downloadContents = async (page, index, filename) => {
const contents = await page.evaluate((index) => {
const editorInfoKey = Object.keys(window).find(
(key) => key.startsWith('editorInfo_') && window[key].editor._id === index
);
return window[editorInfoKey].editor.getValue();
}, index);
await fs.promises.writeFile(filename, contents, 'utf8');
};
const uploadContents = async (page, index, filename) => {
const contents = await fs.promises.readFile(filename, 'utf8');
await page.evaluate((index, contents) => {
const editorInfoKey = Object.keys(window).find(
(key) => key.startsWith('editorInfo_') && window[key].editor._id === index
);
window[editorInfoKey].editor.setValue(contents);
}, index, contents);
await page.waitFor(400);
};
const runTest = async (page, spec) => {
const saveButton = await page.$('#visibleSave');
await saveButton.click();
await page.waitForSelector('.save-button.saved', { visible: true });
await page.waitFor(2000);
const selector = spec === true ? '.specTestBtn' : '.userTestBtn';
const testButton = await page.$(selector);
await testButton.click();
const modeStr = SPEC_TEST === false ? 'Your Test' : 'Spec Test';
console.log(`${FgYellow}Running ${modeStr}${Reset}`);
};
const switchMode = () => {
SPEC_TEST = !SPEC_TEST;
const modeStr = SPEC_TEST === false ? 'Your Test' : 'Spec Test';
console.log(`${FgRed}Mode: ${modeStr}${Reset}`);
};
(async () => {
const args = commandLineArgs([
{ name: 'url', type: String },
{ name: 'src', type: String },
{ name: 'test', type: String },
]);
if (!args.url || !args.src || !args.test) {
return console.log(`Usage: weblab-runner
--url <weblab submission url>
--src <path to src code>
--test <path to test code>`);
}
const url = args.url.indexOf('#') !== -1 ?
args.url.substring(0, args.url.indexOf('#')) : args.url;
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await loadCookies(page);
await visit(page, url, '#maincontainer');
const [hasSignInButton] = await page.$x("//a[contains(., 'Sign in')]");
if (hasSignInButton) {
await signIn(page);
await visit(page, url, '#maincontainer');
}
// Log compiler output
await page.exposeFunction('outputChanged', (text) => {
console.log(`${FgBlue}${htmlEntities.decode(text)}${Reset}`);
});
await page.waitFor(2000);
await page.evaluate(() => {
var el = document.querySelector('#consoleOutput');
var obs = new MutationObserver(function(e) {
const text = document.querySelector('#consoleOutputPre').innerHTML.trim();
window.outputChanged(text);
});
obs.observe(el, { characterData: true, childList: true });
});
await downloadContents(page, 1, args.src);
await downloadContents(page, 2, args.test);
console.log(`Downloaded ${args.src}, watching changes...`);
console.log(`Downloaded ${args.test}, watching changes...`);
const modeStr = SPEC_TEST === false ? 'Your Test' : 'Spec Test';
console.log(`${FgRed}Mode: ${modeStr} (toggle using 's')${Reset}`);
fs.watchFile(args.src, async (stat) => {
console.log(`${FgYellow}Changed: ${args.src}${Reset}`);
await uploadContents(page, 1, args.src);
await runTest(page, SPEC_TEST);
});
fs.watchFile(args.test, async (time) => {
console.log(`${FgYellow}Changed: ${args.test}${Reset}`);
await uploadContents(page, 2, args.test);
await runTest(page, SPEC_TEST);
});
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit();
} else {
if (str === 's') {
switchMode();
}
}
});
})().catch((e) => console.error(e));