Skip to content

Commit

Permalink
feat: reimplement getChromeDevtoolsPort
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Jul 5, 2018
1 parent a7b2116 commit ee2e251
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

var urlLib = require('url');
var http = require('http');
var path = require('path');
var fs = require('fs');

var _ = require('lodash');
var debug = require('debug')('testium-core:init');
Expand Down Expand Up @@ -104,6 +106,21 @@ function initTestium(config) {
return browser && browser.close();
}

var devtoolsPort = null;
function getChromeDevtoolsPort() {
var capabilities = testium.browser.capabilities;
if (!capabilities.chrome) {
throw new Error('Can only get devtools port for chrome');
}
if (devtoolsPort === null) {
var userDataDir = capabilities.chrome.userDataDir;
var devToolsPortPath = path.join(userDataDir, 'DevToolsActivePort');
var devToolsPortFile = fs.readFileSync(devToolsPortPath, 'utf8');
devtoolsPort = +devToolsPortFile.split('\n')[0];
}
return devtoolsPort;
}

function killAllProcesses() {
_.each(procs, function killProc(proc, name) {
try {
Expand All @@ -130,6 +147,7 @@ function initTestium(config) {
testium = {
close: close,
config: config,
getChromeDevtoolsPort: getChromeDevtoolsPort,
getInitialUrl: getInitialUrl,
getNewPageUrl: _.partial(getNewPageUrl, config.get('proxy.targetUrl'))
};
Expand Down
12 changes: 12 additions & 0 deletions test/testium-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ describe('testium-core', () => {
});
});

describe('getChromeDevtoolsPort', () => {
it('returns a number >= 1024 in chrome, throws otherwise', () => {
if (testium.browser.capabilities.browserName === 'chrome') {
assert.hasType(Number, testium.getChromeDevtoolsPort());
assert.expect(testium.getChromeDevtoolsPort() >= 1024);
} else {
const err = assert.throws(() => testium.getChromeDevtoolsPort());
assert.equal('Can only get devtools port for chrome', err.message);
}
});
});

describe('basic navigation', () => {
it('can navigate to /index.html', async () => {
const browser = await getBrowser();
Expand Down

0 comments on commit ee2e251

Please sign in to comment.