08-27-周三_17-09-29

This commit is contained in:
2025-08-27 17:10:05 +08:00
commit 86df397d8f
12735 changed files with 1145479 additions and 0 deletions

99
node_modules/phantom/test/adv.coffee generated vendored Normal file
View File

@@ -0,0 +1,99 @@
vows = require 'vows'
assert = require 'assert'
express = require 'express'
phantom = require '../phantom'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
# Make coffeescript not return anything
# This is needed because vows topics do different things if you have a return
# value
t = (fn) ->
->
fn.apply this, arguments
return
app = express()
app.use express.static __dirname
app.get '/', (req, res) ->
res.send """
<html>
<head>
<title>Test page title</title>
</head>
<body>
<img src="/test.gif" />
</body>
</html>
"""
appServer = app.listen()
describe "The phantom module (adv)",
"Can create an instance with --load-images=no":
topic: t ->
phantom.create '--load-images=no', (ph) =>
@callback null, ph
"which, when you open a page":
topic: t (ph) ->
ph.createPage (page) =>
page.open "http://127.0.0.1:#{appServer.address().port}/", (status) =>
setTimeout =>
@callback null, page, status
, 1500
"and check the settings object":
topic: t (page, status) ->
page.get 'settings', (s) =>
@callback null, s
"loadImages isn't set": (s) ->
assert.strictEqual s.loadImages, false
"succeeds": (err, page, status) ->
assert.equal status, 'success'
"and check a test image":
topic: t (page) ->
page.evaluate (-> document.getElementsByTagName('img')[0]), (img) =>
@callback null, img
"it doesn't load": (img) ->
assert.strictEqual img.width, 0, "width should be 0"
assert.strictEqual img.height, 0, "height should be 0"
teardown: (ph) ->
appServer.close()
ph.exit()
"Can create an instance with a custom port and --load-images=yes":
topic: t ->
phantom.create '--load-images=yes', {port: 12301}, (ph) =>
port = 12301
@callback null, port
"which loads on the correct port": (port) ->
assert.equal port, 12301
"Can create an instance with load-images: no in an args object":
topic: t ->
phantom.create {parameters: {'load-images': 'no'}}, (ph) =>
@callback null, ph
"which, when you open a page":
topic: t (ph) ->
ph.createPage (page) =>
page.open "http://127.0.0.1:#{appServer.address().port}/", (status) =>
setTimeout =>
@callback null, page, status
, 1500
"and check the settings object":
topic: t (page, status) ->
page.get 'settings', (s) =>
@callback null, s
"loadImages isn't set": (s) ->
assert.strictEqual s.loadImages, false

57
node_modules/phantom/test/basic.coffee generated vendored Normal file
View File

@@ -0,0 +1,57 @@
vows = require 'vows'
assert = require 'assert'
phantom = require '../phantom'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
# Make coffeescript not return anything
# This is needed because vows topics do different things if you have a return
# value
t = (fn) ->
->
fn.apply this, arguments
return
describe "The phantom module (basic)",
"Can create an instance":
topic: t ->
phantom.create {port: 12302}, (ph, err) =>
@callback null, [ph, err]
"which is an object": ([ph, err]) ->
assert.isObject ph
"which did not error": ([ph, err]) ->
assert.isNull err
"with a version":
topic: t ([ph, err]) ->
ph.get 'version', (val) =>
@callback null, val
"defined": (ver) ->
assert.notEqual ver, undefined
"an object": (ver) ->
assert.isObject ver
"greater than or equal to 1.3": (ver) ->
assert.ok ver.major >= 1, "major version too low"
if (ver.major is 1)
assert.ok ver.minor >= 3, "minor version too low"
"which can inject Javascript from a file":
topic: t ([ph, err]) ->
ph.injectJs 'test/inject.js', (success) =>
@callback null, success
"and succeed": (success) ->
assert.ok success, "Injection should return true"
"which can create a page":
topic: t ([ph, err]) ->
ph.createPage (page) =>
@callback null, page
"which is an object": (page) ->
assert.isObject page

78
node_modules/phantom/test/callbacks.coffee generated vendored Normal file
View File

