diff --git a/backend/features/step_definitions/stepdefs.js b/backend/features/step_definitions/stepdefs.js index 463db3d34..926630f19 100644 --- a/backend/features/step_definitions/stepdefs.js +++ b/backend/features/step_definitions/stepdefs.js @@ -746,6 +746,48 @@ Then( } ); +Then('So the picture {string} has the name {string}', async function checkPicture(picture, name) { + const world = this; + const identifiers = [`//picture[source[contains(@srcset, '${picture}')] or img[contains(@src, '${picture}') or contains(@alt, '${picture}') or @id='${picture}' or contains(@title, '${picture}')]]`, `//img[contains(@src, '${picture}') or contains(@alt, '${picture}') or @id='${picture}' or contains(@title, '${picture}')]`, `${picture}`]; + const promises = []; + for (const idString of identifiers) promises.push(driver.wait(until.elementLocated(By.xpath(idString)), searchTimeout, `Timed out after ${searchTimeout} ms`, 100)); + const domain = (await driver.getCurrentUrl()).split('/').slice(0, 3).join('/'); + let finSrc = ''; + await Promise.any(promises) + .then(async (elem) => { + if (await elem.getTagName() === 'picture') { + const childSourceElems = await elem.findElements(By.xpath('.//source')); + const elementWithSrcset = await childSourceElems.find(async (element) => { + const srcsetValue = await element.getAttribute('srcset'); + return srcsetValue && srcsetValue.includes(name); + }); + finSrc = await elementWithSrcset.getAttribute('srcset'); + } + const primSrc = await elem.getAttribute('src'); + const secSrc = await elem.getAttribute('srcset'); + if (!finSrc && primSrc && primSrc.includes(name)) finSrc = primSrc; + if (!finSrc && secSrc && secSrc.includes(name)) finSrc = secSrc; + finSrc = finSrc.split(' ').filter((substring) => substring.includes(name)); + }) + .catch(async (e) => { + await driver.takeScreenshot().then(async (buffer) => { + world.attach(buffer, 'image/png'); + }); + throw Error(e); + }); + await fetch(domain + finSrc, { method: 'HEAD' }) + .then((response) => { + if (!response.ok) throw Error(`Image ${finSrc} not Found`); + }) + .catch(async (e) => { + await driver.takeScreenshot().then(async (buffer) => { + world.attach(buffer, 'image/png'); + }); + throw Error(`Image availability check: could not reach image source ${domain + finSrc} `, e); + }); + await driver.sleep(100 + currentParameters.waitTime); +}); + // Search if a text isn't in html code Then('So I can\'t see the text: {string}', async function checkIfTextIsMissing(expectedText) { const world = this; diff --git a/backend/src/database/stepTypes.js b/backend/src/database/stepTypes.js index bf5fad4ae..4443e919f 100644 --- a/backend/src/database/stepTypes.js +++ b/backend/src/database/stepTypes.js @@ -456,6 +456,16 @@ function stepDefs() { '', '' ] + }, { + id: 3, + stepType: 'then', + type: 'Check Image', + pre: 'So the picture', + mid: 'has the name ', + values: [ + '', + '' + ] } ]; }