2017-08-01 161 views
0

使用Cucumber和Selenium測試網站。在我的hooks.js文件中,我有以下內容:Selenium Webdriver Black屏幕截圖PhantomJS

driver.get("https://localhost:8000/"); 

sleep(2000); 

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver); 

var btn = this.driver.wait(selenium.until.elementLocated(By.css('#app > div > div > div.col-xs-6.textColumn > button'), seleniumTimeOut)); 

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver); 

this.driver.sleep(3000); 

這裏的目標是成功加載頁面並截取它。該網站正在運行本地主機。發生截圖時會出現問題。無論我有多長時間讓司機入睡,我都會得到一張黑色的截圖,表明我的網站沒有及時「建立」(根據情況使用可能不正確的術語)。如果我更改URL,以https://google.com/我得到了現場,沒有任何問題的屏幕截圖

Waiting for element to be located By(css selector, #app > div > div > div.col-xs-6.textColumn > button) 
Wait timed out after 20112ms 

:然後我得到這個錯誤。有什麼想法發生在這裏?我的上述假設是否正確? 在此先感謝!

回答

0

這個問題似乎已經認證的問題。測試將在http上運行,但當localhost使用https時,測試將失敗。 我的解決方法是在我的hooks.js文件中添加以下內容到this.BeforeFreature中:

this.driver = new selenium.Builder() 
     .withCapabilities({'phantomjs.cli.args': ['--ignore-ssl-errors=true']}) 
     .forBrowser('phantomjs') 
     .build(); 
0

請嘗試等待其他一些元素可用。使用xpath定位器代替CSS。你的機器是否代理?如果意味着請在代理後面添加幻像瀏覽器。

var phantom = require('phantom'); 
phantom.create(function(browser){ 
    browser.createPage(function(page){ 

    browser.setProxy('98.239.198.83','21320','http', null, null, function(){ 

    page.open(
     'http://example.com/req.php', function() { 

     });});});}); 
0

首先,將驅動程序更改爲chrome驅動程序,並在更改URL後查看腳本正在執行的操作。不同的環境有一段時間會有不同的id和XPath影響腳本。因此,在直接轉到phantomjs之前,請先使用chrome或firefox等常見驅動程序檢查腳本的行爲。

我已經經歷了相同的情況,我也經歷過相同的情況。這是一個愚蠢的錯誤這是浪費了我一半的一天:P

希望它會幫助你:)