0.1.11 • Published 8 years ago
chrome-ditto v0.1.11
Chrome Ditto
Common API for Nightmare, Puppeteer
Install
yarn add chrome-ditto
# or npm
npm i chrome-dittoUsage
const Ditto = require('chrome-ditto')
const Nightmre = require('nightmare')
// const puppeteer = require('puppeteer')
(async () => {
const browser = await Ditto(Nightmare)
// const browser = Ditto(puppeteer)
const page = await browser.newPage()
await page.goto('http://www.example.com/')
await page.wait('h1')
let content = await page.evaluate(() => {
return document.querySelector('p').textContent.trim()
})
console.log('content:', content)
await page.screenshot('./example.png')
await browser.close()
})()API
Ditto
Ditto(model, options: DittoOptions): Browsermodel: Nightmare | Puppeteer, that you want to transformDittoOptionsshow:booleanshow browser or not
showImages:booleanignoreHTTPSErrors:booleanwaitTimeout:numbersessionId:stringpersist states between pages
Browser
Init
init(options: DittoOptions): Promise
Properties
modelname: stringname of
Browser,'nightmare', 'puppeteer'etc.
Page
Properties
insinstance of model
options: DittoOptions
Setters of Page
setUserAgent(ua: string): PromisesetViewport(width: number, height: number): Promise
Actions
click(selector: string): Promiseclose(): Promisegoto(url: string): Promisetype(selector: string, text: string): Promise
Extract
evaluate(func: Function, arg1, arg2...): Promisehtml(): Promise<string>screenshot(relativePath: string): Promise
Waiting
wait(time: number): Promisewait(selector: string, timeout: number): Promisewait(func: Function, timeout: number, arg1, arg2...): Promisefunc(arg1, arg2...): boolean
waitOne(waitings: Array<string|Function>, timeout: number): Promise<number>const isFooPage = () => !!window.location.href.match(/foo/) const flags = ['#succeessBox', '#failedBox', isfooPage] let indexOfFlag = await page.waitOne(flags) switch(indexOfFlag) { case 0: console.log('succeess') break case 1: console.log('failed') break case 2: throw 'foo?' break }