2014-09-04 24 views
3

我希望有條件地執行一些命令/斷言取決於以前的斷言是否成功。功能測試條件與實習生/ Leadfoot

我似乎無法找到一種方法嵌套then調用或使用leadfoot的鏈接語法有條件地執行的東西。

這是我測試(在每個功能的端部的返回是由於這是編譯的CoffeeScript):

tdd.test(tableUrl, function() { 
    this.timeout = 60000; 
    return this.remote.get(require.toUrl("tests/pages/" + tableUrl)) 
    .setPageLoadTimeout(60000) 
    .setFindTimeout(20000) 
    .findByCssSelector('.dataTables_wrapper table.dataTable') 
    .then(null, function(err) { 
     return assert.fail('', '', 'Table should exist'); 
    }).then(pollUntil(function() { 
     var table; 
     table = document.querySelector('.dataTables_wrapper table.dataTable'); 
     if (table.querySelectorAll('tbody td').length > 1) { 
     return true; 
     } else { 
     return null; 
     } 
    }, null, 20000, 500)).then(function() { 
     console.log('Assertion succeeded: Table should have data'); 
     return assert.ok(true, 'Table should have data'); 
    }, function(err) { 
     return assert.fail('', '', 'Table should have data'); 
    }); 
    }); 

當表不存在,既「表應存在」和「表應有數據「的斷言應該被報告爲失敗。但是,只有最後一個斷言是失敗的。當我註釋掉'表應該有數據'錯誤回調時,'表應該存在'斷言被報告爲失敗正確。

我希望做的是,如果一個table存在(通過findByCssSelector)

  1. 測試。

    a)如果存在,則報告它存在,並測試它是否有多個td元素。這目前通過pollUntil完成,以確保命令完成/承諾一旦找到多個td而不是等待整個隱式等待時間,就立即解決。

    b)如果它不存在,報告它不存在。

我似乎無法找到執行第二的錯誤回調的方式「表應該有數據」調查,如果第一findByCssSelector失敗,由於缺乏條件句,只有最後一個斷言在測試中報告失敗。

回答

3

所以條件分支可以then調用發生,this answer地址如何使用實習生做條件分支。

我遇到的問題是由於function returning a callback function that returns a promise rather than a promise directlypollUntil函數的行爲與正常leadfoot Command方法的行爲不同。

這可以通過在一個匿名函數包裹pollUntil,立即調用使用call方法pollUntil函數,傳遞在當前0​​值由於then設定回調的命令對象的上下文,然後最終被規避鏈接另一個命令。

這就是上面的代碼變成使用正確的條件分支pollUntil

tdd.test(tableUrl, function() { 
    this.timeout = 60000; 
    return this.remote.get(require.toUrl("tests/pages/" + tableUrl)) 
    .setPageLoadTimeout(60000) 
    .setFindTimeout(5000) 
    .findByCssSelector('.dataTables_wrapper table.dataTable') 
    .then((function() { 
    return pollUntil(function() { 
     var table; 
     table = document.querySelector('.dataTables_wrapper table.dataTable'); 
     if (table.querySelectorAll('tbody td').length > 1) { 
     return true; 
     } else { 
     return null; 
     } 
    }, null, 20000, 500).call(this).then(function() { 
     console.log('Assertion succeeded: Table should have data'); 
     return assert.ok(true, 'Table should have data'); 
    }, function(err) { 
     return assert.fail('', '', 'Table should have data'); 
    }); 
    }), function(err) { 
    return assert.fail('', '', "Table should exist. Error: " + err.name); 
    }); 
}); 
+0

它可能與registerSuite而不是tdd? – 2014-10-25 09:31:08

相關問題