@@ -0,0 +1,78 @@
vows = require 'vows'
assert = require 'assert'
phantom = require '../phantom'
Promise = require 'bluebird'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
# Make coffeescript not return anything
# This is needed because vows topics do different things if you have a return
# value
t = (fn) ->
->
fn.apply this, arguments
return
# Inject an `onExit` callback on `create` to resolve this promise.
exitPromise = new Promise (resolve) ->
wrapCreate = (p) ->
_cached = p.create
wrapped = false
p.create = (args...) ->
for arg, idx in args when typeof arg is 'object'
args[idx]['onExit'] = resolve
wrapped = true
break
args.push {onExit: resolve} unless wrapped is true
_cached.apply phantom, args
wrapCreate phantom
describe "The phantom module (callbacks)",
"Can create an instance":
topic: t ->
phantom.create {port: 12305}, (ph) =>
@callback null, ph
"and can add cookies":
topic: t (ph) ->
ph.addCookie
name: "cookieName"
value: "cookieValue"
path: "/testPath"
domain: "localhost", (status) =>
@callback null, status
"which succeeds": (status) ->
assert.ok status, "addCookie should succeed"
"and, when getCookies is called,":
topic: t (ph) ->
ph.getCookies (cookies) =>
@callback null, cookies
"the cookie is available": (cookies) ->
assert.equal (c for c in cookies when (c) ->
c.name is "cookieName" and
c.value is "cookieValue" and
c.path is "/testPath").length, 1, "cookie must be in phantom.cookies"
"which, when you call exit()":
topic: t (ph) ->
countdown = null
exitPromise.then =>
clearTimeout countdown
@callback null, 'success'
ph.exit()
countdown = setTimeout =>
@callback 'timeout'
, 500
"runs the onExit callback within 500ms": (status) ->
assert.equal status, 'success'

96
node_modules/phantom/test/closing.coffee generated vendored Normal file
View File

@@ -0,0 +1,96 @@
vows = require 'vows'
child_process = require 'child_process'
exec = child_process.exec
spawn = child_process.spawn
assert = require 'assert'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
t = (fn) ->
->
fn.apply this, arguments
return
program = '''
var phantom = require('./');
process.on('SIGINT', function() {
console.log('SIGINT');
process.exit(0);
});
process.on('SIGTERM', function() {
console.log('SIGTERM');
process.exit(0);
});
process.on('exit', function() {
console.log('EXIT');
});
console.log('Setup');
setTimeout(function() {
console.log('Going out');
}, 1000);
'''
programCbless = '''
var phantom = require('./');
console.log('Setup');
setTimeout(function() {
console.log('Going out');
}, 200);
'''
createTopic = (signal, p) ->
->
that = this
result = ''
co = child_process.exec 'node -e "' + p + '"'
cb = ->
if signal
cb = ->
process.kill co.pid, signal
else
cb = ->
co.stdout.on 'data', (data) ->
result += data
cb() if data.toString().match /^Setup/g
co.stderr.on 'data', (data) ->
result += data
co.on 'exit', (code) ->
that.callback null, [result, co.pid]
return undefined
createExitTest = (expect) ->
(err, [r, pid]) ->
assert.isNull err
assert.deepEqual('Setup\n' + expect, r)
createExitTestCbLess = (expect) ->
(err, [r, pid]) ->
assert.isNull err
assert.deepEqual('Setup\n' + expect, r)
try
process.kill(pid)
assert.fail()
describe "The phantom module",
"SIGINT":
"with callbacks":
topic: createTopic('SIGINT', program)
"exited": createExitTest('SIGINT\nEXIT\n')
"without callbacks":
topic: createTopic('SIGINT', programCbless)
"exited": createExitTestCbLess('')
"SIGTERM":
"with callbacks":
topic: createTopic('SIGTERM', program)
"exited": createExitTest('SIGTERM\nEXIT\n')
"without callbacks":
topic: createTopic('SIGTERM', programCbless)
"exited": createExitTestCbLess('')
"without signals":
"with callbacks":
topic: createTopic(false, program)
"exited": createExitTest('Going out\nEXIT\n')
"without callbacks":
topic: createTopic(false, programCbless)
"exited": createExitTestCbLess('Going out\n')

53
node_modules/phantom/test/error.coffee generated vendored Normal file
View File

