diff --git a/docs/api.md b/docs/api.md index 38a97e80..1bc66a58 100644 --- a/docs/api.md +++ b/docs/api.md @@ -2741,7 +2741,7 @@ If the target is not of type `"page"`, returns `null`. #### target.type() - returns: <[string]> -Identifies what kind of target this is. Can be `"page"`, `"service_worker"`, `"browser"` or `"other"`. +Identifies what kind of target this is. Can be `"page"`, [`"background_page"`](https://developer.chrome.com/extensions/background_pages), `"service_worker"`, `"browser"` or `"other"`. #### target.url() - returns: <[string]> diff --git a/lib/Target.js b/lib/Target.js index 01b6cc16..930e6fbf 100644 --- a/lib/Target.js +++ b/lib/Target.js @@ -53,11 +53,11 @@ class Target { } /** - * @return {"page"|"service_worker"|"other"|"browser"} + * @return {"page"|"background_page"|"service_worker"|"other"|"browser"} */ type() { const type = this._targetInfo.type; - if (type === 'page' || type === 'service_worker' || type === 'browser') + if (type === 'page' || type === 'background_page' || type === 'service_worker' || type === 'browser') return type; return 'other'; } diff --git a/test/assets/simple-extension/index.js b/test/assets/simple-extension/index.js new file mode 100644 index 00000000..a87bdeed --- /dev/null +++ b/test/assets/simple-extension/index.js @@ -0,0 +1 @@ +// Mock script for background extension \ No newline at end of file diff --git a/test/assets/simple-extension/manifest.json b/test/assets/simple-extension/manifest.json new file mode 100644 index 00000000..649bce40 --- /dev/null +++ b/test/assets/simple-extension/manifest.json @@ -0,0 +1,12 @@ +{ + "name": "Simple extension", + "version": "0.1", + "app": { + "background": { + "scripts": ["index.js"] + } + }, + "permissions": ["background"], + + "manifest_version": 2 +} diff --git a/test/target.spec.js b/test/target.spec.js index fe2f0403..7af6d142 100644 --- a/test/target.spec.js +++ b/test/target.spec.js @@ -16,7 +16,7 @@ const {waitEvent} = require('./utils'); -module.exports.addTests = function({testRunner, expect}) { +module.exports.addTests = function({testRunner, expect, puppeteer, browserWithExtensionOptions}) { const {describe, xdescribe, fdescribe} = testRunner; const {it, fit, xit} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; @@ -36,6 +36,15 @@ module.exports.addTests = function({testRunner, expect}) { expect(allPages).toContain(page); expect(allPages[0]).not.toBe(allPages[1]); }); + it('should allow background_page target type to pass through', async({browser}) => { + const browserWithExtension = await puppeteer.launch(browserWithExtensionOptions); + const page = await browserWithExtension.newPage(); + const targets = await browserWithExtension.targets(); + const backgroundPageTarget = targets.find(target => target.type() === 'background_page'); + await page.close(); + await browserWithExtension.close(); + expect(backgroundPageTarget).toBeTruthy(); + }); it('should contain browser target', async({browser}) => { const targets = browser.targets(); const browserTarget = targets.find(target => target.type() === 'browser'); diff --git a/test/test.js b/test/test.js index cf42a892..3cb6c7a7 100644 --- a/test/test.js +++ b/test/test.js @@ -35,6 +35,7 @@ const RESET_COLOR = '\x1b[0m'; const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true'; const executablePath = process.env.CHROME; +const extensionPath = path.resolve(__dirname, '../test/assets/simple-extension'); if (executablePath) console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`); @@ -48,6 +49,15 @@ const defaultBrowserOptions = { headless, args: ['--no-sandbox'] }; +const browserWithExtensionOptions = { + headless: false, + executablePath, + args: [ + '--no-sandbox', + `--disable-extensions-except=${extensionPath}`, + `--load-extension=${extensionPath}`, + ], +}; let parallel = 1; if (process.env.PPTR_PARALLEL_TESTS) @@ -69,7 +79,7 @@ if (fs.existsSync(OUTPUT_DIR)) console.log('Testing on Node', process.version); -beforeAll(async state => { +beforeAll(async state => { const assetsPath = path.join(__dirname, 'assets'); const cachedPath = path.join(__dirname, 'assets', 'cached'); @@ -131,7 +141,7 @@ describe('Page', function() { require('./jshandle.spec.js').addTests({testRunner, expect}); require('./network.spec.js').addTests({testRunner, expect}); require('./page.spec.js').addTests({testRunner, expect, puppeteer, DeviceDescriptors, headless}); - require('./target.spec.js').addTests({testRunner, expect}); + require('./target.spec.js').addTests({testRunner, expect, puppeteer, browserWithExtensionOptions}); require('./tracing.spec.js').addTests({testRunner, expect}); }); @@ -139,7 +149,7 @@ describe('Page', function() { require('./puppeteer.spec.js').addTests({testRunner, expect, PROJECT_ROOT, defaultBrowserOptions}); if (process.env.COVERAGE) { - describe('COVERAGE', function(){ + describe('COVERAGE', function() { const coverage = helper.publicAPICoverage(); const disabled = new Set(['page.bringToFront']); if (!headless)