Last active
February 11, 2017 11:09
-
-
Save inokappa/a11f7e975e07340903972b305f6aee24 to your computer and use it in GitHub Desktop.
簡単は HTML フォームのログイン画面にログインさせる 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
// | |
// phantomjs 2.1.1 にて確認済み | |
// $ phantomjs login_check.js username password | |
// | |
var system = require('system'); | |
var args = system.args; | |
var page = new WebPage(), testindex = 0, loadInProgress = false; | |
var url = 'http://127.0.0.1:3000/'; | |
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('ページ読み取り完了'); | |
}; | |
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() { | |
// Login | |
page.evaluate(function() { | |
console.log('ok') | |
}); | |
}, | |
function() { | |
// Logout | |
page.evaluate(function() { | |
var elem = document.getElementsByClassName('logout')[0].firstElementChild; | |
var e = document.createEvent('Events'); | |
e.initEvent('click', true, false); | |
elem.dispatchEvent(e); | |
}); | |
} | |
]; | |
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