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

Fix headed Chrome screenshot #93

Merged
merged 1 commit into from
Jul 26, 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
1 change: 1 addition & 0 deletions src/browser/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function focusElem(elem) {

export async function hoverAt(x, y) {
await sendMouse({ type: 'move', position: [x, y] });
if (window.d2lTest) window.d2lTest.hovering = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would having the vdiff/pause stuff just add a listener on the window or body for mouseover also work? If so, it might be nice for that code to live over there where it belongs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked offline. Sticking with this for now to avoid overusing inlineStyles, which is yucky and slow.

}

export async function hoverElem(elem) {
Expand Down
45 changes: 45 additions & 0 deletions src/browser/vdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ mocha.setup({
/* eslint-enable */

async function ScreenshotAndCompare(opts) {

if (window.d2lTest) {
document.documentElement.classList.add('screenshot');
inlineStyles(this.elem);
}

const name = this.test.fullTitle();
const rect = this.elem.getBoundingClientRect();
const slowDuration = this.test.slow();
Expand All @@ -25,7 +31,46 @@ async function ScreenshotAndCompare(opts) {
this.test.timeout(0);
result = await executeServerCommand('brightspace-visual-diff-compare-resize', { name });
}

if (window.d2lTest) document.documentElement.classList.remove('screenshot');

if (!result.pass) {
expect.fail(result.message);
}
}

const disallowedProps = ['width', 'inline-size'];
let count = 0;

function inlineStyles(elem) {
// headed chrome takes screenshots by first moving the element and locking it down,
// which breaks the hover state. So, copy current styles inline before screenshot
if (window.d2lTest.hovering && window.chrome) {
count += 1;
document.documentElement.classList.add('screenshot');
elem.classList.add(`__d2lTestHovering-${count}`);

[...elem.children, ...elem.shadowRoot?.children ?? []].forEach(child => inlineStyles(child));

const computedStyle = getComputedStyle(elem);
[...computedStyle].forEach(prop => {
if (!disallowedProps.includes(prop)) {
elem.style[prop] = computedStyle.getPropertyValue(prop);
}
});

['before', 'after'].forEach(pseudoEl => {
const computedStyle = getComputedStyle(elem, `::${pseudoEl}`);
if (computedStyle.content !== 'none') {
const sheet = new CSSStyleSheet();
[...computedStyle].forEach(prop => {
if (!disallowedProps.includes(prop)) {
const value = computedStyle.getPropertyValue(prop);
sheet.insertRule(`.__d2lTestHovering-${count}::${pseudoEl} { ${prop}: ${value} !important; }`);
}
});
elem.getRootNode().adoptedStyleSheets.push(sheet);
}
});
}
}
14 changes: 13 additions & 1 deletion src/server/pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ test.pause = new Promise(r => test.start = r);

const controls = `
<style>
.screenshot ::-webkit-scrollbar {
display: none;
}
#d2l-test-controls [hidden] {
display: none !important;
}
body {
margin-top: 70px;
}
.screenshot body {
margin: 8px;
}
#d2l-test-controls {
position: fixed;
top: 0;
Expand All @@ -23,6 +31,10 @@ const controls = `
color: #222;
}
.screenshot #d2l-test-controls {
display: none;
}
#d2l-test-controls #start,
#d2l-test-controls #run {
display: flex;
Expand Down Expand Up @@ -142,7 +154,7 @@ const testName = document.querySelector('#test-name');
let currentTest, focusEl = runBtn;
beforeEach(async function() {
const fixture = new Promise(r => test.update = r);

test.hovering = false;
currentTest = this.currentTest;

if (test.skipAll) this.test.parent.ctx.skip();
Expand Down