2016-11-10 12 views
2

問題:私はアプリケーションにログインするCasperjsスクリプトを作成しています。アプリケーションはgoogle authを使用します。ユーザーがどのようにサインインするかをシミュレートしようとしているので、サイトに行くと、まずユーザーはGoogleのサインインボタンをクリックし、新しいタブでGoogleの資格情報を要求します。私はこれらの作品が動作している...私がついているところは、ユーザーがGoogleの認証フォームを提出し、サインインタブが閉じて元のウィンドウがそれらのクレデンシャルを受け取り、ユーザーをアプリケーションに入れることが期待できるということです。これらのイベントの後にスクリーンショットを撮ると、アプリにアクセスするのではなく、元のログインページに残ります。それは、私はそれが本当にユーザーでログインしていないと思われるので...つかむためにそれを言ったセレクタを見つけることができないので、私は、スクリプトがタイムアウトしますthis.waitForSelector('.dashboard-container',ラインに到達するとCasperjs 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(); 

:私のコードは、下に掲載されています。 (また、アプリケーションは、重要なことをする反応アプリケーションです)

私はしばらくの間この車に私の車輪を回転されている、任意の洞察力は非常に感謝される!

+3

どのバージョンのphantomJS/CasperJSをお使いですか? –

+2

エラーが記録されましたか? – Vaviloff

+2

phantomjs:2.1.1、casperjs:1.1.3 – jallen

答えて

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();