|
| 1 | +const test = require('ava'); |
| 2 | +const countBy = require('lodash.countby'); |
| 3 | +const puppeteer = require('puppeteer-core'); |
| 4 | +const open = require('../src'); |
| 5 | + |
| 6 | +const browserName = 'Google Chrome'; |
| 7 | +const openUrl = 'https://www.google.com/'; |
| 8 | +const openSecondUrl = 'https://stackoverflow.com/'; |
| 9 | +let chromeExecutablePath; |
| 10 | +if (process.platform === 'darwin') { |
| 11 | + chromeExecutablePath = `/Applications/${browserName}.app/Contents/MacOS/${browserName}`; |
| 12 | +} else if (process.platform === 'linux') { |
| 13 | + // https://github.com/mujo-code/puppeteer-headful#usage |
| 14 | + chromeExecutablePath = process.env.PUPPETEER_EXEC_PATH; |
| 15 | +} else if (process.platform === 'win32') { |
| 16 | + chromeExecutablePath = 'Chrome'; |
| 17 | +} |
| 18 | + |
| 19 | +function sleep(ms) { |
| 20 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 21 | +} |
| 22 | + |
| 23 | +test.serial('the same tab is reused in browser on macOS', async t => { |
| 24 | + if (process.platform === 'darwin') { |
| 25 | + const browser = await puppeteer.launch({ |
| 26 | + headless: false, |
| 27 | + executablePath: chromeExecutablePath, |
| 28 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], |
| 29 | + }); |
| 30 | + |
| 31 | + // Open url with better-opn twice |
| 32 | + await open(openUrl); |
| 33 | + await open(openUrl); |
| 34 | + |
| 35 | + // Workaround since new pages are not avaliable immediately |
| 36 | + // https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698 |
| 37 | + await sleep(5000); |
| 38 | + |
| 39 | + // Get open pages/tabs |
| 40 | + const openPages = (await browser.pages()).map(each => each.url()); |
| 41 | + const openPagesCounter = countBy(openPages); |
| 42 | + // Expect only one page is opened |
| 43 | + t.is(openPagesCounter[openUrl], 1); |
| 44 | + |
| 45 | + // Close browser |
| 46 | + await browser.close(); |
| 47 | + } else { |
| 48 | + // Skip for non-macOS environments |
| 49 | + t.pass(); |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | +test.serial( |
| 54 | + 'two tabs are opened when opening two different urls in browser on macOS', |
| 55 | + async t => { |
| 56 | + if (process.platform === 'darwin') { |
| 57 | + const browser = await puppeteer.launch({ |
| 58 | + headless: false, |
| 59 | + executablePath: chromeExecutablePath, |
| 60 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], |
| 61 | + }); |
| 62 | + |
| 63 | + // Open url with better-opn twice |
| 64 | + await open(openUrl); |
| 65 | + await open(openSecondUrl); |
| 66 | + |
| 67 | + // Workaround since new pages are not avaliable immediately |
| 68 | + // https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698 |
| 69 | + await sleep(5000); |
| 70 | + |
| 71 | + // Get open pages/tabs |
| 72 | + const openPages = (await browser.pages()).map(each => each.url()); |
| 73 | + const openPagesCounter = countBy(openPages); |
| 74 | + // Expect only one of each page is opened |
| 75 | + t.is(openPagesCounter[openUrl], 1); |
| 76 | + t.is(openPagesCounter[openSecondUrl], 1); |
| 77 | + |
| 78 | + // Close browser |
| 79 | + await browser.close(); |
| 80 | + } else { |
| 81 | + // Skip for non-macOS environments |
| 82 | + t.pass(); |
| 83 | + } |
| 84 | + } |
| 85 | +); |
| 86 | + |
| 87 | +test.serial('open url in browser', async t => { |
| 88 | + const browser = await puppeteer.launch({ |
| 89 | + headless: false, |
| 90 | + executablePath: chromeExecutablePath, |
| 91 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], |
| 92 | + }); |
| 93 | + |
| 94 | + await open(openUrl); |
| 95 | + |
| 96 | + // Workaround since new pages are not avaliable immediately |
| 97 | + // https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698 |
| 98 | + await sleep(5000); |
| 99 | + |
| 100 | + // Get open pages/tabs |
| 101 | + const openPages = (await browser.pages()).map(each => each.url()); |
| 102 | + const openPagesCounter = countBy(openPages); |
| 103 | + |
| 104 | + // Expect page is opened |
| 105 | + t.is(openPagesCounter[openUrl], 1); |
| 106 | + |
| 107 | + await browser.close(); |
| 108 | +}); |
| 109 | + |
| 110 | +test.serial( |
| 111 | + 'should not open browser when process.env.BROWSER is none', |
| 112 | + async t => { |
| 113 | + const browser = await puppeteer.launch({ |
| 114 | + headless: false, |
| 115 | + executablePath: chromeExecutablePath, |
| 116 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], |
| 117 | + }); |
| 118 | + process.env.BROWSER = 'none'; |
| 119 | + |
| 120 | + // Open url |
| 121 | + await open(openUrl); |
| 122 | + |
| 123 | + // Get open pages/tabs |
| 124 | + const openPages = (await browser.pages()).map(each => each.url()); |
| 125 | + const openPagesCounter = countBy(openPages); |
| 126 | + |
| 127 | + // Expect no page is opened |
| 128 | + t.is(openPagesCounter[openUrl], undefined); |
| 129 | + |
| 130 | + // Clean up |
| 131 | + process.env.BROWSER = browserName; |
| 132 | + await browser.close(); |
| 133 | + } |
| 134 | +); |
0 commit comments