Skip to content

Commit

Permalink
feat: use jpeg as default image format (#301)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: zhouxiao.shaw <[email protected]>
  • Loading branch information
yuyutaotao and zhoushaw authored Jan 20, 2025
1 parent e6ad45c commit 3aa1b33
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 29 deletions.
16 changes: 12 additions & 4 deletions packages/midscene/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ function getReportTpl() {
if (!reportTpl && (window as any).get_midscene_report_tpl) {
reportTpl = (window as any).get_midscene_report_tpl();
}
assert(
reportTpl,
'reportTpl should be set before writing report in browser',
);
// assert(
// reportTpl,
// 'reportTpl should be set before writing report in browser',
// );
return reportTpl;
}

Expand All @@ -70,6 +70,10 @@ export function reportHTMLContent(
dumpData: string | ReportDumpWithAttributes[],
): string {
const tpl = getReportTpl();
if (!tpl) {
console.warn('reportTpl is not set, will not write report');
return '';
}
let reportContent: string;
if (
(Array.isArray(dumpData) && dumpData.length === 0) ||
Expand Down Expand Up @@ -121,6 +125,10 @@ export function writeDumpReport(

const reportPath = join(getLogDirByType('report'), `${fileName}.html`);
const reportContent = reportHTMLContent(dumpData);
if (!reportContent) {
console.warn('reportContent is empty, will not write report');
return null;
}
writeFileSync(reportPath, reportContent);

return reportPath;
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/img/box-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ export const compositeElementInfoImg = async (options: {
return compositeImage;
})
.then(async (compositeImage: Jimp) => {
const base64 = await compositeImage.getBase64Async(Jimp.MIME_PNG);
compositeImage.quality(75);
const base64 = await compositeImage.getBase64Async(Jimp.MIME_JPEG);
return base64;
})
.catch((error: unknown) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/img/draw-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export async function drawBoxOnImage(options: {
);

// Convert back to base64
const resultBase64 = await image.getBase64Async(Jimp.MIME_PNG);
image.quality(75);
const resultBase64 = await image.getBase64Async(Jimp.MIME_JPEG);
return resultBase64;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/shared/src/img/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function transformImgPathToBase64(inputPath: string) {
// Use Jimp to process images and generate base64 data
const Jimp = await getJimp();
const image = await Jimp.read(inputPath);
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
const buffer = await image.getBufferAsync(Jimp.MIME_JPEG);
return buffer.toString('base64');
}

Expand Down Expand Up @@ -72,7 +72,8 @@ export async function resizeImg(
finalNewSize.height,
Jimp.RESIZE_NEAREST_NEIGHBOR,
);
const resizedBuffer = await image.getBufferAsync(Jimp.MIME_PNG);
image.quality(75);
const resizedBuffer = await image.getBufferAsync(Jimp.MIME_JPEG);

return resizedBuffer;
}
Expand Down
29 changes: 20 additions & 9 deletions packages/visualizer/scripts/build-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,31 @@ function emptyDumpReportHTML() {

const tplRetrieverFn = `window.get_midscene_report_tpl = () => {
const tpl = document.getElementById('midscene_report_tpl').innerText;
if (!tpl) {
return '';
}
const tplDecoded = decodeURIComponent(tpl);
return tplDecoded;
};`;
function putReportTplIntoHTML(html: string, outsourceMode = false) {
assert(html.indexOf('</body>') !== -1, 'HTML must contain </body>');

const tplWrapper = `<noscript id="midscene_report_tpl">\n${encodeURIComponent(
emptyDumpReportHTML(),
)}\n</noscript>`;

if (outsourceMode) {
const tplWrapper = `<noscript id="midscene_report_tpl">\n${encodeURIComponent(
emptyDumpReportHTML(),
)}\n</noscript>`;
// in Chrome extension
return html.replace(
'</body>',
`${tplWrapper}<script src="/lib/set-report-tpl.js"></script>\n</body>`,
);
}
return html.replace(
'</body>',
`${tplWrapper}<script>${tplRetrieverFn}</script>\n</body>`,
);

return html;
// return html.replace(
// '</body>',
// `${tplWrapper}<script>${tplRetrieverFn}</script>\n</body>`,
// );
}

function reportHTMLWithDump(
Expand All @@ -95,8 +99,15 @@ function reportHTMLWithDump(
dumpContent = `<script type="midscene_web_dump">\n${dumpJsonString}\n</script>`;
}

const emptyDumpHTML = emptyDumpReportHTML();
assert(
emptyDumpHTML.length <
(process.env.CI ? 10 * 1000 * 1000 : 20 * 1000 * 1000),
`emptyDumpHTML is too large, length: ${emptyDumpHTML.length}`,
);

const reportHTML = replaceStringWithFirstAppearance(
emptyDumpReportHTML(),
emptyDumpHTML,
'{{dump}}',
dumpContent || '{{dump}}',
);
Expand Down
6 changes: 5 additions & 1 deletion packages/visualizer/src/component/playground-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,11 @@ export function Playground({
replayScripts={replayScriptsInfo.scripts}
imageWidth={replayScriptsInfo.width}
imageHeight={replayScriptsInfo.height}
reportFileContent={result?.reportHTML}
reportFileContent={
serviceMode === 'In-Browser-Extension' && result?.reportHTML
? result?.reportHTML
: null
}
/>
);
} else if (result?.result) {
Expand Down
15 changes: 4 additions & 11 deletions packages/web-integration/src/puppeteer/base-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,13 @@ export class Page<
async screenshotBase64(): Promise<string> {
// get viewport size from underlyingPage
// const viewportSize = await this.size();
const path = getTmpFile('png')!;

const imgType = 'jpeg';
const path = getTmpFile(imgType)!;
await this.underlyingPage.screenshot({
path,
type: 'png',
type: imgType,
quality: 75,
});
// let buf: Buffer;
// if (viewportSize.dpr && viewportSize.dpr > 1) {
// buf = await resizeImg(readFileSync(path), {
// width: viewportSize.width,
// height: viewportSize.height,
// });
// writeFileSync(path, buf);
// }

return base64Encoded(path, true);
}
Expand Down

0 comments on commit 3aa1b33

Please sign in to comment.