Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I run say a nightmare with login, then start a loop and then pick up where I left off? #88

Closed
Cramertech opened this issue Oct 27, 2014 · 2 comments

Comments

@Cramertech
Copy link

I'm just writing to ask if there is a simple way I can accomplish this.

I want to know if I can run a part of a nightmare, then stop and do whatever I like...say start a for loop, and then within that loop start up some more of the nightmare where the nightmare left off. As in, still logged in that is. Then I'd run a little more chunk of nightmare code, and then upon the next iteration begin some more nightmare code....all on the same login.

I'll try and write some psuedo-code to help explain it.

Nightmare = req (nightmare)

var myArray = [.., .., .., ..];
var newArray = [];
var myNM = new Nightmare

//Initial login with my nightmare.
myNM.goto('yahoo.com')
.type('#login', login)
.type('#pass', pass)
.click('#submit')
.run();

//Stop and begin a for loop.
for (i = 0; i < myArray.length; i++) {
    //Immediately invoke the previous nightmare and start some more code right where it had left off..
    myNM.wait()
    .type('#field1', myArray[i])
    .click('#checkBtn')
    .wait()
    //Give back some data...add it to the newArray for later.
    .evaluate(func() {
        newArray.push(document.querySelector('#result'));
        return document.querySelector('#result');
    }, func(res) { console.log(res); })
    .run()
}

//Print out the contents of the newArray we just built.
console.dir(newArray);
@Cramertech
Copy link
Author

I actually figured it out on my own, and I am quite proud if I do say so myself.

Please note that I took no actual elements or selectors from Google's site...it's all sort of just made up, but you should get the idea.

So basically all I did here was eliminate the run()'s until the very end after everything. I invoked the myScrape nightmare by name within the for loop on the first nightmare command, and then continued as normal again....then ending that line/command with the semi-colon at the end of the trail, then run() at the end afterward.

var Nightmare = require('nightmare');

var myScrape = new Nightmare()
    .goto('http://google.com/account')
        .type('#login-id', "joeschmoe.somebody.fancy@gmail.com")
        .type('#login-password', "P@ssw0rd")
        .click('#sign-in')
        .wait();

//Start a loop.
for (i = 0; i < 10; i++) {
    myScrape.goto('http://google.com/mail')
    .wait()
    .screenshot("./screens/google"+i+".jpg");
}
myScrape.run();

@shri3k
Copy link

shri3k commented Jul 17, 2015

This helped me. Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants