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

Reporting/fix visual warning test #164383

Merged
merged 9 commits into from
Aug 22, 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: 0 additions & 1 deletion .buildkite/ftr_configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ enabled:
- x-pack/test/reporting_functional/reporting_and_deprecated_security.config.ts
- x-pack/test/reporting_functional/reporting_and_security.config.ts
- x-pack/test/reporting_functional/reporting_without_security.config.ts
- x-pack/test/reporting_functional/reporting_and_timeout.config.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: added in 124a76c

- x-pack/test/rule_registry/security_and_spaces/config_basic.ts
- x-pack/test/rule_registry/security_and_spaces/config_trial.ts
- x-pack/test/rule_registry/spaces_only/config_basic.ts
Expand Down
47 changes: 47 additions & 0 deletions x-pack/plugins/screenshotting/server/__mocks__/puppeteer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

const stubDevTools = {
send: jest.fn(),
};
const stubTarget = {
createCDPSession: jest.fn(() => {
return stubDevTools;
}),
};
const stubPage = {
target: jest.fn(() => {
return stubTarget;
}),
emulateTimezone: jest.fn(),
setDefaultTimeout: jest.fn(),
isClosed: jest.fn(),
setViewport: jest.fn(),
evaluate: jest.fn(),
screenshot: jest.fn().mockResolvedValue(`you won't believe this one weird screenshot`),
evaluateOnNewDocument: jest.fn(),
setRequestInterception: jest.fn(),
_client: jest.fn(() => ({ on: jest.fn() })),
on: jest.fn(),
goto: jest.fn(),
waitForSelector: jest.fn().mockResolvedValue(true),
waitForFunction: jest.fn(),
};
const stubBrowser = {
newPage: jest.fn(() => {
return stubPage;
}),
};

const puppeteer = {
launch: jest.fn(() => {
return stubBrowser;
}),
};

