2016-08-02 60 views
0

我使用創建一個腳本,我的腳本的步驟必需品:如何根據夢魘中的情況做些什麼?

  1. 打開頁面

  2. 檢查記錄與cookie不會被記錄

  3. 登錄

  4. 完成所有其他任務

事情是這樣的代碼:

nightmare 
    .goto(url) 
    .cookies.get('cookie_key') 
    .then(cookie => { 

    }) 
    ; 

如何我可以做出噩夢登錄,如果沒有登錄,之前執行的必要任務休息嗎?

+1

你不能只在你的'then()'中放一個'if'語句嗎? – 4castle

回答

1

@ 4castle是對的:您應該能夠將您的if塊直接放入您的.then()。例如:

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. 
    }) 

對於進一步的閱讀,你可能想在nightmare-examples看看Running Multiple Steps