@@ -0,0 +1,53 @@
vows = require 'vows'
assert = require 'assert'
phantom = require '../phantom'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
# Make coffeescript not return anything
# This is needed because vows topics do different things if you have a return
# value
t = (fn) ->
->
fn.apply this, arguments
return
describe "The phantom module (error behavior)",
"Can try to spawn instance at nonexistent path":
topic: t ->
phantom.create {port: 12311, path: "./test/doesn't-exist"}, (ph, err) =>
@callback null, [ph, err]
"which is null": ([ph, err]) ->
assert.isNull ph
"which returned an error": ([ph, err]) ->
assert.isObject err
"with an error code of ENOENT": ([ph, err]) ->
assert.strictEqual err?.code, "ENOENT"
"Can try to spawn two instances on the same port":
topic: t ->
phantom.create {port: 12312}, (ph, err) =>
@callback null, [ph, err]
"where the first succeeds": ([ph, err]) ->
assert.isObject ph
"and does not error": ([ph, err]) ->
assert.isNull err
"but, when the second is run,":
topic: t ->
phantom.create {port: 12312}, (ph2, err2) =>
@callback null, [ph2, err2]
"it fails": ([ph2, err2]) ->
assert.isNull ph2
"and it errors": ([ph2, err2]) ->
assert.isObject err2
"with error code EADDRINUSE": ([ph2, err2]) ->
assert.equal err2.code, "EADDRINUSE"

93
node_modules/phantom/test/frames.coffee generated vendored Normal file
View File

@@ -0,0 +1,93 @@
vows = require 'vows'
assert = require 'assert'
express = require 'express'
phantom = require '../phantom'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
# Make coffeescript not return anything
# This is needed because vows topics do different things if you have a return
# value
t = (fn) ->
->
fn.apply this, arguments
return
app = express()
app.use express.static __dirname
app.get '/', (req, res) ->
res.send """
<html>
<head>
<title>Test page title</title>
</head>
<body>
<iframe src="/inner" name="inner"></iframe>
</body>
</html>
"""
app.get '/inner', (req, res) ->
res.send """
<html>
<head>
<title>Inner page title</title>
</head>
<body>
</body>
</html>
"""
appServer = app.listen()
describe "The phantom module (frames)",
"Can switch to inner frame on the page":
topic: t ->
phantom.create (ph) =>
@callback null, ph
"which, when you open a page":
topic: t (ph) ->
ph.createPage (page) =>
page.open "http://127.0.0.1:#{appServer.address().port}/", (status) =>
setTimeout =>
@callback null, page, status
, 1500
"and extract the inner frame's title":
topic: t (page, status) ->
page.switchToFrame("inner")
page.evaluate (-> document.title), (title) =>
@callback null, title
"it is correct": (title) ->
assert.equal title, "Inner page title"
"and switch back to parent frame and extract the title":
topic: t (page, status) ->
page.switchToParentFrame()
page.evaluate (-> document.title), (title) =>
@callback null, title
"it is correct": (title) ->
assert.equal title, "Test page title"
"and switch from inner frame to main frame and extract the title":
topic: t (page, status) ->
page.switchToFrame("inner")
page.switchToMainFrame()
page.evaluate (-> document.title), (title) =>
@callback null, title
"it is correct": (title) ->
assert.equal title, "Test page title"
"succeeds": (err, page, status) ->
assert.equal status, 'success'
teardown: (ph) ->
appServer.close()
ph.exit()

2
node_modules/phantom/test/inject.js generated vendored Normal file
View File

@@ -0,0 +1,2 @@
//This isn't really a very good test
//(function(){var foo = 3;})();

207
node_modules/phantom/test/page.coffee generated vendored Normal file
View File

