2024-09-29 17:16:07 +08:00
|
|
|
import { writeFileSync } from 'node:fs';
|
2024-08-21 17:24:32 +08:00
|
|
|
import path, { join } from 'node:path';
|
2024-08-20 07:41:08 +08:00
|
|
|
import { parseContextFromWebPage } from '@/common/utils';
|
2024-08-22 18:12:01 +08:00
|
|
|
import { generateExtractData } from '@/debug';
|
2024-09-10 07:04:27 +08:00
|
|
|
import { imageInfo } from '@midscene/shared/img';
|
2024-08-20 07:41:08 +08:00
|
|
|
import { describe, expect, it } from 'vitest';
|
2024-09-06 17:19:35 +08:00
|
|
|
import { launchPage } from '../ai/web/puppeteer/utils';
|
2024-08-20 07:41:08 +08:00
|
|
|
|
2024-09-05 20:05:19 +08:00
|
|
|
const pagePath = join(__dirname, './fixtures/web-extractor/index.html');
|
2024-08-21 16:28:58 +08:00
|
|
|
describe(
|
2024-08-20 07:41:08 +08:00
|
|
|
'extractor',
|
|
|
|
() => {
|
|
|
|
it('basic', async () => {
|
2024-08-31 08:17:50 +08:00
|
|
|
const { page, reset } = await launchPage(`file://${pagePath}`, {
|
|
|
|
viewport: {
|
|
|
|
width: 1080,
|
|
|
|
height: 2000,
|
|
|
|
},
|
|
|
|
});
|
2024-08-21 17:24:32 +08:00
|
|
|
|
2024-08-22 18:12:01 +08:00
|
|
|
const { content } = await parseContextFromWebPage(page);
|
|
|
|
await generateExtractData(
|
2024-08-21 17:24:32 +08:00
|
|
|
page,
|
2024-09-05 20:05:19 +08:00
|
|
|
path.join(__dirname, 'fixtures/web-extractor'),
|
2024-08-21 17:24:32 +08:00
|
|
|
{
|
|
|
|
disableInputImage: false,
|
|
|
|
disableOutputImage: false,
|
|
|
|
disableOutputWithoutTextImg: true,
|
|
|
|
disableResizeOutputImg: true,
|
|
|
|
disableSnapshot: true,
|
|
|
|
},
|
|
|
|
);
|
2024-08-20 07:41:08 +08:00
|
|
|
|
|
|
|
const list = content.map((item) => {
|
|
|
|
return {
|
|
|
|
content: item.content,
|
|
|
|
attributes: item.attributes,
|
|
|
|
};
|
|
|
|
});
|
2024-08-26 11:09:39 +08:00
|
|
|
|
2024-08-20 07:41:08 +08:00
|
|
|
expect(list).toMatchSnapshot();
|
2024-08-21 17:24:32 +08:00
|
|
|
await reset();
|
2024-08-20 07:41:08 +08:00
|
|
|
});
|
2024-08-26 11:09:39 +08:00
|
|
|
|
2024-09-10 07:04:27 +08:00
|
|
|
it('check screenshot size - 1x', async () => {
|
|
|
|
const { page, reset } = await launchPage(`file://${pagePath}`, {
|
|
|
|
viewport: {
|
|
|
|
width: 1080,
|
|
|
|
height: 2000,
|
|
|
|
deviceScaleFactor: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-09-23 10:57:19 +08:00
|
|
|
const shotpath = await page.screenshot();
|
2024-09-10 07:04:27 +08:00
|
|
|
|
|
|
|
const info = await imageInfo(shotpath);
|
|
|
|
expect(info.height).toBe(2000);
|
2024-09-29 17:16:07 +08:00
|
|
|
expect(info.width).toBe(1080);
|
2024-09-10 07:04:27 +08:00
|
|
|
await reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('check screenshot size - 2x', async () => {
|
|
|
|
const { page, reset } = await launchPage(`file://${pagePath}`, {
|
|
|
|
viewport: {
|
|
|
|
width: 1080,
|
|
|
|
height: 2000,
|
|
|
|
deviceScaleFactor: 2,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-09-23 10:57:19 +08:00
|
|
|
const shotpath = await page.screenshot();
|
2024-09-10 07:04:27 +08:00
|
|
|
|
|
|
|
const info = await imageInfo(shotpath);
|
2024-09-29 17:16:07 +08:00
|
|
|
expect(info.width).toBeLessThanOrEqual(1080); // always 1x for screenshot
|
|
|
|
expect(info.height).toBeLessThanOrEqual(2000); // always 1x for screenshot
|
2024-09-10 07:04:27 +08:00
|
|
|
await reset();
|
|
|
|
});
|
|
|
|
|
2024-08-28 19:21:32 +08:00
|
|
|
it('scroll', async () => {
|
|
|
|
const { page, reset } = await launchPage(`file://${pagePath}`, {
|
|
|
|
viewport: {
|
|
|
|
width: 1080,
|
|
|
|
height: 200,
|
|
|
|
},
|
|
|
|
});
|
2024-09-05 20:05:19 +08:00
|
|
|
await page.scrollDownOneScreen();
|
2024-08-28 19:21:32 +08:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
await generateExtractData(
|
|
|
|
page,
|
2024-09-05 20:05:19 +08:00
|
|
|
path.join(__dirname, 'fixtures/web-extractor/scroll'),
|
2024-08-28 19:21:32 +08:00
|
|
|
{
|
|
|
|
disableInputImage: false,
|
|
|
|
disableOutputImage: false,
|
|
|
|
disableOutputWithoutTextImg: true,
|
|
|
|
disableResizeOutputImg: true,
|
|
|
|
disableSnapshot: true,
|
|
|
|
},
|
|
|
|
);
|
2024-09-29 17:16:07 +08:00
|
|
|
await reset();
|
2024-08-28 19:21:32 +08:00
|
|
|
});
|
|
|
|
|
2024-08-26 11:09:39 +08:00
|
|
|
it('profile ', async () => {
|
|
|
|
const { page, reset } = await launchPage('https://webinfra.org/about');
|
2024-08-26 18:50:33 +08:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
2024-08-26 11:09:39 +08:00
|
|
|
console.time('total - parseContextFromWebPage');
|
|
|
|
const { content } = await parseContextFromWebPage(page);
|
|
|
|
console.timeEnd('total - parseContextFromWebPage');
|
2024-08-26 18:50:33 +08:00
|
|
|
await reset();
|
2024-08-26 11:09:39 +08:00
|
|
|
});
|
2024-08-20 07:41:08 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
timeout: 90 * 1000,
|
|
|
|
},
|
|
|
|
);
|