Reputation: 8548
I'm creating a script using Nightmare
, steps of my scripts necessaries are:
Open the page
Check is logged with cookie
Login is not logged
Do all the rest task
Something like this code:
nightmare
.goto(url)
.cookies.get('cookie_key')
.then(cookie => {
})
;
How to can I make to nightmare login if is not logged, before execute rest of necessary tasks?
Upvotes: 0
Views: 476
Reputation: 2468
@4castle is right: You should be able to put your if
block directly in your .then()
. For example:
nightmare
.goto(url)
.cookies.get('cookie_key')
.then(cookie => {
if(cookie){
//perform your login action
return nightmare
.type('#username', username)
.type('#password', password)
} else {
//if you need to perform other logic
//if you're already logged in, do it here
//otherwise, this `else` can be omitted
}
})
.then(()=>{
return nightmare
.action()
.action()
//etc.
})
For further reading, you might want to have a look at Running Multiple Steps in nightmare-examples
.
Upvotes: 1