Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save speedupmate/beb69913991bb3752cfd425203ee9449 to your computer and use it in GitHub Desktop.
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
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