test: percy snapshot workaround (#1111)
* Try workaround for percy snapshot * Return our modified document * test: some dark magic to make it possible to take screenshots of WebGL canvases * Apply transformation directly to test DOM * investigate: window:before:load * Register flag in correct place; add experimental-webgl check * Remaining canvas fixes * Remove comment
This commit is contained in:
parent
f0519920fa
commit
70532a6cc6
@ -21,7 +21,6 @@ describe('OHIF Microscopy Extension', () => {
|
|||||||
.should('be.eq', 1);
|
.should('be.eq', 1);
|
||||||
|
|
||||||
cy.wait(3000); //Waiting for image to render before taking the snapshot
|
cy.wait(3000); //Waiting for image to render before taking the snapshot
|
||||||
cy.screenshot();
|
|
||||||
cy.percyCanvasSnapshot('Microscopy Extension');
|
cy.percyCanvasSnapshot('Microscopy Extension');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -20,7 +20,8 @@ describe('OHIF PDF Extension', () => {
|
|||||||
.its('length')
|
.its('length')
|
||||||
.should('be.eq', 1);
|
.should('be.eq', 1);
|
||||||
|
|
||||||
cy.screenshot();
|
// This won't work unless we switch to an extension that renders using `canvas`
|
||||||
|
// Currently, we rely on the browser's built-in implementation
|
||||||
cy.percyCanvasSnapshot('PDF Extension');
|
cy.percyCanvasSnapshot('PDF Extension');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -22,14 +22,20 @@ describe('OHIF VTK Extension', () => {
|
|||||||
btn.click();
|
btn.click();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
//wait VTK toolbar and images to be loaded
|
|
||||||
cy.wait(3000);
|
|
||||||
cy.initVTKToolsAliases();
|
cy.initVTKToolsAliases();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('checks if VTK buttons are displayed on the toolbar', () => {
|
it('checks if VTK buttons are displayed on the toolbar', () => {
|
||||||
cy.screenshot();
|
// Wait for start reformatting
|
||||||
cy.percyCanvasSnapshot('VTK Extension');
|
cy.get('[data-cy="viewprt-grid"]', { timeout: 10000 }).should($grid => {
|
||||||
|
expect($grid).to.contain.text('Reform');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for finish reformatting
|
||||||
|
cy.get('[data-cy="viewprt-grid"]', { timeout: 30000 }).should($grid => {
|
||||||
|
expect($grid).not.to.contain.text('Reform');
|
||||||
|
});
|
||||||
|
|
||||||
cy.get('@crosshairsBtn')
|
cy.get('@crosshairsBtn')
|
||||||
.should('be.visible')
|
.should('be.visible')
|
||||||
@ -50,5 +56,8 @@ describe('OHIF VTK Extension', () => {
|
|||||||
cy.get('@layoutBtn')
|
cy.get('@layoutBtn')
|
||||||
.should('be.visible')
|
.should('be.visible')
|
||||||
.contains('Layout');
|
.contains('Layout');
|
||||||
|
|
||||||
|
cy.wait(3000);
|
||||||
|
cy.percyCanvasSnapshot('VTK Extension');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -314,28 +314,53 @@ Cypress.Commands.add('isInViewport', element => {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
Cypress.Commands.add('percyCanvasSnapshot', (name, options = {}) => {
|
Cypress.Commands.add('percyCanvasSnapshot', (name, options = {}) => {
|
||||||
function convertCanvas(documentClone) {
|
cy.document().then(doc => {
|
||||||
documentClone
|
convertCanvas(doc);
|
||||||
.querySelectorAll('canvas')
|
});
|
||||||
.forEach(selector => canvasToImage(selector));
|
|
||||||
|
|
||||||
return documentClone;
|
// `domTransformation` does not appear to be working
|
||||||
}
|
// But modifying our immediate DOM does.
|
||||||
|
cy.percySnapshot(name, { ...options }); //, domTransformation: convertCanvas });
|
||||||
|
|
||||||
function canvasToImage(selectorOrEl) {
|
cy.document().then(doc => {
|
||||||
let canvas =
|
unconvertCanvas(doc);
|
||||||
typeof selectorOrEl === 'object'
|
});
|
||||||
? selectorOrEl
|
|
||||||
: document.querySelector(selectorOrEl);
|
|
||||||
let image = document.createElement('img');
|
|
||||||
let canvasImageBase64 = canvas.toDataURL();
|
|
||||||
|
|
||||||
image.src = canvasImageBase64;
|
|
||||||
image.style = 'max-width: 100%';
|
|
||||||
canvas.setAttribute('data-percy-modified', true);
|
|
||||||
canvas.parentElement.appendChild(image);
|
|
||||||
canvas.style = 'display: none';
|
|
||||||
}
|
|
||||||
|
|
||||||
cy.percySnapshot(name, { ...options, domTransformation: convertCanvas });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function convertCanvas(documentClone) {
|
||||||
|
documentClone
|
||||||
|
.querySelectorAll('canvas')
|
||||||
|
.forEach(selector => canvasToImage(selector));
|
||||||
|
|
||||||
|
return documentClone;
|
||||||
|
}
|
||||||
|
|
||||||
|
function unconvertCanvas(documentClone) {
|
||||||
|
// Remove previously generated images
|
||||||
|
documentClone
|
||||||
|
.querySelectorAll('[data-percy-image]')
|
||||||
|
.forEach(selector => selector.remove());
|
||||||
|
// Restore canvas visibility
|
||||||
|
documentClone.querySelectorAll('[data-percy-canvas]').forEach(selector => {
|
||||||
|
selector.removeAttribute('data-percy-canvas');
|
||||||
|
selector.style = '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function canvasToImage(selectorOrEl) {
|
||||||
|
let canvas =
|
||||||
|
typeof selectorOrEl === 'object'
|
||||||
|
? selectorOrEl
|
||||||
|
: document.querySelector(selectorOrEl);
|
||||||
|
let image = document.createElement('img');
|
||||||
|
let canvasImageBase64 = canvas.toDataURL('image/png');
|
||||||
|
|
||||||
|
// Show Image
|
||||||
|
image.src = canvasImageBase64;
|
||||||
|
image.style = 'width: 100%';
|
||||||
|
image.setAttribute('data-percy-image', true);
|
||||||
|
// Hide Canvas
|
||||||
|
canvas.setAttribute('data-percy-canvas', true);
|
||||||
|
canvas.parentElement.appendChild(image);
|
||||||
|
canvas.style = 'display: none';
|
||||||
|
}
|
||||||
@ -13,8 +13,23 @@
|
|||||||
// https://on.cypress.io/configuration
|
// https://on.cypress.io/configuration
|
||||||
// ***********************************************************
|
// ***********************************************************
|
||||||
|
|
||||||
// Import commands.js using ES2015 syntax:
|
|
||||||
import './commands';
|
import './commands';
|
||||||
|
|
||||||
// Alternatively you can use CommonJS syntax:
|
Cypress.on('window:before:load', window => {
|
||||||
// require('./commands')
|
// Override our `getContext` function so all contexts that are webGl are
|
||||||
|
// Created with `preserveDrawingBuffer = true`; this is required for percy
|
||||||
|
// Snapshot
|
||||||
|
window.HTMLCanvasElement.prototype.getContext = (function(oldGetContextFn) {
|
||||||
|
return function(type, attrs) {
|
||||||
|
attrs = attrs || {};
|
||||||
|
if (
|
||||||
|
type === 'webgl' ||
|
||||||
|
type === 'webgl2' ||
|
||||||
|
type === 'experimental-webgl'
|
||||||
|
) {
|
||||||
|
attrs.preserveDrawingBuffer = true;
|
||||||
|
}
|
||||||
|
return oldGetContextFn.apply(this, [type, attrs]);
|
||||||
|
};
|
||||||
|
})(HTMLCanvasElement.prototype.getContext);
|
||||||
|
});
|
||||||
|
|||||||
@ -91,6 +91,7 @@ const ViewportGrid = function(props) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
data-cy="viewprt-grid"
|
||||||
style={{
|
style={{
|
||||||
display: 'grid',
|
display: 'grid',
|
||||||
gridTemplateRows: `repeat(${numRows}, ${rowSize}%)`,
|
gridTemplateRows: `repeat(${numRows}, ${rowSize}%)`,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user