gpt4 book ai didi

javascript - 执行异步调用以从 Protractor 内的 REST 端点获取测试数据的正确方法?

转载 作者:行者123 更新时间:2023-12-03 00:22:31 24 4
gpt4 key购买 nike

我们正在使用 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();
});

});

我们可以在 Protractor 中使用 Chakram 代替 Jasmine 中的 Axios 来获取数据吗?

如果上述方法是错误的,那么针对 REST 端点的数据测试 UI 的正确方法是什么? (柴 + Mocha + 环轮 + Protractor )还是其他什么?

最佳答案

可能是这样。 done() 回调告诉 Jasmine 您正在执行异步任务;但是,您应该小心地发现错误。

添加done.fail

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');
});
});

更好的方法。使用异步/等待

在 Protractor 配置中,您需要添加 SELENIUM_PROMISE_MANAGER: false启用异步/等待。现在,这将要求您等待所有 promise 。

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);
});
});

关于javascript - 执行异步调用以从 Protractor 内的 REST 端点获取测试数据的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com