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
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const path = require("node:path");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2024-04-09 01:25:22 +02:00
|
|
|
const {program} = 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 options = {};
|
|
|
|
|
2024-04-09 01:25:22 +02:00
|
|
|
program
|
2024-06-03 10:52:19 +02:00
|
|
|
.arguments("<message_id> <image_path> <realm_url>")
|
|
|
|
.action((messageId, imagePath, realmUrl) => {
|
2020-04-10 19:49:18 +02:00
|
|
|
options.messageId = messageId;
|
2020-04-24 14:48:28 +02:00
|
|
|
options.imagePath = imagePath;
|
2024-06-03 10:52:19 +02:00
|
|
|
options.realmUrl = realmUrl;
|
2020-04-24 14:48:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-22 23:04:11 +01:00
|
|
|
// TODO: Refactor to share code with web/e2e-tests/realm-creation.test.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,
|
2023-07-15 02:21:50 +02:00
|
|
|
headless: "new",
|
2020-04-10 19:49:18 +02:00
|
|
|
});
|
|
|
|
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});
|
2024-06-03 10:52:19 +02:00
|
|
|
await page.goto(`${options.realmUrl}/devlogin`);
|
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
|
2024-06-03 10:52:19 +02:00
|
|
|
await page.goto(`${options.realmUrl}/#narrow/id/${options.messageId}`, {
|
2023-07-15 02:21:50 +02:00
|
|
|
waitUntil: "networkidle2",
|
|
|
|
});
|
2024-01-17 07:53:40 +01:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const message_list_id = await page.evaluate(() => zulip_test.current_msg_list.id);
|
|
|
|
const messageSelector = `#message-row-${message_list_id}-${CSS.escape(options.messageId)}`;
|
2020-04-10 19:49:18 +02:00
|
|
|
await page.waitForSelector(messageSelector);
|
|
|
|
// remove unread marker and don't select message
|
2024-01-17 07:53:40 +01:00
|
|
|
const marker = `#message-row-${message_list_id}-${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);
|
2023-07-15 02:21:50 +02:00
|
|
|
const messageGroup = await messageBox.$("xpath/..");
|
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.x -= 5;
|
|
|
|
clip.width += 10;
|
2023-07-15 02:21:50 +02:00
|
|
|
clip.y += 5;
|
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();
|