我们正在使用Protractor进行端到端UI测试,并使用Jasmine作为BDD框架。我们需要UI的文本来验证来自REST API的数据,我们正在使用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的正确方法是什么?(Chai + Mocha + Chakram + Protractor)或其他什么?
发布于 2019-01-20 05:15:59
可能会吧。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');
});
});更好的方法。使用异步/等待
在您的量角器配置中,您需要添加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);
});
});https://stackoverflow.com/questions/54249463
复制相似问题