Skip to content

Commit

Permalink
feat: export puppeteer agent
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyutaotao committed Jul 25, 2024
1 parent 8125e79 commit 7787a4f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/web-integration/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { PlaywrightAiFixture } from './playwright';
export type { PlayWrightAiFixtureType } from './playwright';

export { PuppeteerAgent } from './puppeteer';
5 changes: 5 additions & 0 deletions packages/web-integration/src/puppeteer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PageAgent } from '@/common/agent';

const PuppeteerAgent = PageAgent;

export { PuppeteerAgent };
50 changes: 20 additions & 30 deletions packages/web-integration/tests/puppeteer/bing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import puppeteer, { Viewport } from 'puppeteer';
import { it, describe, expect, vi } from 'vitest';
import { sleep } from '@midscene/core/utils';
import { launchPage } from './utils';
import { PuppeteerAgent } from '@/puppeteer';

const launchPage = async (
url: string,
opt?: {
viewport?: Viewport;
},
) => {
const browser = await puppeteer.launch();
vi.setConfig({
testTimeout: 60 * 1000,
});

const page = (await browser.pages())[0];
const viewportConfig = {
width: opt?.viewport?.pixelWidth || 1920,
height: opt?.viewport?.pixelHeight || 1080,
deviceScaleFactor: opt?.viewport?.dpr || 1,
};
await page.setViewport(viewportConfig);
await Promise.all([
page.waitForNavigation({
timeout: 20 * 1000,
waitUntil: 'networkidle0',
}),
(async () => {
const response = await page.goto(url);
if (response?.status) {
assert(response.status() <= 399, `Page load failed: ${response.status()}`);
}
})(),
]);
await sleep(2 * 1000);
describe('puppeteer integration', () => {
it('basic launch', async () => {
const page = await launchPage('https://www.bing.com');

return browser;
};
const agent = new PuppeteerAgent(page);

await agent.aiAction('type "how much is the ferry ticket in Shanghai" in search box, hit Enter');
await sleep(5000);

const relatedSearch = await agent.aiQuery('string[], related search keywords on the right');
console.log('related search', relatedSearch);
expect(relatedSearch.length).toBeGreaterThan(3);
});
});
4 changes: 0 additions & 4 deletions packages/web-integration/tests/puppeteer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import assert from 'assert';
import puppeteer, { Viewport } from 'puppeteer';

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function launchPage(
url: string,
opt?: {
Expand Down
2 changes: 1 addition & 1 deletion packages/web-integration/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default defineConfig({
},
},
test: {
include: [],
include: ['./tests/puppeteer/*.spec.ts'],
},
});

0 comments on commit 7787a4f

Please sign in to comment.