[英]Correct way to do async calls to get test data from REST end points inside Protractor?
我們正在使用量角器並將Jasmine作為BDD框架進行端到端UI測試。 我們需要針對REST API中的數據驗證UI文本,為此我們正在使用Axios! 這是正確的方法嗎? 示例代碼如下:
import axios from "axios";
describe("Some test for ", () => {
beforeEach(function(done) {
axios
.get(
"******************"
)
.then(response => {
data_file = response.data;
done();
});
});
it("some spec ", done => {
expect($('#someId').getText()).toBe(data_file.someData);
done();
});
});
我們可以在量角器的Jasmine中使用Chakram代替Axios來獲取數據嗎?
如果上述方法錯誤,那么針對REST端點數據測試UI的正確方法是什么? (柴+摩卡+查克拉姆+量角器)還是其他?
它可能是。 done()
回調告訴Jasmine您正在執行異步任務; 但是,您應該小心捕捉錯誤。
import axios from "axios";
describe("Some test for ", () => {
beforeEach(function(done) {
axios
.get(
"******************"
)
.then(response => {
data_file = response.data;
done();
})
// if the above fails to .get, then we should catch here and fail with a message
.catch(error => {
done.fail('axios.get failed to execute');
});
});
在量角器配置中,您需要添加SELENIUM_PROMISE_MANAGER: false
才能啟用異步/等待。 現在,這將要求您等待所有承諾。
import axios from "axios";
describe("Some test for ", () => {
beforeEach(async () => {
try {
const data_file = await axios.get("******************").data;
} catch (e) {
console.error('axios.get failed to execute');
throw e; // throwing errors should fail the spec.
}
});
it("some spec ", async () => {
// .getText returns a Promise<string> so you'll need to await it
// to get the string value.
expect(await $('#someId').getText()).toBe(data_file.someData);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.