2013-02-21 76 views
0

我在Windows 8上使用PhantomJS 1.8.1下的CasperJS 1.0.2。網站無法按預期與CasperJS一起工作

試圖編寫網站測試。該網站嚴重依賴JS,編碼原則很不尋常,這可能會造成一些問題,但我不確定。

這裏是我使用的測試登錄和搜索功能的代碼:

var url = 'http://www.testsite.com/'; 

var casper = require('casper').create(); 

casper.start(); 

casper.start(url, function() { 
    this.echo('Page: ' + this.getTitle()); 
    this.capture('start.png'); 

    if (this.exists('input#TxtUserName')) { 
     this.sendKeys('input#TxtUserName', 'testlogin'); 
     this.sendKeys('input#TxtPassword', 'testpass'); 
     this.click('input#BtnLogin'); 
     this.capture('loggedin.png'); 
    } 
}); 

casper.then(function() { 
    this.capture('beforesearch.png'); 
    this.sendKeys("input#txtSearch", '1002'); 
    this.click("input#cmdSubmit"); 
    this.echo('Searching'); 
    this.capture('aftersearch.png'); 
}); 

casper.run(); 

當我運行此代碼,屏幕上的捕捉每一個頁面,不同的是登錄信息被填充相同在login.png上。在點擊事件後,它實際上沒有登錄(使用我的真實登錄憑據)。在點擊被觸發後,搜索結果也不會顯示。

任何線索可能導致這種情況?

這裏提交搜索後,我的WAITFOR代碼:

casper.waitForText("Part:", function() { 
    this.capture('searchresults.png'); 
}); 
+0

沒有關於waitFor編輯。在我進行測試的時候,網站的速度令人難以置信。 – TestyTester 2013-02-21 16:15:38

+0

可能與http://stackoverflow.com/questions/14098483/casperjs-click-event-having-ajax-call/15144137#15144137 看到我的答案那裏,可能會幫助你。 – Leentje 2013-02-28 19:53:06

回答

0

您應該使用casper.waitFor,以確保下一個頁面已經被加載。否則,幻影將在表單提交之前得到回覆。

+1

這可以讓它登錄併產生捕獲,但是,waitForText('Part:')'和'waitForSelector(「span:contains('Part:')」)'在等待搜索結果時超時加載。這幾乎就像'click(「input#Tgnameleftmenu3_cmdSubmit」)'實際上不會導致搜索提交。該頁面通常最多需要2秒才能加載。 – TestyTester 2013-02-21 15:53:43

相關問題