2024-05-14 16:28:21 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/* global $, CSS */
|
|
|
|
|
2024-11-10 22:34:32 +01:00
|
|
|
const fs = require("node:fs");
|
2024-10-09 00:25:41 +02:00
|
|
|
const path = require("node:path");
|
2024-11-11 00:10:00 +01:00
|
|
|
const {parseArgs} = require("node:util");
|
2024-05-14 16:28:21 +02:00
|
|
|
|
|
|
|
require("css.escape");
|
|
|
|
const puppeteer = require("puppeteer");
|
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
const usage =
|
|
|
|
"Usage: thread-screenshot.js <narrow_uri> <narrow> <message_id> <image_path> <realm_url>";
|
|
|
|
const {
|
|
|
|
values: {help},
|
|
|
|
positionals: [narrowUri, narrow, messageId, imagePath, realmUrl],
|
|
|
|
} = parseArgs({options: {help: {type: "boolean"}}, allowPositionals: true});
|
2024-05-14 16:28:21 +02:00
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
if (help) {
|
|
|
|
console.log(usage);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
if (realmUrl === undefined) {
|
|
|
|
console.error(usage);
|
2024-05-14 16:28:21 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
console.log(`Capturing screenshot for ${narrow} to ${imagePath}`);
|
|
|
|
|
2024-05-14 16:28:21 +02:00
|
|
|
// TODO: Refactor to share code with web/e2e-tests/realm-creation.test.ts
|
|
|
|
async function run() {
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
args: [
|
2024-05-21 18:08:50 +02:00
|
|
|
"--window-size=500,1024",
|
2024-05-14 16:28:21 +02:00
|
|
|
"--no-sandbox",
|
|
|
|
"--disable-setuid-sandbox",
|
|
|
|
// Helps render fonts correctly on Ubuntu: https://github.com/puppeteer/puppeteer/issues/661
|
|
|
|
"--font-render-hinting=none",
|
|
|
|
],
|
|
|
|
defaultViewport: null,
|
|
|
|
headless: "new",
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const page = await browser.newPage();
|
|
|
|
// deviceScaleFactor:2 gives better quality screenshots (higher pixel density)
|
2024-05-21 18:08:50 +02:00
|
|
|
await page.setViewport({width: 530, height: 1024, deviceScaleFactor: 2});
|
2024-11-11 00:10:00 +01:00
|
|
|
await page.goto(`${realmUrl}/devlogin`);
|
2024-05-14 16:28: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([
|
|
|
|
page.waitForNavigation({waitUntil: "domcontentloaded"}),
|
|
|
|
page.click('[value="iago@zulip.com"]'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Navigate to message and capture screenshot
|
2024-11-11 00:10:00 +01:00
|
|
|
await page.goto(`${narrowUri}`, {
|
2024-05-14 16:28:21 +02:00
|
|
|
waitUntil: "networkidle2",
|
|
|
|
});
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const message_list_id = await page.evaluate(() => zulip_test.current_msg_list.id);
|
|
|
|
const messageListSelector = "#message-lists-container";
|
|
|
|
await page.waitForSelector(messageListSelector);
|
|
|
|
|
2024-05-21 18:08:50 +02:00
|
|
|
// remove unread marker and don't select message
|
|
|
|
const marker = `.message-list[data-message-list-id="${CSS.escape(
|
|
|
|
message_list_id,
|
|
|
|
)}"] .unread_marker`;
|
|
|
|
await page.evaluate((sel) => {
|
|
|
|
$(sel).remove();
|
|
|
|
}, marker);
|
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
const messageSelector = `#message-row-${message_list_id}-${CSS.escape(messageId)}`;
|
2024-05-14 16:28:21 +02:00
|
|
|
await page.waitForSelector(messageSelector);
|
|
|
|
|
|
|
|
const messageListBox = await page.$(messageListSelector);
|
|
|
|
await page.evaluate((msg) => $(msg).removeClass("selected_message"), messageSelector);
|
|
|
|
|
2024-05-21 18:08:50 +02:00
|
|
|
// This is done so as to get white background while capturing screenshots.
|
|
|
|
const background_selectors = [".app-main", ".message-feed", ".message_header"];
|
|
|
|
await page.evaluate((selectors) => {
|
|
|
|
for (const selector of selectors) {
|
|
|
|
$(selector).css("background-color", "white");
|
|
|
|
}
|
|
|
|
}, background_selectors);
|
|
|
|
|
2024-05-14 16:28:21 +02:00
|
|
|
// Compute screenshot area, with some padding around the message group
|
|
|
|
const clip = {...(await messageListBox.boundingBox())};
|
2024-05-21 18:08:50 +02:00
|
|
|
clip.width -= 64;
|
|
|
|
clip.y += 10;
|
|
|
|
clip.height -= 8;
|
2024-05-14 16:28:21 +02:00
|
|
|
const imageDir = path.dirname(imagePath);
|
2024-11-10 22:34:32 +01:00
|
|
|
await fs.promises.mkdir(imageDir, {recursive: true});
|
2024-05-14 16:28:21 +02:00
|
|
|
await page.screenshot({path: imagePath, clip});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|