Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import type { ReportParamsGetter, ReportParamsGetterOptions } from '../../types'
import type { JobParamsProviderOptions } from '../share_context_menu';

const getBaseParams = (objectType: string) => {
const viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ Do we actually need the second part? I assume we don’t need to account for scrollbar width when taking screenshots

Suggested change
const viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
const viewportWidth = document.documentElement.clientWidth;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for the old browsers. Looks like document.documentElement.clientWidth doesn't work for some old browsers.


const el = document.querySelector('[data-shared-items-container]');
const { height, width } = el ? el.getBoundingClientRect() : { height: 768, width: 1024 };
const dimensions = { height, width };

return {
objectType,
layout: {
id: 'preserve_layout' as 'preserve_layout' | 'print',
dimensions,
dimensions: {
height,
width: viewportWidth || width,
},
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('chromium driver', () => {
};
});

afterEach(() => {
jest.resetAllMocks();
});

it('return screenshot with preserve layout option', async () => {
const driver = new HeadlessChromiumDriver(
mockScreenshotModeSetup,
Expand Down Expand Up @@ -105,4 +109,28 @@ describe('chromium driver', () => {
`);
expect(result).toEqual(Buffer.from(`you won't believe this one weird screenshot`, 'base64'));
});

it('sets the PDF image size', async () => {
const driver = new HeadlessChromiumDriver(
mockScreenshotModeSetup,
mockConfig,
mockBasePath,
mockPage
);

const layout = new PreserveLayout({} as Size);

const testSpy = jest.spyOn(layout, 'setPdfImageSize');
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed this file is missing

  afterEach(() => {
    jest.resetAllMocks();
  });

Could you please add it?


await driver.screenshot({
elementPosition: {
boundingClientRect: { top: 200, left: 10, height: 10, width: 100 },
scroll: { x: 100, y: 300 },
},
layout,
});

expect(testSpy).toHaveBeenCalledTimes(1);
expect(testSpy).toHaveBeenCalledWith({ height: 10, width: 100 });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ export class HeadlessChromiumDriver {

const { boundingClientRect, scroll } = elementPosition;

layout.setPdfImageSize({
height: boundingClientRect.height,
width: boundingClientRect.width,
});

const screenshot = await this.page.screenshot({
clip: {
x: boundingClientRect.left + scroll.x,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('PdfMaker', () => {

beforeEach(() => {
layout = createMockLayout();
layout.setPdfImageSize({ height: 100, width: 100 });
logger = loggingSystemMock.createLogger();
packageInfo = {
branch: 'screenshot-test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ViewZoomWidthHeight {

export interface PdfImageSize {
width: number;
height?: number;
height: number;
}

export interface PageSizeParams {
Expand All @@ -40,6 +40,7 @@ export abstract class BaseLayout {
}

public abstract getPdfImageSize(): PdfImageSize;
public abstract setPdfImageSize({ height, width }: PdfImageSize): void;

public abstract getPdfPageOrientation(): 'portrait' | 'landscape' | undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { LayoutSelectorDictionary, Size } from '../../common/layout';
import { DEFAULT_SELECTORS } from '.';
import type { Layout } from '.';
import type { PdfImageSize } from './base_layout';
import { BaseLayout } from './base_layout';

// FIXME - should use zoom from capture config
Expand All @@ -25,6 +26,7 @@ export class CanvasLayout extends BaseLayout implements Layout {
public readonly width: number;
private readonly scaledHeight: number;
private readonly scaledWidth: number;
private imageSize: PdfImageSize = { height: 0, width: 0 };

public hasHeader: boolean = false;
public hasFooter: boolean = false;
Expand Down Expand Up @@ -65,11 +67,12 @@ export class CanvasLayout extends BaseLayout implements Layout {
};
}

public setPdfImageSize({ height, width }: PdfImageSize): void {
this.imageSize = { height, width };
}

public getPdfImageSize() {
return {
height: this.height,
width: this.width,
};
return this.imageSize;
}

public getPdfPageSize(): Size {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('Create Layout', () => {
"hasHeader": true,
"height": 16,
"id": "preserve_layout",
"imageSize": Object {
"height": 0,
"width": 0,
},
"scaledHeight": 32,
"scaledWidth": 32,
"selectors": Object {
Expand Down Expand Up @@ -78,6 +82,10 @@ describe('Create Layout', () => {
"hasHeader": false,
"height": 18,
"id": "canvas",
"imageSize": Object {
"height": 0,
"width": 0,
},
"scaledHeight": 36,
"scaledWidth": 36,
"selectors": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { PreserveLayout } from './preserve_layout';
it('preserve layout uses default layout selectors', () => {
const testPreserveLayout = new PreserveLayout({ width: 16, height: 16 });

testPreserveLayout.setPdfImageSize({ height: 16, width: 16 });

expect(testPreserveLayout.getCssOverridesPath()).toMatch(`layouts/preserve_layout.css`);
expect(testPreserveLayout.getBrowserViewport()).toMatchObject({ height: 32, width: 32 });
expect(testPreserveLayout.getBrowserZoom()).toBe(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { LayoutSelectorDictionary, Size } from '../../common/layout';
import { DEFAULT_SELECTORS } from '.';
import type { Layout } from '.';
import { BaseLayout } from './base_layout';
import type { PageSizeParams } from './base_layout';
import type { PageSizeParams, PdfImageSize } from './base_layout';

// We use a zoom of two to bump up the resolution of the screenshot a bit.
const ZOOM: number = 2;
Expand All @@ -21,6 +21,7 @@ export class PreserveLayout extends BaseLayout implements Layout {
public readonly width: number;
private readonly scaledHeight: number;
private readonly scaledWidth: number;
private imageSize: PdfImageSize = { height: 0, width: 0 };

constructor(size: Size, selectors?: Partial<LayoutSelectorDictionary>) {
super('preserve_layout');
Expand Down Expand Up @@ -56,11 +57,12 @@ export class PreserveLayout extends BaseLayout implements Layout {
};
}

public setPdfImageSize({ height, width }: PdfImageSize): void {
this.imageSize = { height, width };
}

public getPdfImageSize() {
return {
height: this.height,
width: this.width,
};
return this.imageSize;
}

public getPdfPageOrientation() {
Expand All @@ -70,13 +72,16 @@ export class PreserveLayout extends BaseLayout implements Layout {
public getPdfPageSize(pageSizeParams: PageSizeParams): CustomPageSize {
return {
height:
this.height +
this.imageSize.height +
pageSizeParams.pageMarginTop +
pageSizeParams.pageMarginBottom +
pageSizeParams.tableBorderWidth * 2 +
pageSizeParams.headingHeight +
pageSizeParams.subheadingHeight,
width: this.width + pageSizeParams.pageMarginWidth * 2 + pageSizeParams.tableBorderWidth * 2,
width:
this.imageSize.width +
pageSizeParams.pageMarginWidth * 2 +
pageSizeParams.tableBorderWidth * 2,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Layout } from '.';
import { DEFAULT_SELECTORS } from '.';
import type { LayoutParams, LayoutSelectorDictionary } from '../../common/layout';
import { DEFAULT_VIEWPORT } from '../browsers';
import type { PdfImageSize } from './base_layout';
import { BaseLayout } from './base_layout';

export const getPrintLayoutSelectors: () => LayoutSelectorDictionary = () => ({
Expand Down Expand Up @@ -51,9 +52,12 @@ export class PrintLayout extends BaseLayout implements Layout {
public getPdfImageSize() {
return {
width: 500,
};
} as PdfImageSize;
}

// Image size is not used in print layout
setPdfImageSize({ height, width }: PdfImageSize): void {}

public getPdfPageOrientation(): PageOrientation {
return 'portrait';
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.