From 51963a34ba6593cdbfe208fa4569879a182bafe2 Mon Sep 17 00:00:00 2001 From: ossdev07 Date: Fri, 13 Dec 2019 12:33:51 +0530 Subject: [PATCH] [[FEAT]] Replacing PhantomJS with Puppeteer Signed-off-by: ossdev07 --- .travis.yml | 2 +- appveyor.yml | 2 +- dist/jshint-rhino.js | 16 ++++---- dist/jshint.js | 16 ++++---- examples/.jshintrc | 2 +- package.json | 3 +- src/jshint.js | 4 +- src/options.js | 4 +- src/state.js | 2 +- src/vars.js | 4 +- tests/browser.js | 59 ++++++--------------------- tests/helpers/browser/index.html | 32 --------------- tests/helpers/browser/run-all.js.tmpl | 15 ++----- tests/unit/envs.js | 14 +++---- tests/unit/options.js | 8 ++-- 15 files changed, 55 insertions(+), 128 deletions(-) diff --git a/.travis.yml b/.travis.yml index 825ee0c36d..65b434bfb0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ before_install: # they are only needed to verify correctness in a browser-like environment, # they can be removed (and the tests skipped) without effecting coverage. - if [[ $(node --version) != v10* ]] ; then - node scripts/remove-dev-dependencies.js phantom phantomjs-prebuilt; + node scripts/remove-dev-dependencies.js puppeteer; fi script: - npm run pretest diff --git a/appveyor.yml b/appveyor.yml index 49494f1ff9..1351b01d32 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -17,7 +17,7 @@ install: # These packages cannot be installed in legacy versions of Node.js. Because # they are only needed to verify correctness in a browser-like environment, # they can be removed (and the tests skipped) without effecting coverage. - - if NOT "%nodejs_version%" == "10" node scripts/remove-dev-dependencies.js phantom phantomjs-prebuilt + - if NOT "%nodejs_version%" == "10" node scripts/remove-dev-dependencies.js puppeteer - npm install build: off diff --git a/dist/jshint-rhino.js b/dist/jshint-rhino.js index a86007e5ea..12027eec2d 100755 --- a/dist/jshint-rhino.js +++ b/dist/jshint-rhino.js @@ -21946,12 +21946,12 @@ exports.bool = { /** * This option defines globals available when your core is running inside - * of the PhantomJS runtime environment. [PhantomJS](http://phantomjs.org/) + * of the Chrome runtime environment. [chrome](http://chromium.org/) * is a headless WebKit scriptable with a JavaScript API. It has fast and * native support for various web standards: DOM handling, CSS selector, * JSON, Canvas, and SVG. */ - phantom : true + chrome : true }, // Obsolete options @@ -23480,7 +23480,7 @@ var state = { * @returns {boolean} */ impliedClosure: function() { - return this.option.node || this.option.phantom || this.option.browserify; + return this.option.node || this.option.chrome || this.option.browserify; }, // Assumption: chronologically ES3 < ES5 < ES6 < Moz @@ -24287,8 +24287,8 @@ exports.browserify = { process : true }; -exports.phantom = { - phantom : true, +exports.chrome = { + chrome : true, require : true, WebPage : true, console : true, // in examples, but undocumented @@ -24847,8 +24847,8 @@ var JSHINT = (function() { combine(predefined, vars.typed); } - if (state.option.phantom) { - combine(predefined, vars.phantom); + if (state.option.chrome) { + combine(predefined, vars.chrome); } if (state.option.prototypejs) { @@ -31163,4 +31163,4 @@ if (typeof exports === "object" && exports) { JSHINT = require('jshint').JSHINT; if (typeof exports === 'object' && exports) exports.JSHINT = JSHINT; -}()); \ No newline at end of file +}()); diff --git a/dist/jshint.js b/dist/jshint.js index 2896074586..8a4ee94149 100644 --- a/dist/jshint.js +++ b/dist/jshint.js @@ -21944,12 +21944,12 @@ exports.bool = { /** * This option defines globals available when your core is running inside - * of the PhantomJS runtime environment. [PhantomJS](http://phantomjs.org/) + * of the Chrome runtime environment. [Chrome](http://chromium.org/) * is a headless WebKit scriptable with a JavaScript API. It has fast and * native support for various web standards: DOM handling, CSS selector, * JSON, Canvas, and SVG. */ - phantom : true + chrome : true }, // Obsolete options @@ -23478,7 +23478,7 @@ var state = { * @returns {boolean} */ impliedClosure: function() { - return this.option.node || this.option.phantom || this.option.browserify; + return this.option.node || this.option.chrome || this.option.browserify; }, // Assumption: chronologically ES3 < ES5 < ES6 < Moz @@ -24285,8 +24285,8 @@ exports.browserify = { process : true }; -exports.phantom = { - phantom : true, +exports.chrome = { + chrome : true, require : true, WebPage : true, console : true, // in examples, but undocumented @@ -24845,8 +24845,8 @@ var JSHINT = (function() { combine(predefined, vars.typed); } - if (state.option.phantom) { - combine(predefined, vars.phantom); + if (state.option.chrome) { + combine(predefined, vars.chrome); } if (state.option.prototypejs) { @@ -31161,4 +31161,4 @@ if (typeof exports === "object" && exports) { JSHINT = require('jshint').JSHINT; if (typeof exports === 'object' && exports) exports.JSHINT = JSHINT; -}()); \ No newline at end of file +}()); diff --git a/examples/.jshintrc b/examples/.jshintrc index f77978f74e..ec9fce8837 100644 --- a/examples/.jshintrc +++ b/examples/.jshintrc @@ -76,7 +76,7 @@ "mootools" : false, // MooTools "node" : false, // Node.js "nonstandard" : false, // Widely adopted globals (escape, unescape, etc) - "phantom" : false, // PhantomJS + "chrome" : false, // Chrome "prototypejs" : false, // Prototype and Scriptaculous "qunit" : false, // QUnit "rhino" : false, // Rhino diff --git a/package.json b/package.json index f8aab3dcb9..2a318b8fad 100644 --- a/package.json +++ b/package.json @@ -58,8 +58,7 @@ "jscs": "1.11.x", "mock-stdin": "0.3.x", "nodeunit": "0.9.x", - "phantom": "~4.0.1", - "phantomjs-prebuilt": "~2.1.7", + "puppeteer": "1.20.0", "regenerate": "1.2.x", "results-interpreter": "~1.0.0", "sinon": "1.12.x", diff --git a/src/jshint.js b/src/jshint.js index 17224bc886..fbc2942e59 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -285,8 +285,8 @@ var JSHINT = (function() { combine(predefined, vars.typed); } - if (state.option.phantom) { - combine(predefined, vars.phantom); + if (state.option.chrome) { + combine(predefined, vars.chrome); } if (state.option.prototypejs) { diff --git a/src/options.js b/src/options.js index f5599cdfe5..d7def93703 100644 --- a/src/options.js +++ b/src/options.js @@ -780,12 +780,12 @@ exports.bool = { /** * This option defines globals available when your core is running inside - * of the PhantomJS runtime environment. [PhantomJS](http://phantomjs.org/) + * of the Chrome runtime environment. [Chrome](http://chromium.org/) * is a headless WebKit scriptable with a JavaScript API. It has fast and * native support for various web standards: DOM handling, CSS selector, * JSON, Canvas, and SVG. */ - phantom : true + chrome : true }, // Obsolete options diff --git a/src/state.js b/src/state.js index 4a37a222fb..3398e5b363 100644 --- a/src/state.js +++ b/src/state.js @@ -56,7 +56,7 @@ var state = { * @returns {boolean} */ impliedClosure: function() { - return this.option.node || this.option.phantom || this.option.browserify; + return this.option.node || this.option.chrome || this.option.browserify; }, // Assumption: chronologically ES3 < ES5 < ES6 < Moz diff --git a/src/vars.js b/src/vars.js index c9a63ecd80..becadef326 100644 --- a/src/vars.js +++ b/src/vars.js @@ -532,8 +532,8 @@ exports.browserify = { process : true }; -exports.phantom = { - phantom : true, +exports.chrome = { + chrome : true, require : true, WebPage : true, console : true, // in examples, but undocumented diff --git a/tests/browser.js b/tests/browser.js index 3eb5693f14..9e63fc28a5 100644 --- a/tests/browser.js +++ b/tests/browser.js @@ -1,57 +1,24 @@ "use strict"; -var phantom, phantomJsPrebuilt; -try { - phantom = require("phantom"); - phantomJsPrebuilt = require("phantomjs-prebuilt"); -} catch (err) { - throw new Error( - "Unable to run tests in PhantomJS because the required dependencies are " + - "not available. Please note that JSHint does not support development " + - "using versions of Node.js which are no longer maintained." - ); -} - +const puppeteer = require('puppeteer'); var createTestServer = require("./helpers/browser/server"); -var options = { - /** - * The `phantom` module provides a Node.js API for the PhantomJS binary, - * while the `phantomjs` module includes the binary itself. - */ - phantomPath: phantomJsPrebuilt.path -}; var port = process.env.NODE_PORT || 8045; -var ph; - -phantom.create([], options) - .then(function(_ph) { - ph = _ph; - return ph.createPage(); - }) - .then(function(page) { - createTestServer(port, function(server) { - page.on("onConsoleMessage", function(str) { - console.log(str); - }); - - page.on("onCallback", function(err) { - ph.exit(); - server.close(); - if (err) { - process.exit(1); - } +const browser = puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }).then(async browser => { + const page = await browser.newPage(); + createTestServer(port, function(server) { + page.on('console', msg => console.log("", msg.text())); + page.exposeFunction('callChrome', text=> { + browser.close(); + process.exit(); }); page.on("onError", function(msg, trace) { console.error(msg); console.error(trace); - process.exit(1); + process.exit(); + }); + return page.goto("http://localhost:" + port, { + waitUntil: 'load' }); - - return page.open("http://localhost:" + port); }); - }) - .then(null, function(err) { - console.error(err); - process.exit(1); - }); +}); diff --git a/tests/helpers/browser/index.html b/tests/helpers/browser/index.html index cc03d3cea9..eac62bcc39 100644 --- a/tests/helpers/browser/index.html +++ b/tests/helpers/browser/index.html @@ -5,38 +5,6 @@ JSHint Browser Build Testing Server - diff --git a/tests/helpers/browser/run-all.js.tmpl b/tests/helpers/browser/run-all.js.tmpl index 8434160c66..7937efff8f 100644 --- a/tests/helpers/browser/run-all.js.tmpl +++ b/tests/helpers/browser/run-all.js.tmpl @@ -1,20 +1,13 @@ (function() { "use strict"; var nodeunit = require("nodeunit"); - var callPhantom = window.callPhantom; + var callChrome = window.callChrome; var tests; - /** - * `window.callPhantom` is used as the control flow communication channel - * between the document and the PhantomJS context (where available). Because - * of this, it should not be available to other scripts on the page. - */ - window.callPhantom = null; - /** * Nodeunit's "browser" reporter expects Nodeunit to be available on the * global scope. Expose it in all cases to promote consistency between the - * PhantomJS and "true" browser execution contexts. + * Chrome and "true" browser execution contexts. */ window.nodeunit = nodeunit; @@ -38,7 +31,7 @@ assertion_suffix: "\u001B[39m" }; nodeunit.reporters.default.run(tests, colors, function(err) { - callPhantom(err ? err : null); + callChrome(); }); } @@ -48,7 +41,7 @@ reporter.run(tests); } - if (/PhantomJS/.test(navigator.userAgent)) { + if (/Chrome/.test(navigator.userAgent)) { runHeadless(); } else { runHeadful(); diff --git a/tests/unit/envs.js b/tests/unit/envs.js index a064937f74..378cda221b 100644 --- a/tests/unit/envs.js +++ b/tests/unit/envs.js @@ -198,7 +198,7 @@ exports.es5 = function (test) { test.done(); }; -exports.phantom = function (test) { +exports.chrome = function (test) { // Phantom environment assumes `globalstrict` var globalStrict = [ '"use strict";', @@ -210,7 +210,7 @@ exports.phantom = function (test) { .test(globalStrict, { es3: true, strict: true }); TestRun(test) - .test(globalStrict, { es3: true, phantom: true, strict: true }); + .test(globalStrict, { es3: true, chrome: true, strict: true }); test.done(); @@ -230,7 +230,7 @@ exports.globals = function (test) { TestRun(test) .test(src, { node: true }); TestRun(test) - .test(src, { phantom: true }); + .test(src, { chrome: true }); TestRun(test, "Late configuration of `browserify`") .test([ @@ -276,17 +276,17 @@ exports.globals = function (test) { "var first;" ]); - TestRun(test, "Late configuration of `phantom`") + TestRun(test, "Late configuration of `chrome`") .test([ "/* global first */", "void 0;", - "// jshint phantom: true", + "// jshint chrome: true", "var first;" ]); TestRun(test) .test([ - "// jshint phantom: true", + "// jshint chrome: true", "/* global first */", "var first;" ]); @@ -294,7 +294,7 @@ exports.globals = function (test) { TestRun(test) .test([ "/* global first */", - "// jshint phantom: true", + "// jshint chrome: true", "var first;" ]); diff --git a/tests/unit/options.js b/tests/unit/options.js index 2f91f57e2a..17c224d3c7 100644 --- a/tests/unit/options.js +++ b/tests/unit/options.js @@ -2047,7 +2047,7 @@ exports.strict = function (test) { /** * This test asserts sub-optimal behavior. * - * In the "browserify", "node" and "phantomjs" environments, user code is not + * In the "browserify", "node" and "chrome" environments, user code is not * executed in the global scope directly. This means that top-level `use * strict` directives, although seemingly global, do *not* enable ES5 strict * mode for other scripts executed in the same environment. Because of this, @@ -2075,7 +2075,7 @@ exports.strictEnvs = function (test) { .test(partialStrict, { strict: true, node: true }); TestRun(test, "") .addError(2, 15, "Missing \"use strict\" statement.") - .test(partialStrict, { strict: true, phantom: true }); + .test(partialStrict, { strict: true, chrome: true }); partialStrict = [ '(() =>', @@ -2091,7 +2091,7 @@ exports.strictEnvs = function (test) { .test(partialStrict, { esversion: 6, strict: true, node: true }); TestRun(test, "Block-less arrow function in the PhantomJS environment") .addError(3, 1, "Missing \"use strict\" statement.") - .test(partialStrict, { esversion: 6, strict: true, phantom: true }); + .test(partialStrict, { esversion: 6, strict: true, chrome: true }); test.done(); }; @@ -2204,7 +2204,7 @@ exports.globalstrict = function (test) { .test(code, { strict: true, globalstrict: false, node: true }); TestRun(test, "co-occurence with internally-set 'strict: gobal' (Phantom.js code)") - .test(code, { strict: true, globalstrict: false, phantom: true }); + .test(code, { strict: true, globalstrict: false, chrome: true }); TestRun(test, "co-occurence with internally-set 'strict: gobal' (Browserify code)") .test(code, { strict: true, globalstrict: false, browserify: true });