2015-01-08 27 views
0

我遇到問題了解如何在InternJs中測試失敗時才截圖。我在我的registerSuite中有這個簡單的測試;如何在internjs測試失敗時截圖?

'verify google homepage': function() { 
    var url = 'https://www.google.com/'; 
    return this.remote 
     .get(url) 
     .getCurrentUrl() 
     .then(function (data) { 
      assert.strictEqual(data, url, 'Incorrect URL'); 
     }) 
     .findByName('q') 
      .click() 
} 

我可以簡單地使用下面的代碼創建屏幕截圖;

.takeScreenshot 
.then(function (data) { 
    fs.writeFileSync('/path/to/some/file', data, 'base64'); 
)} 

我想只拍一個截圖,如果上面的測試失敗斷言或無法找到定位器。

我看着afterEach方法,但我無法弄清楚如何獲得最後一個測試的狀態來應用條件。

所以我的問題是,有沒有人設置他們的internjs測試,只對失敗截圖,它是如何完成的?

回答

3
  1. 它是目前無法從beforeEachafterEach方法當前正在執行的測試交互;此功能即將出現在下一版Intern中。

  2. Selenium服務器默認提供每個Selenium命令失敗的屏幕截圖,該失敗是error.detail.screen屬性上的Buffer對象。如果Selenium命令失敗,只需使用已經有等待你的截圖的這個屬性。

  3. 對於斷言失敗,您可以創建一個簡單的承諾幫手拿截圖給你:

function screenshotOnError(callback) { 
    return function() { 
    try { 
     return callback.apply(this, arguments); 
    } 
    catch (error) { 
     return this.remote.takeScreenshot().then(function (buffer) { 
     fs.writeFileSync('/path/to/some/file', buffer); 
     throw error; 
     }); 
    } 
    }; 
} 

// ... 

'verify google homepage': function() { 
    return this.remote.get(url).getCurrentUrl().then(screenshotOnError(function (actualUrl) { 
    assert.strictEqual(actualUrl, url); 
    })); 
} 

如果它太不方便手動像這樣的包裝所有的回調,你還可以創建並以類似的方式使用自定義界面來註冊您的測試,並自動爲您包裝測試功能。我將把它作爲讀者的練習。

+0

謝謝。這正是我正在尋找的。 – glamb

0

您可以在鏈條的末尾使用catch方法,並使用C Snover建議的error.detail.screen。

'verify google homepage': function() { 
    return this.remote 
     .get(require.toUrl('./fixture.html')) 
     .findById('operation') 
      .click() 
      .type('hello, world') 
     .end() 
     .findById('submit') 
      .click() 
     .end() 
     .catch(function(error){ 
      fs.writeFileSync('/tmp/screenshot.png', error.detail.screen); 
     }) 
} 
0

我一直在玩這個今天並設法得到它爲整個套件,而不需要將代碼添加到每個單獨的測試,似乎很不必要的。

var counter = -1, 
suite = { 
    beforeEach: function() { 
     counter++; 
    }, 
    afterEach: function() { 
     var currentTest = this.tests[counter]; 
     if (!currentTest.error) { 
      return; 
     } 
     this.remote 
      .takeScreenshot().then(function (buffer) { 
       if (!fs.existsSync(path)) { 
        fs.mkdirSync(path); 
       } 
       fs.writeFileSync('/tmp/' + currentTest.name + '.png', buffer); 
      }); 
    } 
}; 

您需要做的煩人的事情是爲每個測試套件而不是「全局」做這件事,但比每次測試都做得更好。

0

根據this issue,從實習3.0開始,你可以做一個自定義的記者,當測試失敗的時候會拍一個截圖。所以你可以用簡單的方法集中它,只需在你的config.js中引用自定義記者。就我而言,我能只用路徑添加在config.js記者數組我的自定義數組:

reporters: [ 
     { id: 'tests/support/ScreenShot' } 
], 

比我做了一個自定義的記者覆蓋testFail

'use strict'; 

define([ 
    'intern/dojo/node!fs', 
], function(fs) { 

    function ScreenShot(config) { 
     config = config || {}; 
    } 

    ScreenShot.prototype.testFail = function(test) { 
     test.remote.takeScreenshot().then(function(buffer) { 

      try { 
       fs.writeFileSync('./screenshots/' + test.parent.name.replace(/ /g, '') + '-' + 
        test.name.replace(/ /g, '') + '.png', buffer); 

      } catch (err) { 
       console.log('Failed to take a screenshot: ' + err); 
      } 
     }); 
    }; 

    return ScreenShot; 
}); 

,請注意相對路徑既引用自定義記者和地方的屏幕截圖。他們似乎都在考慮在哪裏運行intern-runner,而不是源文件所在的位置。 欲瞭解更多有關自定義記者的信息,請登錄this page

0

大廈雨果大城答案,

// tests/support/CleanScreenshots.js 

define([ 
    'intern/dojo/node!path', 
    'intern/dojo/node!del', 
], function(path, del) { 

    return new Promise((resolve, reject) => { 
    let directory = 'tests/screenshots'; 
    del(path.join(directory, '**/*')) 
     .then(resolve) 
     .catch(reject); 
    }); 

}); 

然後在您的實習生配置:

/* global define */ 

define([ 
    'tests/support/CleanScreenshots' 
    ], function (CleanScreenshots) { 


    return { 

    ... 

    setup: function() { 
     return CleanScreenshots(); 
    }, 

    ... 

    }; 
}); 
相關問題