2014-04-08 55 views
4

我開始爲網站應用程序開發測試,並遇到一些問題。Selenium Webdriver(node.js)截取屏幕並保存測試結果

我使用Node.js,webdriver,chromedriver和selenium rc。

問題是: 1.如何製作屏幕截圖並將其保存在與腳本相同的文件夾中。 2.有沒有辦法保存測試用例的測試日誌?例如,如果檢查頁面上的某個元素,但沒有找到它,我該如何輸出該元素?

+1

不要問兩個問題一次 - 這使得它更復雜,因此對其他人不太有用。你可以例如通過刪除問題的「如何保存測試日誌」部分來修復此問題。 – oberlies

回答

16

爲了保存測試日誌,您通常使用測試運行器。當你檢查一個元素是否在頁面上,並且你找不到它時,你會引發一個異常(通常是一個斷言錯誤),測試運行器會記錄這個異常並將其標記爲失敗的測試。 In the documentation they suggest you use Mocha

至於保存截圖到磁盤中,the api看起來像這樣

driver.takeScreenshot().then(
    function(image, err) { 
     require('fs').writeFile('out.png', image, 'base64', function(err) { 
      console.log(err); 
     }); 
    } 
); 
+0

好像它不工作... 我使用硒webdriver,而不是webdriverjs。不知道他們之間是否有區別。 – paz

+0

看起來不像它:https://code.google.com/p/selenium/wiki/WebDriverJs#Installing_from_NPM,你看到Paz的實際問題是什麼?如果你想在StackOverflow上得到很好的答案,你必須是超級特定的。 – aychedee

+0

掛上,我想我看到了問題。我有一些額外的綁定... – aychedee

4

只是爲了記錄在案,這是你如何採取截圖與WebdriverJS

var webdriverjs = require('webdriverjs'), 
    client = webdriverjs.remote({ 
     desiredCapabilities: { 
      browserName: 'chrome' 
     } 
    }); 

client 
    .init() 
    .url('http://google.com') 
    .saveScreenshot(__dirname + '/googleScreenshot.png') 
    .end(); 

您還可以使用WebdriverCSS拍攝截圖。它是WebdriverJS做CSS迴歸測試的插件。它幾乎和PhantomCSS一樣。

0

適應aychedee的回答到,將解決該文件被寫入後,拒絕上寫失敗了完整的承諾:

const util = require('util) 
const fs = require('fs') 
const writefile = util.promisify(fs.writeFile) 

function takeScreenshot(driver, file){ 
    return driver.takeScreenshot() 
    .then(image => writeFile(file, image, 'base64')) 
} 

或在async功能

async function takeScreenshot(driver, file){ 
    let image = await driver.takeScreenshot() 
    await writeFile(file, image, 'base64')) 
} 
相關問題