const Nick = require("../../lib/Nick")
const nick = new Nick()
const testLog = (text) => {
console.log(`>> ${text}`)
}
const checkUrl = (url, checkbox, color, date, email, number, radio, range, text) => {
url = decodeURIComponent(url)
if(url.indexOf(`checkbox=${(0 + checkbox)}`) >= 0)
testLog(`Checkbox set to ${checkbox}`)
else
testLog("Checkbox set to false")
if(url.indexOf(`color=${color}` >= 0))
testLog(`Color set to ${color}`)
if(url.indexOf(`date=${date}`) >= 0) {
if (date === (new Date()).toISOString().slice(0, 10))
testLog("Date set to today's date")
else
testLog(`Date set to ${date}`)
}
if(url.indexOf(`email=${email}`) >= 0)
testLog(`Email set to ${email}`)
if(url.indexOf(`number=${number}`) >= 0)
testLog(`Number set to ${number}`)
if(url.indexOf(`radio=${(0 + radio)}`) >= 0)
testLog(`Radio set to ${radio}`)
else
testLog("Radio set to false")
if(url.indexOf(`range=${range}`) >= 0)
testLog(`Range set to ${range}`)
if(url.indexOf(`text=${text.replace(/\s/g, "+")}`) >= 0)
testLog(`Text set to ${text}`)
}
const testFill = async (tab, checkbox, color, date, email, number, radio, range, text, submit, file) => {
await tab.fill("form", {
"checkbox": checkbox,
"color": color,
"date": date,
"email": email,
"number": number,
"radio": radio,
"range": range,
"text": text
}, {submit: true})
testLog("Form filled")
await tab.wait(1000)
testLog("Wait done")
checkUrl(await tab.getUrl(), checkbox, color, date, email, number, radio, range, text)
}
;(async () => {
const tab = await nick.newTab()
await tab.open("localhost:8080/form.html")
testLog("Page opened")
await tab.waitUntilVisible("form")
testLog("Page loaded")
const content = await tab.getContent()
if (content.indexOf("<!-- HTML COMMENTS -->") >= 0)
testLog("Get content done")
var date = new Date();
var old = new Date("2000-01-01");
var currentDate = date.toISOString().slice(0,10)
var oldDate = old.toISOString().slice(0, 10)
await testFill(tab, true, "#35C2DB", currentDate, "[email protected]", 20, true, 1, "NickJS test")
await testFill(tab, false, "#35C2CB", oldDate, "[email protected]", 10, false, 10, "NickJS2 test")
nick.exit()
})()
.catch((err) => {
console.log(err)
nick.exit(1)
})
const Nick = require("../../lib/Nick")
const nick = new Nick()
const testLog = (text) => {
console.log(`>> ${text}`)
}
const scrape = (arg, done) => {
var data = $("div.person > div.panel-body").map(function () {
return({
name: $(this).find(".name").text().trim(),
birth_year: $(this).find(".birth_year").text().trim()
})
})
done(null, $.makeArray(data))
}
;(async () => {
const tab = await nick.newTab()
await nick.deleteAllCookies()
let cookies = await nick.getAllCookies()
if (Array.isArray(cookies)) {
testLog(`got an array of cookies`)
testLog(`we have ${cookies.length} cookies`)
}
await tab.open("http://scraping-challenges.phantombuster.com/cookies")
await tab.waitUntilVisible("div.container div.jumbotron h1")
testLog("'wrong cookies' message is visible")
await nick.setCookie({
name: "phantomCookie",
value: "sample_value",
domain: "scraping-challenges.phantombuster.com"
})
cookies = await nick.getAllCookies()
if (cookies.length >= 1) {
testLog(`we have at least one cookie`)
}
for (const c of cookies) {
if (c.value === "sample_value" && c.name === "phantomCookie") {
testLog(`found a cookie with value sample_value and name phantomCookie`)
}
}
await tab.open("http://scraping-challenges.phantombuster.com/cookies")
await tab.waitUntilVisible(".panel-body")
testLog("Page loaded")
await tab.inject("tests/assets/jquery-3.2.1.min.js")
testLog("Local jQuery injected")
const result = await tab.evaluate(scrape)
testLog("Evaluate done")
testLog(`Tenth result: ${result[9].name}`)
testLog(`Result size: ${result.length}`)
await tab.setCookie({
name: "test-cookie-1",
value: "test-value-1",
domain: "google.fr"
})
cookies = await tab.getAllCookies()
if (cookies.length >= 2) {
testLog(`we have at least two cookies`)
}
for (const c of cookies) {
if (c.value === "test-value-1") {
testLog(`we found our test-cookie-1`)
}
}
await nick.deleteCookie("test-cookie-1", "google.fr")
cookies = await tab.getAllCookies()
if (cookies.length >= 1) {
testLog(`we have at least one cookie`)
}
let found = false
for (const c of cookies) {
if (c.name === "test-cookie-1") {
found = true
console.log("We have found this cookie that should have been deleted...", JSON.stringify(c, undefined, 4))
}
}
if (!found) {
testLog(`we havent found the deleted cookie`)
}
await tab.deleteAllCookies()
cookies = await tab.getAllCookies()
if (cookies.length === 0) {
testLog(`all cookies were deleted`)
}
nick.exit(0)
})()
.catch((err) => {
console.log(err)
nick.exit(1)
})
const Nick = require("../../lib/Nick")
const nick = new Nick()
const testLog = (text) => {
console.log(`>> ${text}`)
}
;(async () => {
const tab = await nick.newTab()
await tab.open("localhost:8080/elements.html")
await tab.waitWhileVisible("#first")
testLog("Wait while visible done")
await tab.waitUntilVisible("#second")
testLog("Wait until visible done")
await tab.waitUntilPresent("#third")
testLog("Wait until present done")
await tab.waitWhilePresent("#fourth")
testLog("Wait while present done")
try {
await tab.waitWhileVisible("#second", 500)
} catch (error) {
testLog("Wait while visible fail")
}
try {
await tab.waitUntilVisible("#first", 500)
} catch (error) {
testLog("Wait until visible fail")
}
try {
await tab.waitUntilPresent("#fourth", 500)
} catch (error) {
testLog("Wait until present fail")
}
try {
await tab.waitWhilePresent("#third", 500)
} catch (error) {
testLog("Wait while present fail")
}
if (!await tab.isVisible("#first")) {
testLog("Is visisble fail")
}
if (await tab.isPresent("#third")) {
testLog("Is present done")
}
tab.isVisible("#second", (err, visible) => {
if (err) {
console.log(err)
nick.exit(1)
}
if (visible) {
testLog("Is visible done")
}
tab.isPresent("#fourth", (err, present) => {
if (err) {
console.log(err)
nick.exit(1)
}
if (!present) {
testLog("Is present fail")
}
nick.exit(0)
})
})
})()
.catch((err) => {
console.log(err)
nick.exit(1)
})