Skip to content

Instantly share code, notes, and snippets.

@ProxiBlue
Last active January 26, 2022 22:06
Show Gist options
  • Save ProxiBlue/ad1bc6db0f74e4494f92f94d04423b4b to your computer and use it in GitHub Desktop.
Save ProxiBlue/ad1bc6db0f74e4494f92f94d04423b4b to your computer and use it in GitHub Desktop.
cypress random test from sitemap.xml that old site and new site match data
Add the following to plugins/index.js
module.exports = (on, config) => {
on('task', {
log (message) {
console.log(message)
return null
}
})
}
which will allow logging / output for the process
const X2JS = require('x2js')
describe('Compare Random src and dst products', () => {
const destDomain = 'M1 SITE URL'
beforeEach(() => {
cy.request(destDomain + '/sitemap/sitemap.xml')
.its('body')
.then((body) => {
const x2js = new X2JS()
const json = x2js.xml2js(body)
// get all URLs from the sitemap
expect(json.urlset.url).to.be.an('array').and.have.length.gt(0)
var random = Cypress._.sampleSize(json.urlset.url, 1000)
const locations = random.filter(function (obj) {
// old site have some bad url-rewrites, prefixed with -XXXX.html, so skip them
if((new RegExp(`-[0-9]{4}\.html$`)).test(obj.loc)) {
cy.task('log', 'skipped: ' + obj.loc)
return false
}
if (obj.loc.endsWith(".html")) {
return true
}
cy.task('log', 'skipped: ' + obj.loc)
return false
}).map(function (obj) {
return obj.loc;
});
cy.task('log', 'mapped: ' + locations.length)
cy.wrap(locations).as('urls')
})
})
it('Product Name is the same', function () {
for (const fqdn of this.urls) {
const url = fqdn.replace(/https?:\/\/[^\/]+/i, "");
cy.task('log', 'checking: ' + url)
cy.visit(url)
cy.get('.base').invoke('text')
.then(src => {
cy.log(src)
cy.request(destDomain + url).should((response) => {
var el = document.createElement('html');
el.innerHTML = response.body
var dst = el.querySelector("h2").innerText;
cy.expect(dst).to.eq(src)
})
});
}
})
it('Product Price is the same', function () {
console.log(this.urls)
for (const fqdn of this.urls) {
const url = fqdn.replace(/https?:\/\/[^\/]+/i, "");
cy.task('log', 'checking: ' + url)
cy.visit(url)
cy.get('.product-info-main .price-final_price .final-price .price').invoke('text')
.then(src => {
cy.log(src.replace(/^\s|\s$|\B\s|\s\B/g, ''))
cy.request(destDomain + url).should((response) => {
var el = document.createElement('html');
el.innerHTML = response.body
if (el.querySelector(".product-essential .special-price .price")) {
var dst = el.querySelector(".product-essential .special-price .price").innerText;
} else {
var dst = el.querySelector(".col-product-info .price").innerText;
}
cy.log(dst.replace(/^\s|\s$|\B\s|\s\B/g, ''))
cy.expect(dst.replace(/^\s|\s$|\B\s|\s\B/g, '')).to.eq(src.replace(/^\s|\s$|\B\s|\s\B/g, ''))
})
});
}
})
it('Number of related products', function () {
for (const fqdn of this.urls) {
const url = fqdn.replace(/https?:\/\/[^\/]+/i, "");
cy.task('log', 'checking: ' + url)
cy.visit(url)
cy.get("body").then($body => {
if ($body.find('#related-product-list').length > 0) {
cy.get('#related-product-list').children('li')
.then(src => {
cy.log(src.length)
cy.request(destDomain + url).should((response) => {
var el = document.createElement('html');
el.innerHTML = response.body
var dst = el.querySelector("#block-related").children.length;
cy.log(dst)
cy.expect(dst).to.eq(src.length)
})
});
}
});
}
})
it('Title is the same', function () {
for (const fqdn of this.urls) {
const url = fqdn.replace(/https?:\/\/[^\/]+/i, "");
cy.task('log', 'checking: ' + url)
cy.visit(url)
cy.request(destDomain + url).should((response) => {
var el = document.createElement('html');
el.innerHTML = response.body
var dst = el.querySelector("title").innerText;
cy.log(dst)
cy.title().should('eq', dst)
})
}
})
// it.only('Product Breadcrumbs is the same', function () {
// for (const fqdn of this.urls) {
// const url = fqdn.replace(/https?:\/\/[^\/]+/i, "");
// cy.visit(url)
// cy.get('.breadcrumbs').invoke('text')
// .then(src => {
// cy.log(src.replace(/^\s|\s$|\B\s|\s\B|\//g, ''))
// cy.request(destDomain + url).should((response) => {
// var el = document.createElement('html');
// el.innerHTML = response.body
// var dst = el.querySelector(".breadcrumb").innerText;
// cy.log(dst.replace(/^\s|\s$|\B\s|\s\B|\//g, ''))
// cy.expect(dst.replace(/^\s|\s$|\B\s|\s\B|\//g, '')).to.eq(src.replace(/^\s|\s$|\B\s|\s\B|\//g, ''))
// })
// });
// }
// })
it('Product Canonical is the same', function () {
for (const fqdn of this.urls) {
const url = fqdn.replace(/https?:\/\/[^\/]+/i, "");
cy.task('log', 'checking: ' + url)
cy.visit(url)
cy.get('link[rel="canonical"]').invoke('attr', 'href')
.then(src => {
cy.expect(src).to.contain(url)
cy.request(destDomain + url).should((response) => {
var el = document.createElement('html');
el.innerHTML = response.body
var dst = el.querySelector("link[rel='canonical']").getAttribute("href");
cy.log(dst)
cy.expect(dst).to.contain(url)
})
});
}
});
})
@ProxiBlue
Copy link
Author

note, I had to add some delays in the real world run before each cy.visit(url)

cy.wait(20000)

as the live M1 site got dragged down due to load / frequency of too many rapid requests!

I did not add that to the file above, as it may not be needed in all instances, and slows down the process a lot. I just let it run overnight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment