失败:urls.map 不是函数
Failed: urls.map is not a function
以下代码测试,下载按钮链接是否损坏。如果是,测试应该失败,否则通过。但是代码抛出错误 Failed: urls.map is not a function
。谁能告诉我这是为什么?
it('test if templates are downloadable', async () => {
const links = element.all(by.xpath('//button//img[@src="url_here"]'));
const urls = links.map(link => link.getAttribute('src'));
const requests = urls.map(url => fetch(url));
const responses = await Promise.all(requests);
const statusCodes = responses.map(response => response.status);
statusCodes.forEach(statusCode => {
expect(statusCode).toBeLessThan(400);
});
});
根据 Protractor docs the .map()
function that is available on element.all
result returns 解析为数组的 Promise。
所以你需要await
:
it('test if templates are downloadable', async () => {
const links = element.all(by.xpath('//button//img[@src="url_here"]'));
const urls = await links.map(link => link.getAttribute('src')); // await here
const requests = await urls.map(url => fetch(url)); // and await here
const responses = await Promise.all(requests);
const statusCodes = responses.map(response => response.status);
statusCodes.forEach(statusCode => {
expect(statusCode).toBeLessThan(400);
});
});
以下代码测试,下载按钮链接是否损坏。如果是,测试应该失败,否则通过。但是代码抛出错误 Failed: urls.map is not a function
。谁能告诉我这是为什么?
it('test if templates are downloadable', async () => {
const links = element.all(by.xpath('//button//img[@src="url_here"]'));
const urls = links.map(link => link.getAttribute('src'));
const requests = urls.map(url => fetch(url));
const responses = await Promise.all(requests);
const statusCodes = responses.map(response => response.status);
statusCodes.forEach(statusCode => {
expect(statusCode).toBeLessThan(400);
});
});
根据 Protractor docs the .map()
function that is available on element.all
result returns 解析为数组的 Promise。
所以你需要await
:
it('test if templates are downloadable', async () => {
const links = element.all(by.xpath('//button//img[@src="url_here"]'));
const urls = await links.map(link => link.getAttribute('src')); // await here
const requests = await urls.map(url => fetch(url)); // and await here
const responses = await Promise.all(requests);
const statusCodes = responses.map(response => response.status);
statusCodes.forEach(statusCode => {
expect(statusCode).toBeLessThan(400);
});
});