Javascript Casperjs谷歌认证弹出窗口

Javascript Casperjs谷歌认证弹出窗口,javascript,reactjs,popup,phantomjs,casperjs,Javascript,Reactjs,Popup,Phantomjs,Casperjs,问题是:我正在编写一个Casperjs脚本,它将登录到一个应用程序中。该应用程序使用googleauth。我试图模拟用户登录的方式,因此在访问该站点时,用户首先单击“使用谷歌登录”按钮,打开一个新选项卡,询问谷歌凭据。我有这些工作。。。当用户提交GoogleAuth表单时,我被卡住了,期望登录选项卡关闭,原始窗口接收这些凭据并允许用户进入应用程序。如果我在所有这些事件之后进行屏幕截图,我仍然会停留在原始登录页面上,而不会访问应用程序。我的代码发布在下面: var casper = require

问题是:我正在编写一个Casperjs脚本,它将登录到一个应用程序中。该应用程序使用googleauth。我试图模拟用户登录的方式,因此在访问该站点时,用户首先单击“使用谷歌登录”按钮,打开一个新选项卡,询问谷歌凭据。我有这些工作。。。当用户提交GoogleAuth表单时,我被卡住了,期望登录选项卡关闭,原始窗口接收这些凭据并允许用户进入应用程序。如果我在所有这些事件之后进行屏幕截图,我仍然会停留在原始登录页面上,而不会访问应用程序。我的代码发布在下面:

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();
var casper=require('casper')。创建({
没错,
日志级别:“调试”,
等待超时:5000,
用户代理:“Mozilla/5.0(Macintosh;英特尔Mac OS X 10_7_5)AppleWebKit/537.4(KHTML,类似Gecko)Chrome/22.0.1229.94 Safari/537.4”
});
casper.capturePath=函数(名称){
返回此.capture('./captures/'+名称)
}
casper.on('remote.message',函数(msg){
this.echo('捕获的远程消息:'+msg);
});
casper.on(“page.error”,函数(msg,trace){
echo(“页面错误:+msg,“错误”);
});
casper.on('popup.created',function(){
this.echo(“创建的url弹出窗口:“+this.getCurrentUrl(),“INFO”);
});
casper.on('popup.loaded',function(){
this.echo(“加载的url弹出窗口:“+this.getCurrentUrl(),“INFO”);
});
卡斯珀
.start(“”,函数(){
这
.然后(函数(){
点击标签('用谷歌登录','按钮');
})
.waitForPopup(/accounts\.google/)
.withPopup(/accounts\.google/,函数(popup){
这
.fillSelectors('form#gaia#u loginform',{#Email':'',false)
。然后单击('输入#下一步')
.等待(500,函数(){
this.waitForSelector(“#Passwd”,
函数成功(){
这
.echo('success','INFO')
.fillSelectors('form#gaia_loginform',{'input[name=Passwd]':''},false)
.capturePath('beforesmit.png')
。然后单击('input#SIGN')
.等待(500,函数(){
this.capturePath('afterSubmit.png');
})
},
函数失败(){
这个.echo('failure');
})
})
})
})
.然后(函数(){
此.waitForSelector(“.dashboard容器”,
函数成功(){
这
.echo('已登录!','INFO')
.capturePath('in.png')
},
函数失败(){
这
.capturePath('failed.png')
.echo('登录失败','错误');
})
})
.run();
当我到达
this.waitForSelector(“.dashboard container”
行时,脚本将超时,因为它找不到我告诉它抓取的选择器……可能是因为它没有真正登录用户。(此外,该应用程序是一个React应用程序,这一点很重要)


我已经在这台机器上转了一段时间了,如果有任何见解,我将不胜感激。

我认为它现在可以工作了。问题是,您试图
在主页上的弹出窗口之外使用这个.waitForSelector(“.dashboard container”。

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',
    viewportSize:{width: 1600, height: 900}
});
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(newPage) {
this.echo("url popup created : " + this.getCurrentUrl(),"INFO");
newPage.viewportSize={width:1600,height:900}
});

casper.on('error', function(msg) {
this.echo('Error: ' + msg,"ERROR");
});// You have missed this callback!

casper.on('popup.loaded', function() {
    this.echo("url popup loaded : " + this.getCurrentUrl(),"INFO");
});

casper
    .start('http://domu-test-2/node/10', function() {
        this
            .wait(0,function() {// 'then(function' won't work as expected in any callback function.
                this.clickLabel('Sign in with Google', 'button');
            })
            .waitForPopup(/accounts\.google/)
            .withPopup(/accounts\.google/, function(popup) { 
                this
                    .fillSelectors('form#gaia_loginform', { '#Email': 'luxadm1' }, false)
                    .thenClick('input#next')
                    .wait(700, function() {
                        this.waitForSelector('#Passwd',
                            function success() {
                                this
                                    .echo('success', 'INFO')
                                    .fillSelectors('form#gaia_loginform', { 'input[name=Passwd]': '<pass_here>' }, false)
                                    .capturePath('beforeSubmit.png')
                                    .thenClick('input#signIn')
                                    .wait(300, function() {// less than previous '.wait(700, function() {' -- otherwise will be buggy
                                        this.capturePath('afterSubmit.png');
                                    })
                                },
                                function fail() {
                                    this.echo('failure');
                                })
                    })
            })
    })
    .then(function(){//here outside of the popup!!
        this.withPopup(/accounts\.google/, function(popup){// we need to be here until the previous '.withPopup' function will switch to 'about:blank', otherwise we will get an error: 'CasperError: Couldn't find popup with url matching pattern'
        this
       /*.wait(3000,*/ .waitForSelector('div.sPxS6d',//'.dashboard-container' -- i've not seen such selector there  
            function success() {
                this
                    .echo('logged in!', 'INFO')
                    .capturePath('in.png')
            },
            function fail() {
                this
                    .capturePath('failed.png')
                    .echo('failed to login', 'ERROR');
            })
    });})
.run();
var casper=require('casper')。创建({
没错,
日志级别:“调试”,
等待超时:5000,
用户代理:“Mozilla/5.0(Macintosh;英特尔Mac OS X 10_7_5)AppleWebKit/537.4(KHTML,类似Gecko)Chrome/22.0.1229.94 Safari/537.4”,
视口大小:{宽度:1600,高度:900}
});
casper.capturePath=函数(名称){
返回此.capture('./captures/'+名称)
}
casper.on('remote.message',函数(msg){
this.echo('捕获的远程消息:'+msg);
});
casper.on(“page.error”,函数(msg,trace){
echo(“页面错误:+msg,“错误”);
});
casper.on('popup.created',函数(newPage){
this.echo(“创建的url弹出窗口:“+this.getCurrentUrl(),“INFO”);
viewportSize={宽度:1600,高度:900}
});
casper.on('error',函数(msg){
this.echo('Error:'+msg,“Error”);
});//您错过了这次回调!
casper.on('popup.loaded',function(){
this.echo(“加载的url弹出窗口:“+this.getCurrentUrl(),“INFO”);
});
卡斯珀
.开始('http://domu-test-2/node/10,函数(){
这
.wait(0,function(){/'),则(function)在任何回调函数中都不会按预期工作。
点击标签('用谷歌登录','按钮');
})
.waitForPopup(/accounts\.google/)
.withPopup(/accounts\.google/,函数(popup){
这
.fillSelectors('form#gaia#u loginform',{#Email':'luxadm1',false)
。然后单击('输入#下一步')
.wait(700,函数(){
this.waitForSelector(“#Passwd”,
函数成功(){
这
.echo('success','INFO')
.fillSelectors('form#gaia_loginform',{'input[name=Passwd]':''},false)
.capturePath('beforesmit.png')
。然后单击('input#SIGN')
.wait(300,函数(){//小于上一个'.wait(700,函数(){'--否则将出现错误
this.capturePath('afterSubmit.png');
})