-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
tsullivan
merged 9 commits into
elastic:main
from
tsullivan:reporting/fix-visual-warning-test
Aug 22, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1d3d951
remove failing functional test
tsullivan 368f2d5
Rename screenshots.ts
tsullivan 140a669
Add jest integration test and puppeteer mocks
tsullivan ee9a2ad
Unit test for chromium driver screenshot method
tsullivan ef5dcf1
clean up tests
tsullivan 60ececc
minimal diff
tsullivan 0331bbe
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 107727e
Merge branch 'main' into reporting/fix-visual-warning-test
tsullivan ea9c590
remove reporting_and_timeout.config from ftr list
tsullivan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
x-pack/plugins/screenshotting/server/__mocks__/puppeteer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
x-pack/plugins/screenshotting/server/browsers/chromium/driver.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
*/ | ||
|
||
import type { Logger } from '@kbn/core/server'; | ||
import { loggerMock } from '@kbn/logging-mocks'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
@@ -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(() => ({ | ||
|
@@ -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); | ||
|
@@ -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 }) | ||
|
@@ -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(); | ||
|
@@ -110,7 +104,7 @@ describe('HeadlessChromiumDriverFactory', () => { | |
expect(mockBrowser.close).toHaveBeenCalledTimes(1); | ||
}) | ||
) | ||
.toPromise(); | ||
); | ||
// Check again, after the observable completes | ||
expect(mockBrowser.close).toHaveBeenCalledTimes(1); | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: added in 124a76c