Last active
January 26, 2017 22:45
-
-
Save inokappa/a082979eec996d886e2ce145ba9392e3 to your computer and use it in GitHub Desktop.
PhantomJS を利用してログイン後のサイトを確認(キャプチャを取得)するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var system = require('system'); | |
var args = system.args; | |
var page = new WebPage(), testindex = 0, loadInProgress = false; | |
var url = 'http://127.0.0.1:4567/'; | |
var username = system.args[1]; | |
var password = system.args[2]; | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
page.onLoadStarted = function() { | |
loadInProgress = true; | |
// console.log('ページ読み取り開始'); | |
}; | |
page.onLoadFinished = function() { | |
loadInProgress = false; | |
// console.log('ページ読み取り完了'); | |
}; | |
page.onCallback = function() { | |
page.render('check.png'); | |
} | |
var steps = [ | |
function() { | |
// Load Login Page | |
page.open(url); | |
}, | |
function() { | |
// Enter Username and Password | |
page.evaluate(function(username, password) { | |
var form = document.getElementsByClassName('login_form'); | |
var i; | |
for (i=0; i < form.length; i++) { | |
form[i].elements['username'].value = username; | |
form[i].elements['password'].value = password; | |
return; | |
} | |
}, username, password); | |
}, | |
function() { | |
// Login | |
page.evaluate(function() { | |
var form = document.getElementsByClassName('login_form'); | |
var i; | |
for (i=0; i < form.length; i++) { | |
form[i].submit(); | |
} | |
}); | |
}, | |
function() { | |
// Create Screen Capture | |
// 取得した HTML 内に username が含まれていたらキャプチャを取得する | |
page.evaluate(function(username) { | |
var body = document.getElementsByClassName('username')[0].innerHTML; | |
if (body.indexOf(username) != -1) { | |
window.callPhantom(); | |
} | |
}, username); | |
} | |
]; | |
console.log('チェック開始.'); | |
interval = setInterval(function() { | |
if (!loadInProgress && typeof steps[testindex] == 'function') { | |
// console.log('step ' + (testindex + 1)); | |
steps[testindex](); | |
testindex++; | |
} | |
if (typeof steps[testindex] != 'function') { | |
console.log('チェック完了.'); | |
phantom.exit(); | |
} | |
}, 50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment