These are chat archives for rosshinkley/nightmare

22nd
Jul 2016
Mingsterism
@mingsterism
Jul 22 2016 09:55
hey @UltimateDeadpool your document.querySelector will not work
movieData.title = document.querySelector('.title_wrapper h1').innerText;
if you are getting error movieData not defined.
what i'd recommend you do is test your css selector in chrome console. type document.querySelector('.title_wrapper h1') and see if that works.
Gabriel Diviricean
@UltimateDeadpool
Jul 22 2016 10:18
it works
if I define the var before movieData.title onto .evaluate everything works
but i need I also need the stuff from the 2nd .evaluate into the same object movieData
Mingsterism
@mingsterism
Jul 22 2016 10:34
can you point out exactlly the line of your error.
paste the stack trace here.
Gabriel Diviricean
@UltimateDeadpool
Jul 22 2016 10:37
it doesn't say the line, all it says is movieData is not defined. I dont know what you mean by stack trace. I'm a newbie
but I think it gives error on line 15, it goes well until then
.evaluate(function () {
    var movieData = {};
    movieData.title = document.querySelector('.title_wrapper h1').innerText;
    movieData.releaseDate = document.querySelector('.subtext a[title*="dates" i]').innerText;
    movieData.rating = document.querySelector('span[itemprop="ratingValue"]').innerText;
    return movieData;
  })
if I do this it shows me the first console.log and it also goes and clicks the next link and gives the same error probably at this line movieData.review = document.querySelector('#tn15content p').innerText;
Mingsterism
@mingsterism
Jul 22 2016 10:42
yeah i get ur issue. just trying it out now.
Gediminas Bivainis
@bivainis
Jul 22 2016 10:58
... although now that i'm thinking about it, i'm pretty sure the default response is 'ok'
I saw this function in the code and tried to experiment with it in order to make it work, but neither passing 'ok', nor 'OK', nor true as a second param to console.log() worked.
If you run this fiddle's url http://s.codepen.io/gebidesign/debug/rLdNOb with nightmarejs, you'll see that once you click the button, the color of the page turns red instead of green (which means confirm got auto-cancelled or had some other falsy value?). The console actually log's the defaultResponse on page event. .on('page', function (type, message, response) { console.log(type, message, response); })
@rosshinkley
Mingsterism
@mingsterism
Jul 22 2016 11:07
@UltimateDeadpool that was tricky.
but here you go.
my output
[mingk@DESKTOP-BMERQIM actionNightmare]$ node movieData.js
Data = {"rating":"5.3","releaseDate":"15 July 2016 (USA)","title":"Ghostbusters (2016)"}
Review = *** This review may contain spoilers ***
{ rating: '5.3',
  releaseDate: '15 July 2016 (USA)',
  review: '*** This review may contain spoilers ***',
  title: 'Ghostbusters (2016)' }
Gabriel Diviricean
@UltimateDeadpool
Jul 22 2016 11:12
thats what I was looking for, thank you
can you explain a little how it works? i don't get it
Mingsterism
@mingsterism
Jul 22 2016 11:26
its all about scope
use this code
var name = Promise.resolve("james");
name
    .then((x) => {
        var names = {}
        names.one = x;
        return names;
    })
    .then((z) => {
        var name2 = "Jenny";
        console.log(z);
        z.two = "xavier";
        return z;
    })
    .then((y) => {
        console.log(y);
    }, (err) => {console.error("Error: ", err)})
.evaluate() creates its own scope as well. thats why its little more tricky
.evaluate() is .evaluate(someFn, fnparameters) this is how you use evaluate.
function multiply(x) {
    return x * x
}
nightmare
    .bla
    .bla
    .evaluate(multiply, 10)  // <<< correct
    .evaluate(multiply(10))  // <<< wrong
.evaluate(function (b) {
        b.review = document.querySelector('#tn15content p').innerText;
        return b;
      }, data)  // passing (data) into evaluate scope which contains the movieData dict
Gabriel Diviricean
@UltimateDeadpool
Jul 22 2016 11:46
I understand now, thanks a lot @mingsterism
Mingsterism
@mingsterism
Jul 22 2016 12:34
no worries :)
Mingsterism
@mingsterism
Jul 22 2016 13:00
@rosshinkley hey ross. want to ask. if i just want to use native generators without libraries. can i do something like this? the code runs without errors. but it does not quit.
function *runYahoo() {
    const botYahoo1 = new CoreBot();
    yield Promise.resolve(botYahoo1.getFinYahooTitles())
}

