Commit ac5bae53 authored by YuShijiaCode's avatar YuShijiaCode

driver

parent fa8da103
This diff is collapsed.
# ChromeDriver
[![Build status](https://travis-ci.org/giggio/node-chromedriver.svg)](https://travis-ci.org/giggio/node-chromedriver/) [![Build status](https://ci.appveyor.com/api/projects/status/wr4c16rs5q113vy3?svg=true)](https://ci.appveyor.com/project/giggio/node-chromedriver) [![Build Status](https://api.shippable.com/projects/5c01ad17718ee50700de68bc/badge?branch=master)](https://app.shippable.com/github/giggio/node-chromedriver/runs?branchName=master)
[![npm](https://img.shields.io/npm/dt/chromedriver.svg)](https://www.npmjs.com/package/chromedriver)
An NPM wrapper for Selenium [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/).
## Building and Installing
```shell
npm install chromedriver
```
Or grab the source and
```shell
node ./install.js
```
What this is really doing is just grabbing a particular "blessed" (by
this module) version of ChromeDriver. As new versions are released
and vetted, this module will be updated accordingly.
The package has been set up to fetch and run ChromeDriver for MacOS (darwin),
Linux based platforms (as identified by Node.js), and Windows. If you
spot any platform weirdness, let us know or send a patch.
## Force download
By default this package, when installed, will search for an existing
Chromedriver binary in your configured temp directory. If found, and it is the
correct version, it will simply copy it to your node_modules directory. You can
force it always download by configuring it:
```shell
npm install chromedriver --chromedriver-force-download
```
Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.
```
chromedriver_force_download=true
```
Another option is to use PATH variable `CHROMEDRIVER_FORCE_DOWNLOAD`.
```shell
CHROMEDRIVER_FORCE_DOWNLOAD=true npm install chromedriver
```
## Custom binaries url
To use a mirror of the ChromeDriver binaries use npm config property `chromedriver_cdnurl`.
Default is `http://chromedriver.storage.googleapis.com`.
```shell
npm install chromedriver --chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver
```
Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.
```
chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver
```
Another option is to use PATH variable `CHROMEDRIVER_CDNURL`.
```shell
CHROMEDRIVER_CDNURL=https://npm.taobao.org/mirrors/chromedriver npm install chromedriver
```
## Custom binaries file
To get the chromedriver from the filesystem instead of a web request use the npm config property `chromedriver_filepath`.
```shell
npm install chromedriver --chromedriver_filepath=/path/to/chromedriver_mac64.zip
```
Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.
```
chromedriver_filepath=/path/to/chromedriver_mac64.zip
```
Another option is to use the PATH variable `CHROMEDRIVER_FILEPATH`
```shell
CHROMEDRIVER_FILEPATH=/path/to/chromedriver_mac64.zip
```
This variable can be used to set either a `.zip` file or the binary itself, eg:
```shell
CHROMEDRIVER_FILEPATH=/bin/chromedriver
```
## Custom download options
Install through a proxy.
```shell
npm config set proxy http://[user:pwd]@domain.tld:port
npm config set https-proxy http://[user:pwd]@domain.tld:port
```
Use different User-Agent.
```shell
npm config set user-agent "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
```
## Skipping chromedriver download
You may wish to skip the downloading of the chromedriver binary file, for example if you know for certain that it is already there or if you want to use a system binary and just use this module as an interface to interact with it.
To achieve this you can use the npm config property `chromedriver_skip_download`.
```shell
npm install chromedriver --chromedriver_skip_download=true
```
Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.
```
chromedriver_skip_download=true
```
Another option is to use the PATH variable `CHROMEDRIVER_SKIP_DOWNLOAD`
```shell
CHROMEDRIVER_SKIP_DOWNLOAD=true
```
## Running
```shell
bin/chromedriver [arguments]
```
And npm will install a link to the binary in `node_modules/.bin` as
it is wont to do.
## Running with Selenium WebDriver
```javascript
require('chromedriver');
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
```
(Tested for selenium-webdriver version `2.48.2`)
The path will be added to the process automatically, you don't need to configure it.
But you can get it from `require('chromedriver').path` if you want it.
## Running via node
The package exports a `path` string that contains the path to the
chromdriver binary/executable.
Below is an example of using this package via node.
```javascript
var childProcess = require('child_process');
var chromedriver = require('chromedriver');
var binPath = chromedriver.path;
var childArgs = [
'some argument'
];
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
// handle results
});
```
You can also use the start and stop methods:
```javascript
var chromedriver = require('chromedriver');
args = [
// optional arguments
];
chromedriver.start(args);
// run your tests
chromedriver.stop();
```
With the latest version, you can optionally receive a Promise from the `chromedriver.start` function:
```javascript
var returnPromise = true;
chromedriver
.start(args, returnPromise)
.then(() => {
console.log('chromedriver is ready');
});
```
Note: if your tests are ran asynchronously, chromedriver.stop() will have to be
executed as a callback at the end of your tests
## Versioning
The NPM package version tracks the version of chromedriver that will be installed,
with an additional build number that is used for revisions to the installer.
You can use the package version number to install a specific version, or use the
setting to a specific version. To always install the latest version of Chromedriver,
use `LATEST` as the version number:
```shell
npm install chromedriver --chromedriver_version=LATEST
```
Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.
```
chromedriver_version=LATEST
```
Another option is to use env variable `CHROMEDRIVER_VERSION`.
```shell
CHROMEDRIVER_VERSION=LATEST npm install chromedriver
```
## A Note on chromedriver
Chromedriver is not a library for NodeJS.
This is an _NPM wrapper_ and can be used to conveniently make ChromeDriver available.
It is not a Node.js wrapper.
## Supported Node.js versions
We will do our best to support every supported Node.js versions.
See [nodejs/Release](https://github.com/nodejs/Release) for
the current supported versions. You can also view our build
scripts for
[Windows](https://github.com/giggio/node-chromedriver/blob/master/appveyor.yml)
and
[Linux and Mac](https://github.com/giggio/node-chromedriver/blob/master/.travis.yml).
## Contributing
Questions, comments, bug reports, and pull requests are all welcome. Submit them at
[the project on GitHub](https://github.com/giggio/node-chromedriver/).
Bug reports that include steps-to-reproduce (including code) are the
best. Even better, make them in the form of pull requests.
## Author
[Giovanni Bassi](https://github.com/giggio), with collaboration from
[lots of good people](https://github.com/giggio/node-chromedriver/graphs/contributors).
Thanks for Obvious and their PhantomJS project for heavy inspiration! Check their project on [Github](https://github.com/Obvious/phantomjs/tree/master/bin).
## License
Licensed under the Apache License, Version 2.0.
This diff is collapsed.
const fs = require('fs');
const path = require('path');
const tcpPortUsed = require('tcp-port-used');
function getPortFromArgs(args) {
let port = 9515;
if (!args){
return port;
}
const portRegexp = /--port=(\d*)/;
const portArg = args.find(function (arg) {
return portRegexp.test(arg);
});
if (portArg){
port = parseInt(portRegexp.exec(portArg)[1]);
}
return port;
}
process.env.PATH = path.join(__dirname, 'chromedriver') + path.delimiter + process.env.PATH;
exports.path = process.platform === 'win32' ? path.join(__dirname, 'chromedriver', 'chromedriver.exe') : path.join(__dirname, 'chromedriver', 'chromedriver');
exports.version = '2.46';
exports.start = function(args, returnPromise) {
let command = exports.path;
if (!fs.existsSync(command)) {
console.log('Could not find chromedriver in default path: ', command);
console.log('Falling back to use global chromedriver bin');
command = process.platform === 'win32' ? 'chromedriver.exe' : 'chromedriver';
}
const cp = require('child_process').spawn(command, args);
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
exports.defaultInstance = cp;
if (!returnPromise) {
return cp;
}
const port = getPortFromArgs(args);
const pollInterval = 100;
const timeout = 10000;
return tcpPortUsed.waitUntilUsed(port, pollInterval, timeout)
.then(function () {
return cp;
});
};
exports.stop = function () {
if (exports.defaultInstance != null){
exports.defaultInstance.kill();
}
};
This diff is collapsed.
{
"_args": [
[
"[email protected]",
"C:\\Users\\ts-shijia.a.yu\\IdeaProjects\\jpp-qa_automation_all_nodejs_new"
]
],
"_from": "[email protected]",
"_id": "[email protected]",
"_inBundle": false,
"_integrity": "sha512-dLtKIJW3y/PuFrPmcw6Mb8Nh+HwSqgVrK1rWgTARXhHfWvV822X2VRkx2meU/tg2+YQL6/nNgT6n5qWwIDHbwg==",
"_location": "/chromedriver",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "[email protected]",
"name": "chromedriver",
"escapedName": "chromedriver",
"rawSpec": "2.46.0",
"saveSpec": null,
"fetchSpec": "2.46.0"
},
"_requiredBy": [
"/"
],
"_resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-2.46.0.tgz",
"_spec": "2.46.0",
"_where": "C:\\Users\\ts-shijia.a.yu\\IdeaProjects\\jpp-qa_automation_all_nodejs_new",
"author": {
"name": "Giovanni Bassi",
"email": "[email protected]",
"url": "http://www.lambda3.com.br"
},
"bin": {
"chromedriver": "./bin/chromedriver"
},
"bugs": {
"url": "https://github.com/giggio/node-chromedriver/issues"
},
"dependencies": {
"del": "^3.0.0",
"extract-zip": "^1.6.7",
"mkdirp": "^0.5.1",
"request": "^2.88.0",
"tcp-port-used": "^1.0.1"
},
"description": "ChromeDriver for Selenium",
"homepage": "https://github.com/giggio/node-chromedriver",
"keywords": [
"chromedriver",
"selenium"
],
"license": "Apache-2.0",
"main": "lib/chromedriver",
"name": "chromedriver",
"repository": {
"type": "git",
"url": "git://github.com/giggio/node-chromedriver.git"
},
"scripts": {
"install": "node install.js"
},
"version": "2.46.0"
}
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env node
const path = require("path");
const spawn = require("child_process").spawn;
const binPath = require(path.join(__dirname, "..", "lib", "chromedriver")).path;
const args = process.argv.slice(2);
const cp = spawn(binPath, args);
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
cp.on("exit", process.exit);
process.on("SIGTERM", function() {
cp.kill("SIGTERM");
process.exit(1);
});
\ No newline at end of file
This diff is collapsed.
const fs = require('node:fs');
const path = require('node:path');
const tcpPortUsed = require('tcp-port-used');
function getPortFromArgs(args) {
let port = 9515;
if (!args)
return port;
const portRegexp = /--port=(\d*)/;
const portArg = args.find(function (arg) {
return portRegexp.test(arg);
});
if (portArg)
port = parseInt(portRegexp.exec(portArg)[1]);
return port;
}
process.env.PATH = path.join(__dirname, 'chromedriver') + path.delimiter + process.env.PATH;
const crpath = process.platform === 'win32' ? path.join(__dirname, 'chromedriver', 'chromedriver.exe') : path.join(__dirname, 'chromedriver', 'chromedriver');
const version = '125.0.6422.141';
let defaultInstance = null;
function start(args, returnPromise) {
let command = crpath;
if (!fs.existsSync(command)) {
console.log('Could not find chromedriver in default path: ', command);
console.log('Falling back to use global chromedriver bin');
command = process.platform === 'win32' ? 'chromedriver.exe' : 'chromedriver';
}
const cp = require('child_process').spawn(command, args);
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
defaultInstance = cp;
if (!returnPromise)
return cp;
const port = getPortFromArgs(args);
const pollInterval = 100;
const timeout = 10000;
return tcpPortUsed.waitUntilUsed(port, pollInterval, timeout)
.then(function () {
return cp;
});
}
function stop() {
if (defaultInstance != null)
defaultInstance.kill();
defaultInstance = null;
}
module.exports = {
path: crpath,
version,
start,
stop,
get defaultInstance() {
return defaultInstance;
}
};
{
"name": "chromedriver",
"version": "125.0.3",
"keywords": [
"chromedriver",
"selenium"
],
"description": "ChromeDriver for Selenium",
"homepage": "https://github.com/giggio/node-chromedriver",
"repository": {
"type": "git",
"url": "git://github.com/giggio/node-chromedriver.git"
},
"license": "Apache-2.0",
"author": {
"name": "Giovanni Bassi",
"email": "[email protected]",
"url": "http://www.lambda3.com.br"
},
"main": "lib/chromedriver",
"bin": {
"chromedriver": "bin/chromedriver"
},
"scripts": {
"install": "node install.js",
"update-chromedriver": "node update.js",
"typecheck": "tsc",
"test": "jest",
"test:ci": "jest --ci --reporters=default --reporters=jest-junit",
"lint": "eslint"
},
"dependencies": {
"@testim/chrome-version": "^1.1.4",
"axios": "^1.6.7",
"compare-versions": "^6.1.0",
"extract-zip": "^2.0.1",
"proxy-agent": "^6.4.0",
"proxy-from-env": "^1.1.0",
"tcp-port-used": "^1.0.2"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"semver": "^7.6.0",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment