Skip to content

Commit

Permalink
test: add integration test for tab reused (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellzc authored Apr 2, 2021
1 parent 2a974d2 commit 115a8ec
Show file tree
Hide file tree
Showing 7 changed files with 1,083 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
run: yarn --no-progress --non-interactive --no-lockfile

- name: Lint
run: yarn xo src --node-version ">=10.0.0"
run: yarn xo --node-version ">=10.0.0"
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
integration-test:
name: integration test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ['macos-latest']
steps:
- uses: actions/checkout@v1

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install
run: yarn --no-progress --non-interactive --no-lockfile

- name: Test on macOS
if: matrix.os == 'macos-latest'
run: yarn test
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@
"node"
]
},
"ava": {
"files": [
"tests/**/*",
"!test/host-only.js"
]
},
"scripts": {
"build": "babel src -d dist",
"_lint": "--node-version is a work around since we use v8 as target in babel anyway",
"lint": "xo src/ --fix --node-version \">=10.0.0\""
"lint": "xo --fix --node-version \">=10.0.0\"",
"test": "ava"
},
"dependencies": {
"open": "^8.0.4"
Expand All @@ -48,6 +55,9 @@
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"ava": "^3.6.0",
"lodash.countby": "^4.6.0",
"puppeteer-core": "^8.0.0",
"xo": "^0.28.3"
}
}
2 changes: 1 addition & 1 deletion test/host-only.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const opn = require('../src/index');
const opn = require('../src');

// Run test/open.js first, then run this to ensure tab is reused
process.env.OPEN_MATCH_HOST_ONLY = 'true';
Expand Down
3 changes: 0 additions & 3 deletions test/open.js

This file was deleted.

134 changes: 134 additions & 0 deletions tests/open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const test = require('ava');
const countBy = require('lodash.countby');
const puppeteer = require('puppeteer-core');
const open = require('../src');

const browserName = 'Google Chrome';
const openUrl = 'https://www.google.com/';
const openSecondUrl = 'https://stackoverflow.com/';
let chromeExecutablePath;
if (process.platform === 'darwin') {
chromeExecutablePath = `/Applications/${browserName}.app/Contents/MacOS/${browserName}`;
} else if (process.platform === 'linux') {
// https://github.com/mujo-code/puppeteer-headful#usage
chromeExecutablePath = process.env.PUPPETEER_EXEC_PATH;
} else if (process.platform === 'win32') {
chromeExecutablePath = 'Chrome';
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

test.serial('the same tab is reused in browser on macOS', async t => {
if (process.platform === 'darwin') {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

// Open url with better-opn twice
await open(openUrl);
await open(openUrl);

// Workaround since new pages are not avaliable immediately
// https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698
await sleep(5000);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);
// Expect only one page is opened
t.is(openPagesCounter[openUrl], 1);

// Close browser
await browser.close();
} else {
// Skip for non-macOS environments
t.pass();
}
});

test.serial(
'two tabs are opened when opening two different urls in browser on macOS',
async t => {
if (process.platform === 'darwin') {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

// Open url with better-opn twice
await open(openUrl);
await open(openSecondUrl);

// Workaround since new pages are not avaliable immediately
// https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698
await sleep(5000);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);
// Expect only one of each page is opened
t.is(openPagesCounter[openUrl], 1);
t.is(openPagesCounter[openSecondUrl], 1);

// Close browser
await browser.close();
} else {
// Skip for non-macOS environments
t.pass();
}
}
);

test.serial('open url in browser', async t => {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

await open(openUrl);

// Workaround since new pages are not avaliable immediately
// https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698
await sleep(5000);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);

// Expect page is opened
t.is(openPagesCounter[openUrl], 1);

await browser.close();
});

test.serial(
'should not open browser when process.env.BROWSER is none',
async t => {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
process.env.BROWSER = 'none';

// Open url
await open(openUrl);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);

// Expect no page is opened
t.is(openPagesCounter[openUrl], undefined);

// Clean up
process.env.BROWSER = browserName;
await browser.close();
}
);
Loading

0 comments on commit 115a8ec

Please sign in to comment.