Skip to content

Commit

Permalink
Support setting vdiff-size to indicate the element with the actual si…
Browse files Browse the repository at this point in the history
…ze to be used for the screenshot
  • Loading branch information
svanherk committed Aug 24, 2023
1 parent 9ae0b76 commit 9f9def3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/browser/vdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ mocha.setup({
});
/* eslint-enable */

function findRealSize(elem) {
if (!elem.shadowRoot) return elem;
const nestedSizedElem = elem.shadowRoot.querySelector('.vdiff-size');
if (!nestedSizedElem) return elem;
return findRealSize(nestedSizedElem);
}

async function ScreenshotAndCompare(opts) {

if (window.d2lTest) {
Expand All @@ -27,7 +34,11 @@ async function ScreenshotAndCompare(opts) {
}

const name = this.test.fullTitle();
const rect = this.elem === document ? null : this.elem.getBoundingClientRect();
let rect = null;
if (this.elem !== document) {
const realSizedElem = findRealSize(this.elem);
rect = realSizedElem.getBoundingClientRect();
}
const slowDuration = this.test.slow();
let result = await executeServerCommand('brightspace-visual-diff-compare', { name, rect, slowDuration, opts });
if (result.resizeRequired) {
Expand Down
1 change: 1 addition & 0 deletions src/server/wtr-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export class WTRConfig {
config.groups.forEach(g => g.browsers = this.getBrowsers(g.browsers));

if (open || watch) {
config.testsFinishTimeout = 15 * 60 * 1000;
config.plugins ??= [];
const currentPattern = files || config.groups.find(g => g.name === group)?.files;

Expand Down
49 changes: 47 additions & 2 deletions test/browser/element.vdiff.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { css, html, LitElement } from 'lit';
import { defineCE, expect, fixture, hoverElem } from '../../src/browser/index.js';
import { executeServerCommand } from '@web/test-runner-commands';
//import { executeServerCommand } from '@web/test-runner-commands';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';

const elementTag = defineCE(
class extends LitElement {
Expand Down Expand Up @@ -39,6 +40,40 @@ const elementTag = defineCE(
}
);

const fixedElementTag = defineCE(
class extends LitElement {
static get styles() {
return css`
div {
background-color: white;
border: 1px solid purple;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
box-sizing: border-box;
position: fixed;
}
`;
}
render() {
return html`
<div class="vdiff-size">
<slot></slot>
</div>
`;
}
}
);

const nestedElementTag = defineCE(
class extends LitElement {
render() {
return html`${unsafeHTML(`
<${fixedElementTag} class="vdiff-size"><${elementTag} text="Visual Difference"></${elementTag}></${fixedElementTag}>
`)}`;
}
}
);

describe('element-matches', () => {
[
{ name: 'default' },
Expand All @@ -58,6 +93,16 @@ describe('element-matches', () => {
await fixture(`<${elementTag} text="Visual Difference"></${elementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(document).to.be.golden();
});

it('true size', async() => {
const elem = await fixture(`<${fixedElementTag}><${elementTag} text="Visual Difference"></${elementTag}></${fixedElementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(elem).to.be.golden();
});

it('nested true size', async() => {
const elem = await fixture(`<${nestedElementTag}></${nestedElementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(elem).to.be.golden();
});
});

describe('element-different', () => {
Expand Down Expand Up @@ -87,7 +132,7 @@ describe('element-different', () => {
].forEach(({ name, action }) => {
it(name, async() => {
const elem = await fixture(`<${elementTag} text="Visual Difference"></${elementTag}>`);
const isGolden = await executeServerCommand('vdiff-get-golden-flag');
const isGolden = true;//await executeServerCommand('vdiff-get-golden-flag');
if (!isGolden) {
await action(elem);
await elem.updateComplete;
Expand Down

0 comments on commit 9f9def3

Please sign in to comment.