function mainRun4()    {
    var abc = runYahoo();
    abc.next().value.then((x) => {
            console.log("this should be last");
            return botYahoo1.nightmare.end();
        })
}
this should be last actually gets called first too.
Ross Hinkley
@rosshinkley
Jul 22 2016 14:43
@gebidesign ah, then you should be able to override it with a custom preload
my response was off the cuff without taking a dive into it
if i have time later today, i'll take a closer look
@mingsterism thanks for answering @UltimateDeadpool's question :)
as for native generators without libraries... hm
yyyes, but i'd have to play around
Mingsterism
@mingsterism
Jul 22 2016 14:46
haha. just doing what ever i can to contribute :)
ah. ok. no worries. that issue im just stuck on that.
Ross Hinkley
@rosshinkley
Jul 22 2016 14:47
i wrote about this a while back
... or something close
can i ask why you're against using, say, co?
Mingsterism
@mingsterism
Jul 22 2016 14:48
well. make it less complicated. also, libraries tend to abstract away stuff, so sometimes it gets more complicated than working at lower level
is what i was thinknig of
i would argue that co would make your life much easier
Mingsterism
@mingsterism
Jul 22 2016 14:49
i see. will take a look at co again.
Ross Hinkley
@rosshinkley
Jul 22 2016 14:50
or vo, for that matter
but it's probably worth answering your original question :)
for completeness
Mingsterism
@mingsterism
Jul 22 2016 14:51
yeah. i dont get whats wrong with it. it run through my whole function botYahoo1.getFinYahooTitles() but nightmare does not exit
Ross Hinkley
@rosshinkley
Jul 22 2016 14:51
that's reasonably easy
Mingsterism
@mingsterism
Jul 22 2016 14:51
and this should be last is getting called first. i suspect thats where the problem is
Ross Hinkley
@rosshinkley
Jul 22 2016 14:51
nightmare isn't exiting because .end() is queued but not executed with .then()
the this should be last part ... is a little trickier, i think
Mingsterism
@mingsterism
Jul 22 2016 14:56
oh. i see. yeap. it did work in the end
but funny, i tried back my original one, nightmare somehow ended.
function yahooTitles() {
    const botYahoo = new CoreBot()
    return Promise.resolve(botYahoo.getFinYahooTitles())
        .then((x) => {
            console.log('finished getting titles');
            console.log(x);
            return botYahoo.nightmare.end();
        })
        // .then(() => {
        //     console.log("ended ");
        // })
}
Mingsterism
@mingsterism
Jul 22 2016 15:04
hey Ross. do u have any thoughts how to do this?
```
const settings = {
        result: document.querySelectorAll('selector'),
        price: document.querySelectorAll('selector'),
        ...
        ...
        N: document.querySelector('selector');  // there are N variables in settings
    }

for (x in settings) {
    result.map   // map all N variables with innerText and Zip them
}
Ross Hinkley
@rosshinkley
Jul 22 2016 15:05
"and zip them"
... like... gzip?
Mingsterism
@mingsterism
Jul 22 2016 15:06
like this
function extraction(settings) {
        const result = Array.from(document.getElementsByClassName(settings.result)) 
        const price = Array.from(document.querySelectorAll(settings.price))
        return result.map((x, y) => {
            return JSON.stringify([x.innerText, price[y].innerText])
        })
    }
but the thing is this is much more rigid. specific to only 2 variables, result and price
im wnated to make it generic for N variables.
Ross Hinkley
@rosshinkley
Jul 22 2016 15:07
i think i understand
Mingsterism
@mingsterism
Jul 22 2016 15:07
javascript has no zip, but the idea of zip is from python
Ross Hinkley
@rosshinkley
Jul 22 2016 15:07
you want to change from arrays to having all of the positional parts together in objects
Mingsterism
@mingsterism
Jul 22 2016 15:08
arr1 = ["A", "B", "C"]
arr2 = [10, 20, 30]
arr1.zip(arr2) = [A: 10, B:20, C:30]
soemthing like that
Ross Hinkley
@rosshinkley
Jul 22 2016 15:08
yeah
object zip
not ... like...compression zip
sorry
Mingsterism
@mingsterism
Jul 22 2016 15:08
haha. yeah not compression zip.
someone said transpose but i couldnt work it out
The idea is every page has different amount of selectors. so rather than changing the function everytime, it just accepts a different settings parameter. all need to set is the object literal settings.
const page1 = {
    sel1: document.querySelectorAll('selector'),
    sel2: document.querySelectorAll('selector'),
}

const page2 = {
    sel1: document.querySelectorAll('selector'),
    sel2: document.querySelectorAll('selector'),
    sel3: document.querySelectorAll('selector'),
    sel4: document.querySelectorAll('selector'),
}
rosshinkley @rosshinkley nods
Ross Hinkley
@rosshinkley
Jul 22 2016 15:10
i follow
Mingsterism
@mingsterism
Jul 22 2016 15:11
i got to go now. but if you have any thoughts. pls let me know. really cracking my head :)
Ross Hinkley
@rosshinkley
Jul 22 2016 15:11
sure
Matthew Steedman
@knubie
Jul 22 2016 15:57
Has anyone used phantomjs as part of their testing suite on circleci?
Matthew Steedman
@knubie
Jul 22 2016 16:07
sorry meant to say nightmarejs, not phantomjs
Rob Brackett
@Mr0grog
Jul 22 2016 16:33
well, Nightmare tests itself on Circle, if that helps