2016-08-11 50 views
-1

獲得一些練習運行測試與摩卡柴和噩夢。一切似乎都有效,直到我進入評估區塊。我的噩夢測試沒有進入我的評估陳述

var Nightmare = require('nightmare'), 
    should = require('chai').should() 

describe('Frontend Masters', function() { 
    this.timeout(20000); 

    it('should show form when loaded', function(done) { 
    var nightmare = new Nightmare({show: true}) 
    nightmare 
     .goto('https://frontendmasters.com/') 
     .wait('a[href*="https://frontendmasters.com/login/"]') 
     .click('a[href*="https://frontendmasters.com/login/"]') 
     .wait('#rcp_login_form') 
     .evaluate(function() { 
     return window.document.title; 
     }, function(result) { 
     result.should.equal('Login to Frontend Masters'); 
     done(); 
     }) 
     .run(function(){ 
     console.log('done') 
     }); 
    }); 
}); 

我已經拋出控制檯日誌,它永遠不會進入評估。我試過將幾個選擇器傳入我的wait()函數,但它似乎沒有效果。只有我收到的錯誤是我的超時已被超過。但不管多久我設置它

+0

它似乎並不有什麼關係'evaluate'。如果「等待」超時失敗,則以前的點擊不起作用或網站被破壞。順便說一下,這兩個「等待」電話中的哪一個失敗了? –

+1

當你用環境變量DEBUG =「nightmare *」運行時,你會得到任何有用的輸出嗎? – yoz

回答

1

你使用的是什麼版本的夢魘?

.evaluate()的簽名已更改,我認爲這可能是您問題的根源。你傳遞的第二個函數 - 曾經是處理評估結果的函數 - 實際上已經作爲第一個參數傳遞給了參數。換句話說,第二個參數永遠不會運行,done()永遠不會被調用,並且您的測試將超時。

也值得一提:.run() is not directly supported。改爲使用.then()

最後,讓我們修改源代碼以反映上述讓你開始:

var Nightmare = require('nightmare'), 
    should = require('chai') 
    .should() 

describe('Frontend Masters', function() { 
    this.timeout(20000); 

    it('should show form when loaded', function(done) { 
    var nightmare = new Nightmare({ 
     show: true 
    }) 
    nightmare 
     .goto('https://frontendmasters.com/') 
     .wait('a[href*="https://frontendmasters.com/login/"]') 
     .click('a[href*="https://frontendmasters.com/login/"]') 
     .wait('#rcp_login_form') 
     .evaluate(function() { 
     return window.document.title; 
     }) 
     .then(function(result) { 
     result.should.equal('Login to Frontend Masters'); 
     console.log('done') 
     done(); 
     }) 
    }); 
});