Skip to content

Commit 115a8ec

Browse files
authored
test: add integration test for tab reused (#8)
1 parent 2a974d2 commit 115a8ec

File tree

7 files changed

+1083
-40
lines changed

7 files changed

+1083
-40
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
run: yarn --no-progress --non-interactive --no-lockfile
3131

3232
- name: Lint
33-
run: yarn xo src --node-version ">=10.0.0"
33+
run: yarn xo --node-version ">=10.0.0"

.github/workflows/test.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
integration-test:
13+
name: integration test on ${{ matrix.os }}
14+
runs-on: ${{ matrix.os }}
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
os: ['macos-latest']
19+
steps:
20+
- uses: actions/checkout@v1
21+
22+
- name: Get yarn cache directory path
23+
id: yarn-cache-dir-path
24+
run: echo "::set-output name=dir::$(yarn cache dir)"
25+
26+
- uses: actions/cache@v1
27+
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
28+
with:
29+
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
30+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
31+
restore-keys: |
32+
${{ runner.os }}-yarn-
33+
34+
- name: Install
35+
run: yarn --no-progress --non-interactive --no-lockfile
36+
37+
- name: Test on macOS
38+
if: matrix.os == 'macos-latest'
39+
run: yarn test

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@
3636
"node"
3737
]
3838
},
39+
"ava": {
40+
"files": [
41+
"tests/**/*",
42+
"!test/host-only.js"
43+
]
44+
},
3945
"scripts": {
4046
"build": "babel src -d dist",
4147
"_lint": "--node-version is a work around since we use v8 as target in babel anyway",
42-
"lint": "xo src/ --fix --node-version \">=10.0.0\""
48+
"lint": "xo --fix --node-version \">=10.0.0\"",
49+
"test": "ava"
4350
},
4451
"dependencies": {
4552
"open": "^8.0.4"
@@ -48,6 +55,9 @@
4855
"@babel/cli": "^7.8.4",
4956
"@babel/core": "^7.9.0",
5057
"@babel/preset-env": "^7.9.0",
58+
"ava": "^3.6.0",
59+
"lodash.countby": "^4.6.0",
60+
"puppeteer-core": "^8.0.0",
5161
"xo": "^0.28.3"
5262
}
5363
}

test/host-only.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const opn = require('../src/index');
1+
const opn = require('../src');
22

33
// Run test/open.js first, then run this to ensure tab is reused
44
process.env.OPEN_MATCH_HOST_ONLY = 'true';

test/open.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/open.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)