-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: QA-126 Add compability with playwright
- Loading branch information
1 parent
b247ea5
commit 9970eaf
Showing
25 changed files
with
745 additions
and
345 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ dist | |
node_modules | ||
reports | ||
coverage | ||
*.log |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
puppeteer_download_host=https://nexus.hltech.dev/repository/binaries | ||
playwright_download_host=https://nexus.hltech.dev/repository/binaries |
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,51 @@ | ||
/** | ||
* Interface through with Mocketeer communicates with different browser automation libraries | ||
*/ | ||
export interface BrowserController { | ||
startInterception(onRequest: BrowserRequestHandler): Promise<void>; | ||
} | ||
|
||
/** | ||
* Callback function called whenever a request is intercepted in the browser | ||
*/ | ||
export type BrowserRequestHandler = ( | ||
request: BrowserRequest, | ||
respond: (response: ResponseData) => Promise<void>, | ||
skip: () => void | ||
) => void; | ||
|
||
/** | ||
* Data of intercepted browser request | ||
*/ | ||
export interface BrowserRequest { | ||
type: BrowserRequestType; | ||
method: string; | ||
url: string; | ||
headers: Record<string, string>; | ||
body: any; | ||
path: string; | ||
hostname: string; | ||
query: Record<string, string | string[]>; | ||
sourceOrigin: string; | ||
} | ||
|
||
export interface ResponseData { | ||
status: number; | ||
body?: Buffer | string; | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export type BrowserRequestType = | ||
| 'document' | ||
| 'stylesheet' | ||
| 'image' | ||
| 'media' | ||
| 'font' | ||
| 'script' | ||
| 'texttrack' | ||
| 'xhr' | ||
| 'fetch' | ||
| 'eventsource' | ||
| 'websocket' | ||
| 'manifest' | ||
| 'other'; |
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,34 @@ | ||
import * as puppeteer from 'puppeteer'; | ||
import playwright from 'playwright'; | ||
import { PlaywrightController } from './PlaywrightController'; | ||
import { PuppeteerController } from './PuppeteerController'; | ||
import { BrowserController } from './BrowserController'; | ||
|
||
export type BrowserPage = puppeteer.Page | playwright.Page; | ||
|
||
export class BrowserControllerFactory { | ||
/** | ||
* Return instantiated Playwright or Puppeteer controller | ||
* corresponding to provided page object | ||
*/ | ||
public static getForPage(page: BrowserPage): BrowserController { | ||
if (this.isPlaywrightPage(page)) { | ||
return new PlaywrightController(page); | ||
} else if (this.isPuppeteerPage(page)) { | ||
return new PuppeteerController(page); | ||
} else { | ||
throw new Error( | ||
'Expected instance of Puppeteer or Playwright Page. Got: ' + | ||
page | ||
); | ||
} | ||
} | ||
|
||
private static isPlaywrightPage(page: any): page is playwright.Page { | ||
return typeof page['route'] === 'function'; | ||
} | ||
|
||
private static isPuppeteerPage(page: any): page is puppeteer.Page { | ||
return typeof page['setRequestInterception'] === 'function'; | ||
} | ||
} |
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,65 @@ | ||
import playwright from 'playwright'; | ||
import { | ||
BrowserController, | ||
BrowserRequestHandler, | ||
ResponseData, | ||
BrowserRequest, | ||
BrowserRequestType, | ||
} from './BrowserController'; | ||
import { tryJsonParse } from '../utils'; | ||
import { parse } from 'url'; | ||
|
||
export class PlaywrightController implements BrowserController { | ||
constructor(private readonly page: playwright.Page) {} | ||
|
||
async startInterception(onRequest: BrowserRequestHandler) { | ||
this.page.route('**/*', (request: playwright.Request) => { | ||
onRequest( | ||
this.toBrowserRequest(request), | ||
data => this.respond(request, data), | ||
() => request.continue() | ||
); | ||
}); | ||
} | ||
|
||
private toBrowserRequest(request: playwright.Request): BrowserRequest { | ||
// TODO find a better alternative for url.parse | ||
const { pathname = '', query, protocol, host } = parse( | ||
request.url(), | ||
true | ||
); | ||
|
||
return { | ||
type: request.resourceType() as BrowserRequestType, | ||
url: request.url(), | ||
body: tryJsonParse(request.postData()), | ||
method: request.method(), | ||
headers: request.headers() || {}, | ||
path: pathname, | ||
hostname: `${protocol}//${host}`, | ||
query: query, | ||
sourceOrigin: this.getRequestOrigin(request), | ||
}; | ||
} | ||
|
||
private async respond(request: playwright.Request, response: ResponseData) { | ||
await request.fulfill({ | ||
headers: response.headers || {}, | ||
status: response.status, | ||
body: response.body ? response.body : '', | ||
contentType: response.headers | ||
? response.headers['content-type'] | ||
: 'application/json', | ||
}); | ||
} | ||
|
||
/** | ||
* Obtain request origin url from originating frame url | ||
*/ | ||
private getRequestOrigin(request: playwright.Request) { | ||
const originFrame = request.frame(); | ||
const originFrameUrl = originFrame ? originFrame.url() : ''; | ||
const { protocol, host } = parse(originFrameUrl); | ||
return `${protocol}//${host}`; | ||
} | ||
} |
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,53 @@ | ||
import { Page, Request } from 'puppeteer'; | ||
import { parse } from 'url'; | ||
import { | ||
BrowserController, | ||
BrowserRequest, | ||
BrowserRequestHandler, | ||
} from './BrowserController'; | ||
import { tryJsonParse } from '../utils'; | ||
|
||
export class PuppeteerController implements BrowserController { | ||
constructor(private readonly page: Page) {} | ||
|
||
async startInterception(onRequest: BrowserRequestHandler) { | ||
await this.page.setRequestInterception(true); | ||
this.page.on('request', request => { | ||
onRequest( | ||
this.getRequestData(request), | ||
response => request.respond(response), | ||
() => request.continue() | ||
); | ||
}); | ||
} | ||
|
||
private getRequestData(request: Request): BrowserRequest { | ||
// TODO find a better alternative for url.parse | ||
const { pathname = '', query, protocol, host } = parse( | ||
request.url(), | ||
true | ||
); | ||
|
||
return { | ||
type: request.resourceType(), | ||
url: request.url(), | ||
body: tryJsonParse(request.postData()), | ||
method: request.method(), | ||
headers: request.headers() || {}, | ||
path: pathname, | ||
hostname: `${protocol}//${host}`, | ||
query: query, | ||
sourceOrigin: this.getRequestOrigin(request), | ||
}; | ||
} | ||
|
||
/** | ||
* Obtain request origin url from originating frame url | ||
*/ | ||
private getRequestOrigin(request: Request) { | ||
const originFrame = request.frame(); | ||
const originFrameUrl = originFrame ? originFrame.url() : ''; | ||
const { protocol, host } = parse(originFrameUrl); | ||
return `${protocol}//${host}`; | ||
} | ||
} |
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
Oops, something went wrong.