import { IQueryStatics } from '../common/IQueryStatics'; import { IBuildConfig, ISettings } from "../common/IConfig"; import { IDriver, IDriverManager } from '../common/IDriver'; import { CherrioQuery } from './CherrioQuery'; import { NetworkDriver } from '../fetch/NetworkDriver'; import { IQuery } from '../common/IQuery'; import { CheerioUtils } from './CheerioUtils'; import { SelectorsEx } from '../common/SelectorsEx'; import { FileDetector, Session, Capabilities, Actions, WebElementPromise, Locator, WebElement, Navigation, TargetLocator } from 'selenium-webdriver'; import { Command, Executor } from 'selenium-webdriver/lib/command'; export interface ICheerioBuildConfig extends IBuildConfig { name: 'cheerio' html: string } let driver: CheerioDriverInner; export const CheerioDriver: IQueryStatics = { fromHtml(html: string) { return CheerioDriver.build({ html }); }, build(config: ICheerioBuildConfig): IQuery { let html = config.html; driver = new CheerioDriverInner(config); driver.html = html; let el: any = CheerioUtils.fromHtml(html); let query = new CherrioQuery(el); query.ctx.source = html; return query; }, load(url: string, config: ICheerioBuildConfig): CherrioQuery { driver = new CheerioDriverInner(config); return driver.getAsQuery(url) as any as CherrioQuery; }, fetch(url: string, config: IBuildConfig, setts?: ISettings) { return this.load(url, config, setts); }, setDriver(driver: IDriver) { throw new Error('Cheerio does not support driver'); }, getDriver(config: IBuildConfig, setts?: ISettings): Promise { return Promise.resolve(driver); }, unlockDriver(mix) { driver = null; }, pseudo: SelectorsEx.pseudoFns }; class CheerioDriverInner implements IDriver { public url: string public status: number public headers: { [name: string]: string } public html: string constructor(public config: ICheerioBuildConfig) { } async get(url: string): Promise { let query = CherrioQuery.newAsync(); let resp = await NetworkDriver.load(url, this.config); let html = resp.body.toString(); let $el = CheerioUtils.fromHtml(html); this.url = resp.url; this.headers = resp.headers; this.status = resp.status; this.html = html; query.ctx.source = html; query.ctx.url = url; query.ctx.status = resp.status; query.ctx.headers = resp.headers; query.add($el); query.resolve(query); } async getCurrentUrl(): Promise { return this.url; } async getPageSource(): Promise { return this.html; } async getAsQuery(url: string): Promise { await this.get(url); let query = CherrioQuery.newAsync(); let $el = CheerioUtils.fromHtml(this.html); query.ctx.source = this.html; query.ctx.url = url; query.ctx.status = this.status; query.ctx.headers = this.headers; query.add($el); query.resolve(query); return query as any as Promise; } // NOT IMPLEMENTED manage() { throw new Error('Method not implemented.'); return null; } execute(command: Command, description?: string): Promise { throw new Error('Method not implemented.'); } setFileDetector(detector: FileDetector): void { throw new Error('Method not implemented.'); } getExecutor(): Executor { throw new Error('Method not implemented.'); } getSession(): Promise { throw new Error('Method not implemented.'); } getCapabilities(): Promise { throw new Error('Method not implemented.'); } quit(): Promise { throw new Error('Method not implemented.'); } actions(options?: { async: boolean; bridge: boolean; } | { async: boolean; } | { bridge: boolean; }): Actions { throw new Error('Method not implemented.'); } wait(...args) { throw new Error('Method not implemented.'); return null; } sleep(ms: number): Promise { throw new Error('Method not implemented.'); } getWindowHandle(): Promise { throw new Error('Method not implemented.'); } getAllWindowHandles(): Promise { throw new Error('Method not implemented.'); } close(): Promise { throw new Error('Method not implemented.'); } getTitle(): Promise { throw new Error('Method not implemented.'); } findElement(locator: Locator): WebElementPromise { throw new Error('Method not implemented.'); } findElements(locator: Locator): Promise { throw new Error('Method not implemented.'); } takeScreenshot(): Promise { throw new Error('Method not implemented.'); } navigate(): Navigation { throw new Error('Method not implemented.'); } switchTo(): TargetLocator { throw new Error('Method not implemented.'); } executeScript(script: string, ...var_args: any[]): Promise { throw new Error('Method not implemented.'); } executeAsyncScript(script: string, ...var_args: any[]): Promise { throw new Error('Method not implemented.'); } }