【问题标题】:Casperjs google auth popupCasperjs 谷歌身份验证弹出窗口
【发布时间】: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


【解决方案1】:

我认为它现在有效。问题是,您实际上在主页上的弹出窗口之外尝试this.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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2020-08-28
    • 2019-05-15
    • 1970-01-01
    • 2017-12-24
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多