phobetor v0.3.3
Phobetor
Browser emulation library build with Nightmare.js
Nightmare.js is great library, but you cannot straightforward pass functions as arguments to the evaluation context.
Phobetor uses eval to run those functions in browser's context and give you back the result.
Example use
Reddit login
// yarn add phobetor
const browser = require("phobetor");
(async () => {
browser.init({show:true})
await browser.get("https://www.reddit.com/")
await browser.wait({condition:1000})
await browser.type({
selector:"input[name='user']",
payload:"foo"
})
await browser.type({
selector:"input[name='passwd']",
payload:"bar"
})
const submitButtonSelector = ".submit button[type=submit]"
await browser.click({
selector: submitButtonSelector
})
await browser.sleep(2000)
await browser.close()
})()This example is part of Phobetor's showcase project Reddit voter, which logs into Reddit with your credentials, redirect to declared subreddit and then vote randomly.
Naming
Phobetor is Greek god of nightmares

API
Two most important functions in 'Phobetor' are 'getElements' and 'getSingleElement'. Most of the other methods are build using those two functions.
getElements
Typing
getElements({ constants?: any, evaluate?: Function, filter?: Function<Boolean>, map?: Function, selector: String }): Promise
- selector(required)
Elements query selector compatible with document.querySelectorAll
- filter
Boolean returning function. You can use it to filter the result of document.querySelectorAll(selector).
If filter fails to return true for at least one element, you will receive null as result.
- map
Function that will map over the returned elements from filter
- evaluate
Function that receives result of map as argument. What evaluate returns is the actual return value of getElements
- constants
You can use constants to pass values to browser context
Example
const browser = require("phobetor");
browser.init({show:true})
const constants = "placeholder"
const result = await browser.getElements({
constants: {limit: 100},
selector:".foo",
filter: element => element.innerHTML.length > constants.limit,
map: element => element.querySelector("p").textContent,
evaluate: elements => elements.join("\n")
})Notice that in order to use
constantsin our functions, you need to have it in your context, when you declareevaluate-like functions.
For convenience this method has alias '$$' so you can use it also like that:
const result = await browser.$$({
...
})getSingleElement
Typing
getSingleElement({ constants?: any, evaluate?: Function, filter?: Function, selector: String }): Promise
- selector*(required)
Element query selector compatible with document.querySelector
- filter
Boolean returning function. You can use it to filter the result of document.querySelector(selector)
If filter return false, you will receive null as result.
- evaluate
Function that receives result of document.querySelector(selector) as argument. What evaluate returns is the actual return value of getSingleElement
- constants
You can use constants to pass values to browser context
Example
const constants = "placeholder"
const result = await browser.getSingleElement({
constants: "baz",
selector:".foo",
filter: element => element.innerHTML.includes(constants),
evaluate: element => element.textContent
})As you may expect, when filter is evaluated, it will replace constants with "baz" not "placeholder".
For convenience this method has alias '$' so you can use it also like that:
const result = await browser.$({
...
})getNightmareInstance(): Object
Return the same Nightmare.js instance as the library uses itself.
init(options: Object)
options are used to create Nightmare.js instance
Check requirements at Nightmare.js's page
get(url: String): Promise
Same as Nightmare.js.goto
Check Nightmare.js's desription for
goto
wait
Typing
wait({ condition: String|Function|Number, conditionArguments?: any, timeout?: Number }): Promise
conditionis passed toNightmare.js's waitIf
conditionis a function, it is passed toNightmare.js's wait withconditionArgumentsas argument.timeoutis delay which race withcondition. Thus even ifconditionfails, you can proceed as nothing happened. Default value is 5000ms
isElementPresent
isElementPresent({selector:String}):Promise<Boolean>
sleep
sleep(ms: Number): Promise
setTimeout as a promise
delay
delay(ms: Number): Promise
setTimeout as a promise