2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-03-26 23:53:30 +01:00
|
|
|
/* global $, CSS */
|
2020-12-11 03:08:45 +01:00
|
|
|
|
2020-04-10 19:49:18 +02:00
|
|
|
const path = require("path");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
|
|
|
const commander = require("commander");
|
2021-02-03 23:23:32 +01:00
|
|
|
require("css.escape");
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:37:19 +02:00
|
|
|
// TODO: Refactor to share code with frontend_tests/puppeteer_tests/realm-creation.ts
|
2020-04-10 19:49:18 +02:00
|
|
|
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
|
2021-03-29 21:33:19 +02:00
|
|
|
await page.goto(`http://${host}/#narrow/id/${options.messageId}`);
|
2021-02-03 23:23:32 +01:00
|
|
|
const messageSelector = `#zfilt${CSS.escape(options.messageId)}`;
|
2020-04-10 19:49:18 +02:00
|
|
|
await page.waitForSelector(messageSelector);
|
|
|
|
// remove unread marker and don't select message
|
2021-02-03 23:23:32 +01:00
|
|
|
const marker = `#zfilt${CSS.escape(options.messageId)} .unread_marker`;
|
2020-12-11 03:08:45 +01:00
|
|
|
await page.evaluate((sel) => $(sel).remove(), marker);
|
2020-04-10 19:49:18 +02:00
|
|
|
const messageBox = await page.$(messageSelector);
|
2021-03-29 21:33:19 +02:00
|
|
|
await page.evaluate((msg) => $(msg).removeClass("selected_message"), 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
|
2020-12-22 00:37:36 +01:00
|
|
|
const clip = {...(await messageGroup.boundingBox())};
|
2020-04-10 19:49:18 +02:00
|
|
|
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-10-07 10:20:41 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2020-04-10 19:49:18 +02:00
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|