2015-03-02 19 views
1

我正在測試一個網頁,用戶可以通過textinput發送消息給另一個網頁。然後在服務器上發送POST請求,並將消息轉儲到var/mail/new文件夾中的磁盤上。如何等待量角器中的後端?

在使用量角器自動發送頁面中的消息後,我打電話給browser.waitForAngular()browser.driver.sleep(4000),讓後端在磁盤上寫郵件。

這些調用後,電子郵件的檢查失敗。在Unix shell中查看時,我可以確認電子郵件已發送,並且在Jasmine中使用it標記的下一個測試確認了電子郵件的存在。

爲什麼browser.driver.sleep(4000)無法等待後端進行?我怎樣才能糾正下面的代碼?

it("is possible to send a message", function() { 
    shared.loginContributor(); 

    var mailsBeforeMessaging = 
     fs.readdirSync(browser.params.mail.queue_path + "/new"); 
    console.log('mailsBeforeMessaging'); 
    console.log(mailsBeforeMessaging.length); 
    console.log(fs.lstatSync(browser.params.mail.queue_path + "/new")); 

    var usersListing = new UserPages.UsersListing().get(); 
    var annotatorPage = usersListing.getUserPage("annotator"); 

    annotatorPage.sendMessage("title5", "content64"); 

    exec("/tmp/check.sh"); 

    // we expect the message widget to disappear 
    var button = element(by.css(".user-profile-info-button")); 
    console.log('waiting'); 
    browser.wait(EC.elementToBeClickable(button), 5000); 
    console.log('waiting is finished'); 
    expect(EC.elementToBeClickable(button)).toBeTruthy(); 

    // wait for mail to be dumped on the disk? 
    browser.waitForAngular(); 
    browser.driver.sleep(4000); 

    exec("/tmp/check.sh"); 

    var mailsAfterMessaging = 
     fs.readdirSync(browser.params.mail.queue_path + "/new"); 
    console.log('mailsAfterMessaging'); 
    // ERROR: here the number of emails is NOT incremented 
    console.log(mailsAfterMessaging.length); 
    console.log(fs.lstatSync(browser.params.mail.queue_path + "/new")); 
}); 

it("xyz", function() { 

    console.log(fs.lstatSync(browser.params.mail.queue_path + "/new")); 
    // here the number of emails is incremented 
    var mailsAfterMessaging = 
     fs.readdirSync(browser.params.mail.queue_path + "/new"); 
    console.log('mailsAfterMessaging'); 
    console.log(mailsAfterMessaging.length); 
}); 

回答

3

大多數量角器功能不什麼。他們排隊等待後來,並承諾做到這一點。在it塊安排了一大堆事情之後,他們實際上開始發生(通過他們在ControlFlow中註冊的許諾)。

但是,您的支票全部立即執行。所以,在任何量角器調用完成任何事情之前,它們正在發生。

使用then可使測試中的等待和依賴關係顯式化。就像這樣:

annotatorPage.sendMessage("title5", "content64").then(function() { 
    exec("/tmp/check.sh"); 
}); 

或:

browser.wait(EC.elementToBeClickable(button), 5000).then(function() { 
    console.log('wait-for-clickable has completed'); // B 
}); 
console.log('wait-for-clickable has been scheduled'); // A 

Protractor Control Flow文檔和Webdriver JS API doc

它不是你。這是一個瘋狂的API學習,因爲它不像熟悉普通同步編程的人所期望的那樣完全不起作用。

+0

謝謝你的友好和詳細的答案。在執行測試之前是否有任何方法清空隊列並避免調用「then」? – z1naOK9nu8iY5A 2015-03-03 09:00:31

+0

注意未來的我:可以通過browser.controlFlow()和'flow.execute'控制流程。 – z1naOK9nu8iY5A 2015-03-03 14:21:21

相關問題