Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD] Added support for win32 platform #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 59 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,56 @@ const chromeFinder = require('chrome-launcher/dist/src/chrome-finder');
const chromeUtils = require('chrome-launcher/dist/src/utils');
const parser = require('xml2js');

function getMajorFromVersion(version) {
return +version.split('.')[0];
}

function getChromeMajorVersion() {
const promise = new Promise((resolve, reject) => {
const platform = chromeUtils.getPlatform();

const installations = chromeFinder[platform]();

console.log('Platform', platform);
console.log('Chrome Installations', installations);

if (installations && installations.length > 0) {
const source = spawn(
installations[0],
[
'--version'
]
);

source.stdout.on('data', (data) => {
const chromeVersion = data.toString().trim().substr('Google Chrome '.length);
const chromeMajorVersion = +chromeVersion.split('.')[0];

resolve({
chromeVersion: chromeVersion,
chromeMajorVersion: chromeMajorVersion
if (platform === 'win32') {

const source = spawn('wmic');
source.stdin.end(
`datafile where "name='${installations[0].replace(/\\/g, '\\\\')}'" get version /value`
);

source.stdout.on('close', () => reject('Unable to get Chrome version from wmic (close)'));
source.stderr.on('data', () => reject('Unable to get Chrome version from wmic (err)'));
source.stdout.on('data', (data) => {

const dataCleaned = data.toString().trim().toLowerCase();
if (dataCleaned.indexOf('version=') > -1) {
const chromeVersion = dataCleaned.split('version=')[1].split('wmic')[0].replace(/\r?\n|\r/g, '');
resolve({
chromeVersion: chromeVersion,
chromeMajorVersion: getMajorFromVersion(chromeVersion)
});
}
});
});

} else {

const source = spawn(installations[0], ['--version']);
source.stdout.on('close', () => reject('Unable to get Chrome version (close)'));
source.stderr.on('data', () => reject('Unable to get Chrome version (err)'));
source.stdout.on('data', (data) => {
const chromeVersion = data.toString()
.trim().substr('Google Chrome '.length).split(' ')[0];
resolve({
chromeVersion: chromeVersion,
chromeMajorVersion: getMajorFromVersion(chromeVersion)
});
});

}

} else {
reject('No local installation of Chrome was found.');
}
Expand All @@ -38,7 +65,7 @@ function getChromeMajorVersion() {
function getLatestChromeDriverVersion() {
return new Promise((resolve, reject) => {
request(
'https://chromedriver.storage.googleapis.com/',
'https://chromedriver.storage.googleapis.com/',
(error, response, body) => {
if (error) {
reject(error);
Expand All @@ -57,18 +84,18 @@ function getLatestChromeDriverVersion() {
.sort((a, b) => {
const aMinorVersion = +a.split('.')[1];
const bMinorVersion = +b.split('.')[1];

if (aMinorVersion < bMinorVersion) {
return 1;
}

if (bMinorVersion < aMinorVersion) {
return -1;
}

return 0;
});

resolve(versionNumbers[0]);
}
});
Expand All @@ -81,7 +108,7 @@ function getLatestChromeDriverVersion() {
function getReleaseNotes(latestChromeDriverVersion) {
return new Promise((resolve, reject) => {
request(
'https://chromedriver.storage.googleapis.com/' +
'https://chromedriver.storage.googleapis.com/' +
latestChromeDriverVersion +
'/notes.txt',
(error, response, body) => {
Expand All @@ -101,7 +128,7 @@ function findVersionInReleaseNotes(releaseNotes, chromeMajorVersion) {
// Supports Chrome v69-71

const lines = releaseNotes.split('\n');

for (let i = 0, n = lines.length; i < n; i++) {
const line = lines[i];

Expand All @@ -123,11 +150,11 @@ function findVersionInReleaseNotes(releaseNotes, chromeMajorVersion) {
}

function getChromeDriverVersion() {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
Promise.all(
[
getChromeMajorVersion(),
getLatestChromeDriverVersion()
getLatestChromeDriverVersion()
]
)
.then((results) => {
Expand All @@ -139,11 +166,15 @@ function getChromeDriverVersion() {
resolve({
chromeVersion: chromeVersion.chromeVersion,
chromeDriverVersion: findVersionInReleaseNotes(
releaseNotes,
releaseNotes,
chromeVersion.chromeMajorVersion
)
});
});
})
.catch((err) => {
console.log(err);
reject(err);
});
});
}
Expand All @@ -152,4 +183,5 @@ module.exports = {
getChromeDriverVersion: getChromeDriverVersion
};

// getChromeDriverVersion().then(result => console.log(result));
// USAGE:
// getChromeDriverVersion().then(result => console.log(result));