Apply prettier.

This commit is contained in:
Guo Xiang Tan 2018-09-11 09:16:03 +08:00
parent 51edb19aa9
commit 4ee8e77073
1 changed files with 65 additions and 42 deletions

View File

@ -9,8 +9,8 @@ const url = args[0];
console.log(`Starting Discourse Smoke Test for ${url}`); console.log(`Starting Discourse Smoke Test for ${url}`);
const puppeteer = require('puppeteer'); const puppeteer = require("puppeteer");
const path = require('path'); const path = require("path");
(async () => { (async () => {
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
@ -26,7 +26,8 @@ const path = require('path');
}); });
const takeFailureScreenshot = function() { const takeFailureScreenshot = function() {
const screenshotPath = `${process.env.SMOKE_TEST_SCREENSHOT_PATH || 'tmp/smoke-test-screenshots'}/smoke-test-${Date.now()}.png`; const screenshotPath = `${process.env.SMOKE_TEST_SCREENSHOT_PATH ||
"tmp/smoke-test-screenshots"}/smoke-test-${Date.now()}.png`;
console.log(`Screenshot of failure taken at ${screenshotPath}`); console.log(`Screenshot of failure taken at ${screenshotPath}`);
return page.screenshot({ path: screenshotPath, fullPage: true }); return page.screenshot({ path: screenshotPath, fullPage: true });
}; };
@ -34,21 +35,26 @@ const path = require('path');
const exec = (description, fn, assertion) => { const exec = (description, fn, assertion) => {
const start = +new Date(); const start = +new Date();
return fn.call().then(async output => { return fn
.call()
.then(async output => {
if (assertion) { if (assertion) {
if (assertion.call(this, output)) { if (assertion.call(this, output)) {
console.log(`PASSED: ${description} - ${(+new Date()) - start}ms`); console.log(`PASSED: ${description} - ${+new Date() - start}ms`);
} else { } else {
console.log(`FAILED: ${description} - ${(+new Date()) - start}ms`); console.log(`FAILED: ${description} - ${+new Date() - start}ms`);
await takeFailureScreenshot(); await takeFailureScreenshot();
console.log("SMOKE TEST FAILED"); console.log("SMOKE TEST FAILED");
process.exit(1); process.exit(1);
} }
} else { } else {
console.log(`PASSED: ${description} - ${(+new Date()) - start}ms`); console.log(`PASSED: ${description} - ${+new Date() - start}ms`);
} }
}).catch(async error => { })
console.log(`ERROR (${description}): ${error.message} - ${(+new Date()) - start}ms`); .catch(async error => {
console.log(
`ERROR (${description}): ${error.message} - ${+new Date() - start}ms`
);
await takeFailureScreenshot(); await takeFailureScreenshot();
console.log("SMOKE TEST FAILED"); console.log("SMOKE TEST FAILED");
process.exit(1); process.exit(1);
@ -59,11 +65,13 @@ const path = require('path');
return exec(description, fn, assertion); return exec(description, fn, assertion);
}; };
page.on('console', msg => console.log(`PAGE LOG: ${msg.text()}`)); page.on("console", msg => console.log(`PAGE LOG: ${msg.text()}`));
page.on('response', resp => { page.on("response", resp => {
if (resp.status() !== 200) { if (resp.status() !== 200) {
console.log("FAILED HTTP REQUEST TO " + resp.url() + " Status is: " + resp.status()); console.log(
"FAILED HTTP REQUEST TO " + resp.url() + " Status is: " + resp.status()
);
} }
return resp; return resp;
}); });
@ -71,7 +79,9 @@ const path = require('path');
if (process.env.AUTH_USER && process.env.AUTH_PASSWORD) { if (process.env.AUTH_USER && process.env.AUTH_PASSWORD) {
await exec("basic authentication", () => { await exec("basic authentication", () => {
return page.setExtraHTTPHeaders({ return page.setExtraHTTPHeaders({
'Authorization': `Basic ${new Buffer(`${process.env.AUTH_USER}:${process.env.AUTH_PASSWORD}`).toString('base64')}` Authorization: `Basic ${new Buffer(
`${process.env.AUTH_USER}:${process.env.AUTH_PASSWORD}`
).toString("base64")}`
}); });
}); });
} }
@ -85,7 +95,7 @@ const path = require('path');
}); });
await exec("go to latest page", () => { await exec("go to latest page", () => {
return page.goto(path.join(url, 'latest')); return page.goto(path.join(url, "latest"));
}); });
await exec("at least one topic shows up", () => { await exec("at least one topic shows up", () => {
@ -93,7 +103,7 @@ const path = require('path');
}); });
await exec("go to categories page", () => { await exec("go to categories page", () => {
return page.goto(path.join(url, 'categories')); return page.goto(path.join(url, "categories"));
}); });
await exec("can see categories on the page", () => { await exec("can see categories on the page", () => {
@ -126,10 +136,16 @@ const path = require('path');
}); });
await exec("type in credentials & log in", () => { await exec("type in credentials & log in", () => {
let promise = page.type("#login-account-name", process.env.DISCOURSE_USERNAME || 'smoke_user'); let promise = page.type(
"#login-account-name",
process.env.DISCOURSE_USERNAME || "smoke_user"
);
promise = promise.then(() => { promise = promise.then(() => {
return page.type("#login-account-password", process.env.DISCOURSE_PASSWORD || 'P4ssw0rd'); return page.type(
"#login-account-password",
process.env.DISCOURSE_PASSWORD || "P4ssw0rd"
);
}); });
promise = promise.then(() => { promise = promise.then(() => {
@ -166,7 +182,7 @@ const path = require('path');
}); });
await exec("compose new topic", () => { await exec("compose new topic", () => {
const date = `(${(+new Date())})`; const date = `(${+new Date()})`;
const title = `This is a new topic ${date}`; const title = `This is a new topic ${date}`;
const post = `I can write a new topic inside the smoke test! ${date} \n\n`; const post = `I can write a new topic inside the smoke test! ${date} \n\n`;
@ -210,16 +226,19 @@ const path = require('path');
}); });
await exec("composer is open", () => { await exec("composer is open", () => {
return page.waitForSelector("#reply-control .d-editor-input", { visible: true }); return page.waitForSelector("#reply-control .d-editor-input", {
visible: true
});
}); });
await exec("compose reply", () => { await exec("compose reply", () => {
const post = `I can even write a reply inside the smoke test ;) (${(+new Date())})`; const post = `I can even write a reply inside the smoke test ;) (${+new Date()})`;
return page.type("#reply-control .d-editor-input", post); return page.type("#reply-control .d-editor-input", post);
}); });
await exec("waiting for the preview", () => { await exec("waiting for the preview", () => {
return page.waitForXPath("//div[contains(@class, 'd-editor-preview') and contains(.//p, 'I can even write a reply')]", return page.waitForXPath(
"//div[contains(@class, 'd-editor-preview') and contains(.//p, 'I can even write a reply')]",
{ visible: true } { visible: true }
); );
}); });
@ -228,7 +247,9 @@ const path = require('path');
return page.click("#reply-control .create"); return page.click("#reply-control .create");
}); });
await assert("reply is created", () => { await assert(
"reply is created",
() => {
let promise = page.waitForSelector(".topic-post"); let promise = page.waitForSelector(".topic-post");
promise = promise.then(() => { promise = promise.then(() => {
@ -238,9 +259,11 @@ const path = require('path');
}); });
return promise; return promise;
}, output => { },
output => {
return output === 2; return output === 2;
}); }
);
} }
await exec("close browser", () => { await exec("close browser", () => {