These are chat archives for rosshinkley/nightmare

31st
Oct 2016
Chris Nguyen
@nguyenchr
Oct 31 2016 08:57
hey guys, I was hoping someone might have an idea of what I am doing wrong here
it('should show stories', function(done) {
    Nightmare()
      .goto(CONFIG.url)
      .wait('.story-list-item')
      .evaluate(function () {
        return document.querySelectorAll('.story-list-item')
      })
      .end()
      .then(function(stories) {
        console.log(stories.length)
        done();
      })
      .catch(function(err) {
        console.log('in the catch', err.message)
      })
  });
I’m trying to get all elements with the class .story-list-item, the above code hangs
however if i do
it('should show stories', function(done) {
    Nightmare()
      .goto(CONFIG.url)
      .wait('.story-list-item')
      .evaluate(function () {
        return document.querySelectorAll('.story-list-item')[0].textContent
      })
      .end()
      .then(function(stories) {
        console.log(stories.length)
        done();
      })
      .catch(function(err) {
        console.log('in the catch', err.message)
      })
  });
it correctly returns the first text within the first element
document.querySelectorAll('.story-list-item’).length seems to work ok within the browser itself
Chris Nguyen
@nguyenchr
Oct 31 2016 09:31
I think I found the answer, I guess you cannot return NodeList objects from the evaluate call, and you can map it into a simple array and return it within the evaluate function
jomcat
@jomcat
Oct 31 2016 11:31

I've given this a few days before asking for newbie help but I now have to ask and beg for help! I want to put nightmare code into a module and call functions for regular reuse.

My issue is that when I call .use from .then it doesn't execute the code. My test code should got to Google.com, then Example.com and then Yahoo.com. I know I can get it to do that but I want it to do so in a module as a POC. Here's what I have that doesn't work:
usetest.js

  var Nightmare = require('nightmare');
  var moduletest = require('./usetestmodule');
  var nightmare = Nightmare({ show: true });

 nightmare
  .cookies.clear()
  .goto('https://www.google.com')
  .wait(2000)
  .exists('.mainarea')
  .then(function(result){
    // not doing anything with result just for testing
    nightmare
    .wait(2000)
    .goto('https://www.example.com')
    .wait(2000)
// if I comment out the next line the previous gotos work.
    nightmare.use(moduletest.testThens(nightmare))
  })
  .then(function(){
    return nightmare.end();
  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

usetestmodule.js

exports.testThens = function (testThensNightmare){
    return testThensNightmare
    .goto('http://yahoo.com')
    .wait(2000)
}

Any help would be great!!!
John

Rob Brackett
@Mr0grog
Oct 31 2016 16:01
@nguyenchr Two things:
  1. It might be helpful to consider that, when using Nightmare, data is being passed around between three entirely separate programs (your JS, the "browser" application (Electron), and the browser window (yes, this is actually a separate application!)). So whenever you return data from evaluate, it's getting passed off between several programs. The raw JS objects (like a DOM element) can't actually be sent that way, so what you actually get is an object deconstructed and reconstructed each time. Not all information can be preserved when that happens.
  2. BUT! It definitely shouldn't hang when you return something that is not OK. This issue sounds a lot like the one @Siilwyn was experiencing, but hasn't filed an issue for. So if you could file an issue with details (like the minimum code necessary to cause the hang), that'd be great.
Chris Nguyen
@nguyenchr
Oct 31 2016 19:38
@Mr0grog thanks for the explanation, it makes a lot of sense, I will add an issue