// eslint-disable-next-line import/no-default-export
export default puppeteer;
107 changes: 107 additions & 0 deletions x-pack/plugins/screenshotting/server/browsers/chromium/driver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { Logger } from '@kbn/logging';
import { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import * as puppeteer from 'puppeteer';
import { Size } from '../../../common/layout';
import { ConfigType } from '../../config';
import { PreserveLayout } from '../../layouts/preserve_layout';
import { HeadlessChromiumDriver } from './driver';

describe('chromium driver', () => {
let mockConfig: ConfigType;
let mockLogger: Logger;
let mockScreenshotModeSetup: ScreenshotModePluginSetup;
let mockPage: puppeteer.Page;

const mockBasePath = '/kibanaTest1';

beforeEach(() => {
mockLogger = { debug: jest.fn(), error: jest.fn(), info: jest.fn() } as unknown as Logger;
mockLogger.get = () => mockLogger;

mockConfig = {
networkPolicy: {
enabled: false,
rules: [],
},
browser: {
autoDownload: false,
chromium: { proxy: { enabled: false } },
},
capture: {
timeouts: {
openUrl: 60000,
waitForElements: 60000,
renderComplete: 60000,
},
zoom: 2,
},
poolSize: 1,
};

mockPage = {
screenshot: jest.fn().mockResolvedValue(`you won't believe this one weird screenshot`),
evaluate: jest.fn(),
} as unknown as puppeteer.Page;

mockScreenshotModeSetup = {
setScreenshotContext: jest.fn(),
setScreenshotModeEnabled: jest.fn(),
isScreenshotMode: jest.fn(),
};
});

it('return screenshot with preserve layout option', async () => {
const driver = new HeadlessChromiumDriver(
mockScreenshotModeSetup,
mockConfig,
mockBasePath,
mockPage
);

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

expect(result).toEqual(Buffer.from(`you won't believe this one weird screenshot`, 'base64'));
});

it('add error to screenshot contents', async () => {
const driver = new HeadlessChromiumDriver(
mockScreenshotModeSetup,
mockConfig,
mockBasePath,
mockPage
);

// @ts-expect-error spy on non-public class method
const testSpy = jest.spyOn(driver, 'injectScreenshottingErrorHeader');

const result = await driver.screenshot({
elementPosition: {
boundingClientRect: { top: 200, left: 10, height: 10, width: 100 },
scroll: { x: 100, y: 300 },
},
layout: new PreserveLayout({} as Size),
error: new Error(`Here's the fake error!`),
});

expect(testSpy.mock.lastCall).toMatchInlineSnapshot(`
Array [
[Error: Here's the fake error!],
"[data-shared-items-container]",
]
`);
expect(result).toEqual(Buffer.from(`you won't believe this one weird screenshot`, 'base64'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { Logger } from '@kbn/core/server';
import { loggerMock } from '@kbn/logging-mocks';
Copy link
Member Author

@tsullivan tsullivan Aug 22, 2023

Choose a reason for hiding this comment

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

There aren't any changes made to this file except for some minor cleanups

import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import * as puppeteer from 'puppeteer';
import * as Rx from 'rxjs';
Expand All @@ -24,22 +25,18 @@ describe('HeadlessChromiumDriverFactory', () => {
},
},
} as ConfigType;
let logger: jest.Mocked<Logger>;
let screenshotMode: jest.Mocked<ScreenshotModePluginSetup>;
let logger: Logger;
let screenshotMode: ScreenshotModePluginSetup;
let factory: HeadlessChromiumDriverFactory;
let mockBrowser: jest.Mocked<puppeteer.Browser>;
let mockBrowser: puppeteer.Browser;

beforeEach(async () => {
logger = {
debug: jest.fn(),
error: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
get: jest.fn(() => logger),
} as unknown as typeof logger;
screenshotMode = {} as unknown as typeof screenshotMode;
logger = loggerMock.create();

screenshotMode = {} as unknown as ScreenshotModePluginSetup;

let pageClosed = false;

mockBrowser = {
newPage: jest.fn().mockResolvedValue({
target: jest.fn(() => ({
Expand All @@ -57,9 +54,8 @@ describe('HeadlessChromiumDriverFactory', () => {
pageClosed = true;
}),
process: jest.fn(),
} as unknown as jest.Mocked<puppeteer.Browser>;

(puppeteer as jest.Mocked<typeof puppeteer>).launch.mockResolvedValue(mockBrowser);
} as unknown as puppeteer.Browser;
jest.spyOn(puppeteer, 'launch').mockResolvedValue(mockBrowser);

factory = new HeadlessChromiumDriverFactory(screenshotMode, config, logger, path, '');
jest.spyOn(factory, 'getBrowserLogger').mockReturnValue(Rx.EMPTY);
Expand All @@ -84,9 +80,8 @@ describe('HeadlessChromiumDriverFactory', () => {
});

it('rejects if Puppeteer launch fails', async () => {
(puppeteer as jest.Mocked<typeof puppeteer>).launch.mockRejectedValue(
`Puppeteer Launch mock fail.`
);
jest.spyOn(puppeteer, 'launch').mockRejectedValue(`Puppeteer Launch mock fail.`);

expect(() =>
factory
.createPage({ openUrlTimeout: 0, defaultViewport: DEFAULT_VIEWPORT })
Expand All @@ -99,9 +94,8 @@ describe('HeadlessChromiumDriverFactory', () => {

describe('close behaviour', () => {
it('does not allow close to be called on the browse more than once', async () => {
await factory
.createPage({ openUrlTimeout: 0, defaultViewport: DEFAULT_VIEWPORT })
.pipe(
await Rx.firstValueFrom(
factory.createPage({ openUrlTimeout: 0, defaultViewport: DEFAULT_VIEWPORT }).pipe(
take(1),
mergeMap(async ({ close }) => {
expect(mockBrowser.close).not.toHaveBeenCalled();
Expand All @@ -110,7 +104,7 @@ describe('HeadlessChromiumDriverFactory', () => {
expect(mockBrowser.close).toHaveBeenCalledTimes(1);
})
)
.toPromise();
);
// Check again, after the observable completes
expect(mockBrowser.close).toHaveBeenCalledTimes(1);
});
Expand Down
Loading