問題描述
我正在尋找一種在異步獲取數據后定義 Mocha 測試的解決方案.
I'm looking for a solution to define Mocha tests after getting data asynchronously.
目前,我使用 gulp-webdriver 通過 Selenium 獲取 HTML 內容.我想測試某些 HTML 標簽結構.
For now, I use gulp-webdriver to getting HTML content with Selenium. And I want tests certain HTML tags structure.
例如,我想從 HTML 頁面中獲取所有按鈕結構.
For example, I want to get all buttons structure from an HTML page.
1° 在 Mocha Before() 中,我得到按鈕:
1° In Mocha Before(), I get buttons :
var buttons = browser.url("url").getHTML("button");
2° 然后,我想在單獨的 it
中測試每個按鈕:
2° And after that, I want tests each button in a separate it
:
buttons.forEach(function(button) { it() });
找到的唯一解決方案是在使用 data_driven 啟動 Mocha 測試之前使用 Gulp 加載 HTML 和提取按鈕或 leche.withData 插件.
The only solution found is loading HTML and extract buttons with Gulp before launch Mocha test with data_driven or leche.withData plugin.
您知道直接在 Mocha 測試定義中的另一種解決方案嗎?
Do you know another solution directly in Mocha test definition?
提前致謝,
推薦答案
似乎不可能用 mocha 動態創建 it() 測試.
It doesn't seem possible to dynamically create it() tests with mocha.
我終于像這樣組織我的測試了:
I finally organise my test like this :
it('Check if all tag have attribute', function() {
var errors = [];
elements.forEach(function(element, index, array) {
var $ = cheerio.load(element);
var tag = $(tagName);
if (tag.length) {
if (!tag.attr(tagAttr)) errors.push(element);
}
});
expect(errors).to.be.empty;
}
}
這篇關于從 webdriver.io 獲取數據后動態構建 Mocha 測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!