Forked from ProxiBlue/gist:ad1bc6db0f74e4494f92f94d04423b4b
Created
January 25, 2022 09:07
-
-
Save speedupmate/beb69913991bb3752cfd425203ee9449 to your computer and use it in GitHub Desktop.
cypress random test from sitemap.xml that old site and new site match data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 10) | |
const locations = random.filter(function (obj) { | |
if (obj.loc.endsWith(".html")) { | |
return true | |
} | |
return false | |
}).map(function (obj) { | |
return obj.loc; | |
}); | |
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.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.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.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.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.log(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) | |
}) | |
}); | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment