Skip to content

Commit 4ac0d6e

Browse files
author
SentienceDEV
committed
intercept example.com to fix tests
1 parent b835243 commit 4ac0d6e

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

tests/actions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
uncheck,
2424
uploadFile,
2525
} from '../src';
26-
import { createTestBrowser, getPageOrThrow } from './test-utils';
26+
import { createTestBrowser, getPageOrThrow, patchExampleDotCom } from './test-utils';
2727
import * as fs from 'fs';
2828
import * as os from 'os';
2929
import * as path from 'path';
@@ -220,8 +220,8 @@ describe('Actions', () => {
220220
try {
221221
await browser.start();
222222
const page = getPageOrThrow(browser);
223+
patchExampleDotCom(page);
223224
await page.goto('https://example.com');
224-
await page.waitForLoadState('networkidle', { timeout: 10000 });
225225

226226
await expect(search(browser, 'sentience sdk', 'duckduckgo')).rejects.toThrow(
227227
'domain not allowed'

tests/browser.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { chromium, BrowserContext, Page } from 'playwright';
77
import * as fs from 'fs';
88
import * as os from 'os';
99
import * as path from 'path';
10+
import { patchExampleDotCom } from './test-utils';
1011

1112
describe('Browser Proxy Support', () => {
1213
describe('Proxy Parsing', () => {
@@ -228,6 +229,7 @@ describe('Browser Proxy Support', () => {
228229
if (!page) {
229230
throw new Error('Browser page is not available');
230231
}
232+
patchExampleDotCom(page);
231233
await page.goto('https://example.com', { waitUntil: 'domcontentloaded', timeout: 20000 });
232234

233235
const viewportSize = await page.evaluate(() => ({
@@ -293,6 +295,7 @@ describe('Browser Proxy Support', () => {
293295
expect(sentienceBrowser.getContext()).toBe(context);
294296

295297
// Test that we can use it
298+
patchExampleDotCom(page);
296299
await page.goto('https://example.com');
297300
await page.waitForLoadState('networkidle', { timeout: 10000 });
298301

tests/test-utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export async function createTestBrowser(headless?: boolean): Promise<SentienceBr
1313
const browser = new SentienceBrowser(undefined, undefined, headless);
1414
try {
1515
await browser.start();
16+
const page = browser.getPage();
17+
if (page) {
18+
patchExampleDotCom(page);
19+
}
1620
return browser;
1721
} catch (e: any) {
1822
// Clean up browser on failure to prevent resource leaks
@@ -44,3 +48,28 @@ export function getPageOrThrow(browser: SentienceBrowser): Page {
4448
}
4549
return page;
4650
}
51+
52+
const DEFAULT_TEST_HTML = `<!doctype html>
53+
<html>
54+
<head><meta charset="utf-8" /></head>
55+
<body>
56+
<a id="link" href="#ok">Example Link</a>
57+
<input id="text" type="text" value="hello" />
58+
<button id="btn" type="button">Click me</button>
59+
<div style="height: 2000px;"></div>
60+
</body>
61+
</html>`;
62+
63+
export async function setTestPageContent(page: Page, html?: string): Promise<void> {
64+
await page.setContent(html ?? DEFAULT_TEST_HTML, { waitUntil: 'domcontentloaded' });
65+
}
66+
67+
export function patchExampleDotCom(page: Page): void {
68+
void page.route(/https?:\/\/example\.com\/?.*/, async route => {
69+
await route.fulfill({
70+
status: 200,
71+
contentType: 'text/html',
72+
body: DEFAULT_TEST_HTML,
73+
});
74+
});
75+
}

0 commit comments

Comments
 (0)