@@ -0,0 +1,207 @@
vows = require 'vows'
assert = require 'assert'
phantom = require '../phantom'
express = require 'express'
temp = require 'temp'
path = require 'path'
fs = require 'fs'
describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
# Make coffeescript not return anything
# This is needed because vows topics do different things if you have a return
# value
t = (fn) ->
->
fn.apply this, arguments
return
app = express()
app.get '/', (req, res) ->
res.send """
<html>
<head>
<title>Test page title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="somediv">
<div class="anotherdiv">Some page content</div>
</div>
<button class="clickme" style="position: absolute; top: 123px; left: 123px; width: 20px; height; 20px" onclick="window.i_got_clicked = true;" />
<input type='file' id='upfile' name='upfile'>
</body>
</html>
"""
appServer = app.listen()
describe "Pages",
"A Phantom page":
topic: t ->
phantom.create {port: 12303}, (ph) =>
ph.createPage (page) =>
@callback null, page, ph
"can open a URL on localhost":
topic: t (page) ->
page.open "http://127.0.0.1:#{appServer.address().port}/", (status) =>
@callback null, page, status
"and succeed": (err, page, status) ->
assert.equal status, "success"
"and the page, once it loads,":
topic: t (page, status) ->
setTimeout =>
@callback null, page
, 1500
"has a title":
topic: t (page) ->
page.evaluate (-> document.title), (title) =>
@callback null, title
"which is correct": (title) ->
assert.equal title, "Test page title"
"has cookies":
topic: t (page) ->
page.getCookies (cookies) =>
@callback null, cookies
"which works correctly": (cookies) ->
assert.ok cookies, "cookies should not be empty"
"can inject Javascript from a file":
topic: t (page) ->
page.injectJs 'test/inject.js', (success) =>
@callback null, success
"and succeed": (success) ->
assert.ok success, "Injection should return true"
"can evaluate DOM nodes":
topic: t (page) ->
page.evaluate (-> document.getElementById('somediv')), (node) =>
@callback null, node
"which match": (node) ->
assert.equal node.tagName, 'DIV'
assert.equal node.id, 'somediv'
"can evaluate scripts defined in the header":
topic: t (page) ->
page.evaluate (-> $('#somediv').html()), (html) =>
@callback null, html
"which return the correct result": (html) ->
html = html.replace(/\s\s+/g, "")
assert.equal html, '<div class="anotherdiv">Some page content</div>'
"can set a nested property":
topic: t (page) ->
page.set 'settings.loadPlugins', true, (oldVal) =>
@callback null, page, oldVal
"and get it again":
topic: t (page, oldVal) ->
page.get 'settings.loadPlugins', (val) =>
@callback null, oldVal, val
"and they match": (err, oldVal, val) ->
assert.equal oldVal, val
"can simulate clicks on page locations":
topic: t (page) ->
page.sendEvent 'click', 133, 133
page.evaluate (-> window.i_got_clicked), (clicked) =>
@callback null, clicked
"and have those clicks register": (clicked) ->
assert.ok clicked
###
"can register an onAlert handler":
topic: t (page) ->
page.set 'onAlert', (msg) =>
@callback null, msg
page.evaluate (-> alert "Hello, world!")
"which works correctly": (msg) ->
assert.equal msg, "Hello, world!"
###
###
"can register an onConsoleMessage handler":
topic: t (page) ->
page.set 'onConsoleMessage', (msg) =>
@callback null, msg
page.evaluate (-> console.log "Hello, world!")
"which works correctly": (msg) ->
assert.equal msg, "Hello, world!"
###
"can register an onConsoleMessage handler":
topic: t (page) ->
page.onConsoleMessage (msg) =>
@callback null, msg
page.evaluate (-> console.log "Hello, world!")
"which works correctly": (msg) ->
assert.equal msg, "Hello, world!"
"can register an onError handler":
topic: t (page) ->
page.onError (msg) =>
@callback null, msg
page.evaluate (-> eval "syntaxerror[")
"which works correctly": (msg) ->
assert.equal msg, "SyntaxError: Parse error"
"can render the page to a file":
topic: t (page) ->
fileName = temp.path suffix: '.png'
page.render fileName, =>
@callback null, fileName
"which is created": (fileName) ->
assert.ok fs.existsSync(fileName), "rendered image should exist"
teardown: (fileName) ->
fs.unlink fileName
# handles clicking on 'input[type=file]'. Based on
# https://github.com/ariya/phantomjs/blob/eddb0db/test/webpage-spec.js
"can set the file to upload when the file picker is invoked":
topic: t (page) ->
fileToUpload = (
if /^win/.test(process.platform)
'C:\\Windows\\System32\\drivers\\etc\\hosts'
else
'/etc/hosts'
)
page.setFileOnPicker fileToUpload
page.evaluate (->
upFile = document.querySelector('input[name=upfile]')
ev = document.createEvent('MouseEvents')
ev.initEvent('click', true, true)
upFile.dispatchEvent(ev)
), => @callback null, page, fileToUpload
"which":
topic: t (page, fileToUpload) ->
page.evaluate (->
document.querySelector('input[name=upfile]').files[0].name
), (fileName) =>
@callback null, fileName, fileToUpload
"matches with the passed filename": (err, fileName, fileToUpload) ->
assert.ok fileToUpload.indexOf(fileName) > -1
teardown: (page, ph) ->
appServer.close()
ph.exit()

BIN
node_modules/phantom/test/test.gif generated vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB