Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for img data and wildcard protocol. #72

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-ncatestify-plugin",
"version": "2.0.34",
"version": "2.0.35",
"description": "NCA TESTIFY commands and tests",
"main": "src",
"types": "src",
Expand Down
83 changes: 41 additions & 42 deletions src/commands/tt-validate-all-images-response-status-ok.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
const excludedUrlPrefixes = ['data:'];
const isExcludedUrl = (url) => {
return excludedUrlPrefixes.some((prefix) => url.startsWith(prefix));
};
const normalizeUrl = (url) => {
return url.startsWith('//') ? `https:${url}` : url;
};
export const ttValidateAllImagesResponseStatusOk = () => {
cy.log('ttValidateAllImagesResponseStatusOk - NCA TESTIFY');
const srcArray = [];
const srcSetArray = [];
const srcSet = new Set();
const srcSetArray = new Set();
cy.get('img').should('have.length.gt', 0);
cy.get('img')
.each((img, i) => {
if (img.attr('src') !== undefined) {
cy.wrap(img)
.invoke('attr', 'src')
.then((src) => {
if (typeof src === 'string') {
srcArray.push(src);
}
else {
cy.log('img src is not a string');
}
});
.each(($img) => {
var _a;
const img = $img[0];
const src = img.getAttribute('src');
const srcset = img.getAttribute('srcset');
if (src !== null) {
const normalizedSrc = normalizeUrl(src);
if (!isExcludedUrl(normalizedSrc)) {
srcSet.add(normalizedSrc);
}
}
if (img.attr('srcset') !== undefined) {
cy.wrap(img)
.invoke('attr', 'srcset')
.then((srcset) => {
if (typeof srcset === 'string') {
const srcsetUrls = srcset
.split(',')
.map((srcsetItem) => srcsetItem.trim().split(' ')[0]);
srcSetArray.push(...srcsetUrls);
}
else {
cy.log('img srcset is not a string');
if (srcset !== null) {
const srcsetUrls = srcset
.split(',')
.map((srcsetItem) => srcsetItem.trim().split(' ')[0])
.map(normalizeUrl);
srcsetUrls.forEach((url) => {
if (!isExcludedUrl(url)) {
srcSetArray.add(url);
}
});
}
if (img.attr('srcset') === undefined && img.attr('src') === undefined) {
cy.wrap(img)
.invoke('attr', 'alt')
.then((imgAlt) => {
cy.log(`Image ${imgAlt} has neither src nor srcset attribute`).then(() => {
expect(img).to.have.attr('src');
});
if (src === null && srcset === null) {
const alt = (_a = img.getAttribute('alt')) !== null && _a !== void 0 ? _a : '';
cy.log(`Image ${alt} has neither src nor srcset attribute`).then(() => {
expect(img).to.have.attr('src');
});
}
})
.then(() => {
const promises = [];
srcArray.forEach((url) => {
promises.push(cy
srcSet.forEach((url) => {
const promise = cy
.request('HEAD', url)
.its('status')
.should('eq', 200)
.then(() => {
cy.log('Validated image: ' + url);
}));
cy.log(`Validated image: ${url}`);
});
promises.push(promise);
});
srcSetArray.forEach((url) => {
promises.push(cy
const promise = cy
.request('HEAD', url)
.its('status')
.should('eq', 200)
.then(() => {
cy.log('Validated image in srcset: ' + url);
}));
cy.log(`Validated image in srcset: ${url}`);
});
promises.push(promise);
});
return Cypress.Promise.all(promises);
})
.then(() => {
srcArray.forEach((entry) => {
srcSet.forEach((entry) => {
cy.get(`[src="${entry}"]`).should('exist');
});
srcSetArray.forEach((entry) => {
Expand Down
135 changes: 67 additions & 68 deletions src/commands/tt-validate-all-images-response-status-ok.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,84 @@
const excludedUrlPrefixes = ['data:'];

const isExcludedUrl = (url: string): boolean => {
return excludedUrlPrefixes.some((prefix) => url.startsWith(prefix));
};

const normalizeUrl = (url: string): string => {
return url.startsWith('//') ? `https:${url}` : url;
};

export const ttValidateAllImagesResponseStatusOk = () => {
cy.log('ttValidateAllImagesResponseStatusOk - NCA TESTIFY')
const srcArray: string[] = []
const srcSetArray: string[] = []
const srcSet = new Set<string>();
const srcSetArray = new Set<string>();

cy.get('img').should('have.length.gt', 0)
cy.get('img').should('have.length.gt', 0);
cy.get('img')
.each((img, i) => {
if (img.attr('src') !== undefined) {
cy.wrap(img)
.invoke('attr', 'src')
.then((src) => {
if (typeof src === 'string') {
srcArray.push(src)
} else {
cy.log('img src is not a string')
}
})
.each(($img) => {
const img = $img[0] as HTMLImageElement;
const src = img.getAttribute('src');
const srcset = img.getAttribute('srcset');

if (src !== null) {
const normalizedSrc = normalizeUrl(src);
if (!isExcludedUrl(normalizedSrc)) {
srcSet.add(normalizedSrc);
}
}

if (img.attr('srcset') !== undefined) {
cy.wrap(img)
.invoke('attr', 'srcset')
.then((srcset) => {
if (typeof srcset === 'string') {
const srcsetUrls = srcset
.split(',')
.map((srcsetItem) => srcsetItem.trim().split(' ')[0])
srcSetArray.push(...srcsetUrls)
} else {
cy.log('img srcset is not a string')
}
})
if (srcset !== null) {
const srcsetUrls = srcset
.split(',')
.map((srcsetItem) => srcsetItem.trim().split(' ')[0])
.map(normalizeUrl);
srcsetUrls.forEach((url) => {
if (!isExcludedUrl(url)) {
srcSetArray.add(url);
}
});
}

if (img.attr('srcset') === undefined && img.attr('src') === undefined) {
cy.wrap(img)
.invoke('attr', 'alt')
.then((imgAlt) => {
cy.log(`Image ${imgAlt} has neither src nor srcset attribute`).then(
() => {
expect(img).to.have.attr('src')
},
)
})
if (src === null && srcset === null) {
const alt = img.getAttribute('alt') ?? '';
cy.log(`Image ${alt} has neither src nor srcset attribute`).then(() => {
expect(img).to.have.attr('src');
});
}
})
.then(() => {
const promises = []
const promises: Cypress.Chainable[] = [];

srcArray.forEach((url) => {
promises.push(
cy
.request('HEAD', url)
.its('status')
.should('eq', 200)
.then(() => {
cy.log('Validated image: ' + url)
}),
)
})
srcSet.forEach((url) => {
const promise = cy
.request('HEAD', url)
.its('status')
.should('eq', 200)
.then(() => {
cy.log(`Validated image: ${url}`);
});
promises.push(promise);
});

srcSetArray.forEach((url) => {
promises.push(
cy
.request('HEAD', url)
.its('status')
.should('eq', 200)
.then(() => {
cy.log('Validated image in srcset: ' + url)
}),
)
})
const promise = cy
.request('HEAD', url)
.its('status')
.should('eq', 200)
.then(() => {
cy.log(`Validated image in srcset: ${url}`);
});
promises.push(promise);
});

return Cypress.Promise.all(promises)
return Cypress.Promise.all(promises);
})
.then(() => {
srcArray.forEach((entry) => {
cy.get(`[src="${entry}"]`).should('exist')
})
srcSet.forEach((entry) => {
cy.get(`[src="${entry}"]`).should('exist');
});
srcSetArray.forEach((entry) => {
cy.get(`[srcset*="${entry}"]`).should('exist')
})
})
}
cy.get(`[srcset*="${entry}"]`).should('exist');
});
});
};

Loading