【发布时间】:2016-11-10 22:45:59
【问题描述】:
问题:我正在编写一个将登录到应用程序的 Casperjs 脚本。该应用程序使用谷歌身份验证。我正在尝试模拟用户如何登录,因此访问该站点时,用户首先单击“使用 Google 登录”按钮,该按钮会打开一个新选项卡,要求输入 Google 凭据。我有这些工作......我卡住的地方是当用户提交谷歌身份验证表单时,期望登录选项卡关闭并且原始窗口接收这些凭据并允许用户进入应用程序。如果我在所有这些事件之后截屏,我仍然会留在原始登录页面上,而不是访问应用程序。我的代码贴在下面:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
waitTimeout: 5000,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
});
casper.capturePath = function(name) {
return this.capture('./captures/' + name)
}
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
casper.on('popup.created', function() {
this.echo("url popup created : " + this.getCurrentUrl(),"INFO");
});
casper.on('popup.loaded', function() {
this.echo("url popup loaded : " + this.getCurrentUrl(),"INFO");
});
casper
.start('<url>', function() {
this
.then(function() {
this.clickLabel('Sign in with Google', 'button');
})
.waitForPopup(/accounts\.google/)
.withPopup(/accounts\.google/, function(popup) {
this
.fillSelectors('form#gaia_loginform', { '#Email': '<username>' }, false)
.thenClick('input#next')
.wait(500, function() {
this.waitForSelector('#Passwd',
function success() {
this
.echo('success', 'INFO')
.fillSelectors('form#gaia_loginform', { 'input[name=Passwd]': '<password>' }, false)
.capturePath('beforeSubmit.png')
.thenClick('input#signIn')
.wait(500, function() {
this.capturePath('afterSubmit.png');
})
},
function fail() {
this.echo('failure');
})
})
})
})
.then(function() {
this.waitForSelector('.dashboard-container',
function success() {
this
.echo('logged in!', 'INFO')
.capturePath('in.png')
},
function fail() {
this
.capturePath('failed.png')
.echo('failed to login', 'ERROR');
})
})
.run();
当我到达this.waitForSelector('.dashboard-container', 行时,脚本将超时,因为它找不到我告诉它抓取的选择器......大概是因为它没有真正登录用户。 (如果这很重要,该应用程序也是一个 React 应用程序)
我已经在这个上旋转了一段时间,任何见解都将不胜感激!
【问题讨论】:
-
你用的是什么版本的phantomJS/CasperJS?
-
记录了任何错误?
-
phantomjs:2.1.1,casperjs:1.1.3
-
Page Error: TypeError: undefined is not a constructor (evaluating 'Object.assign({}, state, { instant: [].concat(_toConsumableArray(state.instant), [action.data]) })'),我认为出现此错误是因为我正在等待它找不到的选择器
标签: javascript reactjs popup phantomjs casperjs