vo(function* () { var nightmare = Nightmare({ show: true }); var link = yield nightmare .goto('http://localhost') .wait(1000) .click('div.col-sm-2.buttonSushi') .wait(1000) .click('button.btn.Sushi0') .wait(1000) .click('div.col-sm-2.buttonBento') .wait(1000) .click('button.btn.Bento0') .wait(1000) .click('div.col-sm-2.buttonNoodle') .wait(1000) .click('button.btn.Noodle0') .wait(1000) .click('button.btn.Noodle0') .wait(1000) .click('div.col-sm-2.buttonDrink') .wait(1000) .click('button.btn.Drink0') .wait(10000) .click('div.col-sm-2.buttonSushi') .wait(1000) .click('button.btn.Sushi2') .wait(1000) yield nightmare.end(); return link; })(function (err, result) {
function next (url, cbFn) { nightmare() .goto(url) .evaluate(query) .end() .run(analyze) function query () { return { date: (document.querySelector('.listing-header h3')||{}).innerText||null, skills: null, requirements: null, title: (document.querySelector('.listing-header h1')||{}).innerText||null, type: null, payment: null, duration: null, budget: null, description: (document.querySelector('.listing-container')||{}).innerText||'', details: null, company: (((document.querySelector('.listing-header h2')||{}).innerText||'').split('\n')[0])||null, location: null, benefits: null } } function analyze (error, item) { if (error) return cbFn(error) meta({ item, raw: item.description }, cbFn) } }
function next (url, cbFn) { nightmare() .goto(url) .evaluate(query) .end() .run(analyze) function query () { return { date: null, skills: null, requirements: null, title: null, type: null, payment: null, duration: null, budget: null, description: (document.querySelector('.thing .entry .expando .usertext .usertext-body .md')||{}).innerText||'', details: null, company: null, location: null, benefits: null } } function analyze (error, item) { if (error) return cbFn(error) meta({ item, raw: item.description }, cbFn) } }
function next (item, cbFn) { nightmare() .goto(item.url) .evaluate(query) .end() .run(analyze) function query (){ return { date: (document.querySelector('.job-entry .date')||{}).innerText||'', skills: null, requirements: null, title: (document.querySelector('.job-entry h2')||{}).innerText||'', type: (document.querySelector('.job-entry .tags')||{}).innerText||'', payment: null, duration: null, budget: null, description: [...document.querySelectorAll('.job-entry p')].map(x => x.innerText||'').join('\n'), details: null, company: ((document.querySelector('.job-entry .author')||{}).innerText||'').split('(')[0]||null, location: null, benefits: null } } function analyze (error, item) { if (error) return cbFn(error) meta({ item, raw: item.description }, cbFn) } }
function screenshot (room) { // let uri = `http://petakampus.itb.ac.id/lantai_gmap2.php?id_gedung=${room.buildingId}&id_lantai=${room.floorId}&gid=${room.gId}` let uri = `http://petakampus.itb.ac.id/lantai_gmap2.php?id_gedung=100138&id_lantai=10013801&gid=41` let nightmare = Nightmare({ show: true }) // console.dir(room) console.log(uri) setTimeout(() => { nightmare .goto(uri) .wait(10000) // .screenshot(`./data/img/${room.gId}-${room.building}-${room.name}.png`, { // x: 424, // y: 27, // width: 480, // height: 160 // }) .end() .then((res) => { console.log(`SUCCESS MAPPING room ${room.gId}`) // next() }).catch((error) => { console.log(`ERROR MAPPING room ${room.gId}`) // next() }); }, 100) }
const next = () => { const nightmare = Nightmare(opts.nightmare) // eslint-disable-line new-cap return nightmare.goto(url) .evaluate(() => document.documentElement.outerHTML) .end() .then(res => cheerio.load(res, cheerioOpts)) }
const generateUrl = async (email, password, firstRun = false) => { const nightmare = Nightmare(); let url = await nightmare .goto('https://codestitch.io/auth/sign_in') .type('#user_email', email) .type('#user_password', password) .click('input[value="Sign in"]') .wait('img', 2000) // Hacky way to wait until done logging in .url(); if (url !== 'https://codestitch.io/pads') { throw new Error(`Couldn't log in.`); } url = await nightmare .goto('https://codestitch.io/pads/new') .wait('#run', 2000) // Hacky way to wait until pad created .url(); if (!url.match(/[a-z0-9]{8}$/)) { throw new Error(`Couldn't get to pad.`); } // .then after .end is required nightmare.end(() => 'Headless browser stopped.').then(console.log.bind(console)); if (firstRun) { return url; } else { fs.writeFile(path.join(__dirname, 'codestitchUrl.txt'), url, () => console.log(`New Codestitch URL generated for next interview: ${url}.`)); } };
vo(function* () { /* Nightmare Options */ var nightmare = Nightmare( { plugins: true, allowDisplayingInsecureContent: true, allowRunningInsecureContent: true }) /* Open page in nightmare and read svg result */ var result = yield nightmare .goto(path.join('file://', graphifyPath, 'app/index.html')) .type('#txtInput', input) .click('#btnInput') .wait('#svgOutput') .wait(1000) .evaluate(function(css) { var svg = document.getElementById('svgOutput') svg.innerHTML = "<style> /* <![CDATA[ */" + css + "/* ]]> */ </style>" + svg.innerHTML return svg.outerHTML }, css) yield nightmare.end() resolve(result) })(function (err, result)
function next (url, cbFn) { nightmare() .goto(url) .evaluate(query) .end() .run(analyze) function query () { return { date: null, skills: (document.querySelector('#project .project-categories')||{}).innerText||'', requirements: null, title: (document.querySelector('#project .headline-lightblue')||{}).innerText||'', type: (document.querySelectorAll('#project .project-details .project-detail-description')[0]||{}).innerText||'', payment: null, // fixed / per hour duration: (document.querySelectorAll('#project .project-details .project-detail-description')[2]||{}).innerText||'', budget: null, description: ((document.querySelector('.projectcontent')||{}).innerText||'').split(" Apply now")[1]||'', details: null, company: null, location: (document.querySelectorAll('#project .project-details .project-detail-description')[4]||{}).innerText||'', benefits: null } } function analyze (error, item) { if (error) return cbFn(error) meta({ item, raw: item.description }, cbFn) } }
function *runNotas(cookie, aluno) { if(!cookie || !aluno) return; let nightmare = Nightmare({ show: false, webPreferences: { images: false } }); yield nightmare .useragent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36') .cookies.set({ 'url': 'http://apsweb.senacrs.com.br/modulos/aluno/boletim.php5?aluno=' + aluno, name: 'PHPSESSID', value: cookie, domain: 'apsweb.senacrs.com.br', hostOnly: true, path: '/', secure: false, httpOnly: false, session: true }) .goto('http://apsweb.senacrs.com.br/modulos/aluno/boletim.php5?aluno=' + aluno) yield nightmare.end(); return true; }
function execute (opts, done) { if (typeof done !== 'function') return opts = opts || { show: false } nightmare(opts) .goto(`http://${URL}`) .evaluate(query) .end() .run(collect) function collect (error, urls) { if (error) return done(error) var DATA = [] var total = urls.length if (urls.length) next(urls.pop(), callback) function callback (error, data) { if (error) return done(error) if (data) DATA.push(data) console.log(`${urls.length}/${total} - ${URL}`) if (urls.length) return next(urls.pop(), callback) done(null, { NAME, DATA }) } } }
function* run() { var nightmare = Nightmare({ show: true }), MAX_PAGE = 10, currentPage = 0, nextExists = true, links = []; yield nightmare .useragent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36') .goto('https://www.yahoo.com') .type('#uh-search-box', 'github nightmare') .click('#uh-search-button') .wait(3000); nextExists = yield nightmare.visible('.next'); while (nextExists && currentPage < MAX_PAGE) { links.push(yield nightmare .evaluate(function() { var links = document.querySelectorAll('ol.searchCenterMiddle a'); return links[0].href; })); yield nightmare .click('.next') .wait('body'); currentPage++; nextExists = yield nightmare.visible('.next'); } console.dir(links); yield nightmare.end(); }
it('has title "React Apollo Koa Example"', async () => { const title = await Nightmare() .goto(`${BASE_URL}`) .evaluate(() => document.title) .end(); expect(title).toEqual('React Apollo Koa Example'); });
export function scrape (cb) { const nightmare = Nightmare({ show: false }) nightmare .goto(url) .wait('h1 ~ table tbody') .evaluate((url) => { function toDate(rawDate, rawTime) { const date = `0${parseInt(rawDate, 10)}`.slice(-2); const month = ('0' + ([ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ].indexOf((rawDate.match(/\w+$/) || [])[0] || 'Jan') + 1)).slice(-2); const year = 2016; const hours = `0${parseInt(rawTime, 10)}`.slice(-2); const minutes = ('0' + (rawTime.match(/\d+$/) || [])[0] || '0').slice(-2); return new Date(`${year}-${month}-${date}T${hours}:${minutes}:00+0300`); } return [].slice.call(document.querySelectorAll('h1 ~ table tbody tr'), 0) // Remove headline rows .filter(e => e.children.length > 1) // Normalize data .map(e => { const startTime = toDate(e.querySelector('td:nth-child(2)').innerText, e.querySelector('td:nth-child(3)').innerText); const endTime = new Date(startTime.getTime()); endTime.setHours(endTime.getHours() + 1); return { category: 'Pludmales volejbols', start_time: startTime.toUTCString(), end_time: endTime.toUTCString(), title: e.querySelector('td:nth-child(5)').innerText + " vs " + e.querySelector('td:nth-child(6)').innerText, scraped_from: url, place: 'Antalya', attributes: {} }; }) // Only events with lv guys .filter(e => /(Samoilovs|Plavins|Finsters)/.test(e.title)); }, url) .end() .then(result => { cb(null, result); }) .catch(err => { cb(err); }); }
before(function () { if (osType !== 'Linux') { nightmare = Nightmare({ show: true }); } connector = new WordpressConnector({clientId, clientSecret}); });
export function createBrowser() { return Nightmare({ show: false, waitTimeout: 5 * 1000, webPreferences: { webSecurity: false } }) }
beforeEach(() => { page = Nightmare(); page .goto('http://localhost:8000/') .evaluate(() => { window.localStorage.setItem('antd-pro-authority', 'guest'); }) .goto('http://localhost:8000/#/user/login'); });
export default function (path = '') { const location = url.resolve(BASE_URL, path) const page = nightmare({ show: DEBUG, pollInterval: 50 }) return page.goto(location) }
async function main(argv) { const script = require(argv[2]); const nightmare = Nightmare(script.nightmare_props); let atlasLoginInfo = {}; let chartsLoginInfo = {}; if (argv[3] !== undefined) { const properties = PropertiesReader(argv[3]); atlasLoginInfo = { username: properties.get('atlasUser.login.username'), password: properties.get('atlasUser.login.password') } chartsLoginInfo = { email: properties.get('chartsUser.login.email'), password: properties.get('chartsUser.login.password') } } const screenshotNames = await script.run({ nightmare: nightmare, screenshot_dir: './screenshots-temp/', loginToAtlas: function() { nightmare.goto('https://cloud.mongodb.com/user#/atlas/login') nightmare.wait('input[name="username"]'); if (atlasLoginInfo.username && atlasLoginInfo.password) { nightmare.type('input[name="username"]', atlasLoginInfo.username); nightmare.type('input[name="password"]', atlasLoginInfo.password); } else { throw new Error("No login information specified"); } nightmare.click('.login-form-submit-button'); }, loginToCharts: function() { nightmare.goto('http://charts.mongodb.parts/login') nightmare.wait('input#email'); if (chartsLoginInfo.email && chartsLoginInfo.password) { nightmare.type('input#email', chartsLoginInfo.email); nightmare.type('input#password', chartsLoginInfo.password); } else { throw new Error("No login information specified"); } nightmare.click('[data-test-id="login-form-submit-button"]'); } }); // Compare the images for (const image of screenshotNames) { await util.compareImages(image); } }
it('เลือกหนังสือเล่ม1 จำนวน 1 เล่ม ', function * () { this.timeout(30000) var nightmare = Nightmare() var link = yield nightmare .goto('http://localhost:5000') .click('#book1') .evaluate(function () { return document.querySelector('#sum').innerHTML }) expect(link).to.equal('100') })
it('bundles scripts', co.wrap(function* () { const url = `http://localhost:${ port }/index.html`, text = yield Nightmare() .goto(url) .wait() .evaluate(() => document.querySelector('md').innerHTML) .end() assert.equal(text, '<p>Hello <strong>Rollup</strong>!</p>\n') }))
function nextPage (error, data) { //because of .run, we need 2 arguments: err & result if (error) return done(error) allUrls = allUrls.concat(data.urls) console.log(`collected urls: ${allUrls.length}`) var next = data.next if (next) return nightmare(opts) .goto(next) .evaluate(query) .end() .run(nextPage) collect(error, allUrls) }
it('test 24-25', function * () { this.timeout(15000) var nightmare = Nightmare() var value = yield nightmare .goto('http://localhost:8080/') .type('input[id="cusNameID"]', 'Chanwit') .type('input[placeholder="เบอร์โทร"]', '0805629799') .click('#btnSub') .evaluate(function () { return document.querySelector('td#datereser-0').innerHTML }) expect(value).to.equal('24/04/2016 - 26/04/2016') })
function Before() { this.browser = Browser({ waitTimeout: 2e3, webPreferences: { partition: 'test' }, // Store session in memory // show: true, }); this.browser.ctx = {}; this.browser.baseUrl = 'http://localhost:3000'; for (const [key, PageObject] of Object.entries(pageObjects)) { this[key] = new PageObject({ browser: this.browser }); } }
it('choose 1 book', function * () { this.timeout(30000) var nightmare = Nightmare() var link = yield nightmare .goto('http://localhost:5000') .type('input[title="Search"]', 'github nightmare') .click('#book1') .wait(3000) .evaluate(function () { return document.querySelector('#total').innerHTML }) expect(link).to.equal('100') })
test('change text', function (t) { t.plan(1) Nightmare() .goto(process.env.URI) .click('button') .evaluate(function () { return document.querySelector('button').innerText }) .end() .then(function (text) { t.equal(text, 'hello world') }) })
function *runLogin(username, password, unidade) { let nightmare = Nightmare({ show: false, webPreferences: { images: false } }); // Efetua o Login yield nightmare .useragent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36') .goto('http://apsweb.senacrs.com.br/modulos/aluno') .wait('a') .click('a') .wait(100) .wait('body') .evaluate(function(){ oAjax.requester.map(function(el){ el.abort() }); }) .inject('js', __dirname + '/evil.js') .select('#lstUnidades', unidade) .type('#usr', username) .type('[name=passwd]', password) .click('#btnEntrar') .wait(function(){ var textFailed = document.querySelector('span[style="color:red"]'); var loginFailed = textFailed ? textFailed.innerHTML.trim() !== '': false; var loginSuccess = document.querySelectorAll('iframe').length; return loginFailed || loginSuccess; }); let isLoggedIn = yield nightmare .evaluate(function(){ return document.querySelectorAll('iframe').length; }); let cookies = yield nightmare .cookies.get() .then(function(cookies){ return cookies; }); yield nightmare.end(); if(!isLoggedIn) return false; return Browser.mapCookies(cookies); }
return new Promise(function (resolve, reject) { var flags = { error: false // Whether one or more tasks threw an error }; var gotoStack = []; var nightmare = Nightmare(options); nightmare.on('did-get-response-details', function (event, status, newURL, originalURL, httpResponseCode, requestMethod, referrer, headers) { // Handle HTTP errors thrown by goto tasks var gotoIndex = _.findIndex(gotoStack, function (o) { return newURL === o.url && false === o.flags.run; }); if (gotoIndex > -1) { gotoStack[gotoIndex].flags.run = true; gotoStack[gotoIndex].httpResponseCode = httpResponseCode; // Client (400) or server (500) error if (httpResponseCode >= 400) { flags.error = true; self.emit('httpError', newURL, httpResponseCode); } } }); _.forEach(tasks, function (value, index) { var task = value[0]; var args = _.slice(value, 1); if ('goto' === task) { gotoStack.push({ url: args[0], flags: { run: false }, httpResponseCode: undefined }); } nightmare[task].apply(nightmare, args); }); return nightmare.end().then(function () { if (flags.error) { reject(); } else { resolve.apply(nightmare, arguments); } }, reject); });
todo.push(new Promise(resolve => { console.log(`* Printing page to ${path.resolve(__dirname, 'classes', 'pdfs', file + '.pdf')} ...`); Nightmare() .goto(`http://localhost:8080/${file}.md?print-pdf`) .pdf(path.resolve(__dirname, 'classes', 'pdfs', file + '.pdf')) .end() .then(resolve) .catch((err) => { console.log(`Error while printing ${file}: ${err}`) // to avoid stopping resolve(); }); }));
it('discount20', function * () { this.timeout(30000) var nightmare = Nightmare() var value = yield nightmare .goto('http://localhost:5000') .click('.btn0') .click('.btn0') .click('.btn1') .evaluate(function () { return document.querySelector('.discount').innerHTML }) expect(value).to.equal('20.00') })