2020-04-10 19:49:18 +02:00
|
|
|
const path = require("path");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
|
|
|
const commander = require("commander");
|
2020-04-17 13:07:48 +02:00
|
|
|
const mkdirp = require("mkdirp");
|
2020-07-24 06:02:07 +02:00
|
|
|
const puppeteer = require("puppeteer");
|
|
|
|
|
2020-04-10 19:49:18 +02:00
|
|
|
const host = "localhost:9991";
|
|
|
|
const options = {};
|
|
|
|
|
|
|
|
commander
|
2020-07-15 01:29:15 +02:00
|
|
|
.arguments("<message_id> <image_path>")
|
2020-04-24 14:48:28 +02:00
|
|
|
.action((messageId, imagePath) => {
|
2020-04-10 19:49:18 +02:00
|
|
|
options.messageId = messageId;
|
2020-04-24 14:48:28 +02:00
|
|
|
options.imagePath = imagePath;
|
|
|
|
console.log(`Capturing screenshot for message ${messageId} to ${imagePath}`);
|
2020-04-10 19:49:18 +02:00
|
|
|
})
|
|
|
|
.parse(process.argv);
|
|
|
|
|
2020-04-24 14:48:28 +02:00
|
|
|
if (options.messageId === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
console.error("no messageId specified!");
|
2020-04-10 19:49:18 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Refactor to share code with frontend_tests/puppeteer_tests/00-realm-creation.js
|
|
|
|
async function run() {
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
args: [
|
2020-07-15 01:29:15 +02:00
|
|
|
"--window-size=1400,1024",
|
2020-07-15 00:34:28 +02:00
|
|
|
"--no-sandbox",
|
|
|
|
"--disable-setuid-sandbox",
|
2020-04-10 19:49:18 +02:00
|
|
|
// Helps render fonts correctly on Ubuntu: https://github.com/puppeteer/puppeteer/issues/661
|
2020-07-15 01:29:15 +02:00
|
|
|
"--font-render-hinting=none",
|
2020-04-10 19:49:18 +02:00
|
|
|
],
|
|
|
|
defaultViewport: null,
|
|
|
|
headless: true,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const page = await browser.newPage();
|
|
|
|
// deviceScaleFactor:2 gives better quality screenshots (higher pixel density)
|
2020-07-16 22:40:18 +02:00
|
|
|
await page.setViewport({width: 1280, height: 1024, deviceScaleFactor: 2});
|
2020-07-15 01:29:15 +02:00
|
|
|
await page.goto("http://" + host);
|
2020-06-13 12:40:21 +02:00
|
|
|
// wait for Iago devlogin button and click on it.
|
|
|
|
await page.waitForSelector('[value="iago@zulip.com"]');
|
|
|
|
|
|
|
|
// By waiting till DOMContentLoaded we're confirming that Iago is logged in.
|
|
|
|
await Promise.all([
|
2020-07-16 22:40:18 +02:00
|
|
|
page.waitForNavigation({waitUntil: "domcontentloaded"}),
|
2020-06-13 12:40:21 +02:00
|
|
|
page.click('[value="iago@zulip.com"]'),
|
|
|
|
]);
|
2020-04-10 19:49:18 +02:00
|
|
|
|
|
|
|
// Navigate to message and capture screenshot
|
|
|
|
await page.goto(`http://${host}/#narrow/near/${options.messageId}`);
|
|
|
|
const messageSelector = `#zfilt${options.messageId}`;
|
|
|
|
await page.waitForSelector(messageSelector);
|
|
|
|
// remove unread marker and don't select message
|
|
|
|
const marker = `#zfilt${options.messageId} .unread_marker`;
|
2020-07-16 23:29:01 +02:00
|
|
|
await page.evaluate((sel) => $(sel).remove(), marker); // eslint-disable-line no-undef
|
|
|
|
await page.evaluate(() => navigate.up()); // eslint-disable-line no-undef
|
2020-04-10 19:49:18 +02:00
|
|
|
const messageBox = await page.$(messageSelector);
|
2020-07-15 01:29:15 +02:00
|
|
|
const messageGroup = (await messageBox.$x(".."))[0];
|
2020-04-10 19:49:18 +02:00
|
|
|
// Compute screenshot area, with some padding around the message group
|
|
|
|
const clip = Object.assign({}, await messageGroup.boundingBox());
|
|
|
|
clip.y -= 5;
|
|
|
|
clip.x -= 5;
|
|
|
|
clip.width += 10;
|
|
|
|
clip.height += 10;
|
2020-04-24 14:48:28 +02:00
|
|
|
const imagePath = options.imagePath;
|
|
|
|
const imageDir = path.dirname(imagePath);
|
2020-04-17 13:07:48 +02:00
|
|
|
mkdirp.sync(imageDir);
|
2020-07-20 22:18:43 +02:00
|
|
|
await page.screenshot({path: imagePath, clip});
|
2020-04-10 19:49:18 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|