Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

javascript - Failed: urls.map is not a function

Following code tests, if the download button links are broken or not. If yes, a test should fail otherwise pass. But the code throws an error Failed: urls.map is not a function. Can someone tell, why's that?

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);
  });
});
question from:https://stackoverflow.com/questions/65897269/failed-urls-map-is-not-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the Protractor docs the .map() function that is available on element.all result returns a Promise that resolves to an array.

So you need to await for it:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...