Lai32290
Lai32290

Reputation: 8548

How to do something depending the condition in Nightmare?

I'm creating a script using Nightmare, steps of my scripts necessaries are:

  1. Open the page

  2. Check is logged with cookie

  3. Login is not logged

  4. 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

Answers (1)

Ross
Ross

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

Related Questions