-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add playwright demo. (#65)
* .git: ignore all temp files * examples: add playwright driver. * feat: improve playwright driver. * package: add playwright test script.
- Loading branch information
Showing
10 changed files
with
588 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,6 @@ dist | |
.dist | ||
|
||
# Examples | ||
examples/puppeteer/temp/ | ||
**/*/temp/ | ||
|
||
**/*/detox_copilot_cache.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/tests/**/*.test.ts'], | ||
moduleFileExtensions: ['ts', 'js', 'json', 'node'], | ||
moduleDirectories: ['node_modules', '<rootDir>/node_modules'], | ||
transformIgnorePatterns: ['node_modules/(?!examples)'], | ||
moduleNameMapper: { | ||
'^@copilot$': '<rootDir>/../../src', | ||
'^@copilot/(.*)$': '<rootDir>/../../src/$1', | ||
'^@/(.*)$': '<rootDir>/../../src/$1' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "detox-copilot-playwright-example", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"detox-copilot": "file:../.." | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.5.12", | ||
"jest": "^29.7.0", | ||
"ts-jest": "^29.2.4", | ||
"typescript": "^5.3.3", | ||
"playwright": "^1.42.1", | ||
"@playwright/test": "^1.42.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import copilot from "@copilot"; | ||
import { PromptHandler } from "../../utils/promptHandler"; | ||
import { PlaywrightFrameworkDriver } from "@copilot/drivers/playwright"; | ||
|
||
describe("Example Test Suite", () => { | ||
jest.setTimeout(300000); | ||
|
||
let frameworkDriver: PlaywrightFrameworkDriver; | ||
|
||
beforeAll(async () => { | ||
const promptHandler: PromptHandler = new PromptHandler(); | ||
|
||
frameworkDriver = new PlaywrightFrameworkDriver(); | ||
|
||
copilot.init({ | ||
frameworkDriver, | ||
promptHandler, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
const page = frameworkDriver.getCurrentPage(); | ||
if (page) { | ||
await page.context().browser()?.close(); | ||
} | ||
}); | ||
|
||
beforeEach(async () => { | ||
copilot.start(); | ||
}); | ||
|
||
afterEach(async () => { | ||
copilot.end(); | ||
}); | ||
|
||
it("perform test with pilot", async () => { | ||
await copilot.pilot( | ||
"Open https://www.wix.com/domains and search for the domain Shraga.com, is it available?", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "../../src", | ||
"rootDir": ".", | ||
"paths": { | ||
"@/*": ["./*"], | ||
"@copilot": ["./index"], | ||
"@copilot/*": ["./*"] | ||
} | ||
}, | ||
"include": ["tests/**/*"], | ||
"exclude": ["node_modules"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as playwright from "playwright"; | ||
|
||
/** | ||
* Get clean DOM from the page content | ||
* - Removes hidden elements | ||
* - Removes ads, analytics, tracking elements | ||
* - Removes unnecessary attributes | ||
* - Removes empty elements | ||
* @param page | ||
*/ | ||
export default async function getCleanDOM(page: playwright.Page) { | ||
await page.waitForSelector("body"); | ||
|
||
return await page.evaluate(() => { | ||
const copiedDocument = document.cloneNode(true) as Document; | ||
|
||
copiedDocument | ||
.querySelectorAll('[hidden], [aria-hidden="true"]') | ||
.forEach((el) => el.remove()); | ||
|
||
const removeSelectors = [ | ||
"script", | ||
"style", | ||
"link", | ||
"meta", | ||
"noscript", | ||
"iframe", | ||
'[class*="ads"]', | ||
'[id*="ads"]', | ||
'[class*="analytics"]', | ||
'[class*="tracking"]', | ||
"footer", | ||
"header", | ||
"nav", | ||
"path", | ||
"aside", | ||
]; | ||
|
||
const allowedAttributes = [ | ||
"src", | ||
"href", | ||
"alt", | ||
"title", | ||
"aria-label", | ||
"aria-labelledby", | ||
"aria-describedby", | ||
"aria-hidden", | ||
"role", | ||
"class", | ||
"id", | ||
"data-*", | ||
]; | ||
|
||
copiedDocument.querySelectorAll("*").forEach((el) => { | ||
Array.from(el.attributes).forEach((attr) => { | ||
if (!allowedAttributes.includes(attr.name)) { | ||
el.removeAttribute(attr.name); | ||
} | ||
}); | ||
|
||
if (!el.innerHTML.trim()) { | ||
el.remove(); | ||
} | ||
}); | ||
|
||
removeSelectors.forEach((selector) => { | ||
copiedDocument.querySelectorAll(selector).forEach((el) => el.remove()); | ||
}); | ||
|
||
const mainContent = copiedDocument.body.innerHTML; | ||
return mainContent.replace(/\s+/g, " ").trim(); | ||
}); | ||
} |
Oops, something went wrong.