簡體   English   中英

如何確保在節點上使用mocha和phantomjs進行的每項測試都保持不變的HTML

[英]how to ensure untouched HTML for every test with mocha and phantomjs on node

我寫了一個Web應用程序,我想在命令行中使用mocha和phantomjs進行一些DOM測試,我試圖找出最好的方法。

由於Web應用程序正在執行許多DOM操作,因此我希望能夠為每次測試完全加載DOM。 我嘗試使用phantomjs網頁模塊,但是當時我不知道如何在頁面上運行mocha測試(或者這是否是正確的方法)。

當然,我可以只創建一個文件test.html ,在其中通過腳本標記包含摩卡,然后運行phantomjs test.html 但是,如何確保每個測試都不會破壞DOM? 為每個具有相同內容的測試添加一個HTML文件將是一場噩夢。

到目前為止,我有:

我的testrunner.js:

var Mocha = require('mocha'),
    expect = require('expect.js'),
    fs    = require('fs'),
    path  = require('path');

var mocha = new Mocha(
{
  ui: 'bdd'
});
// I only add one file for now
mocha.addFile(
  path.join(__dirname, 'tests_integration/test.js')
);

mocha.run(function(failures){
  process.on('exit', function () {
    process.exit(failures);
  });
});

我的test.js:這是我在奮斗的地方,因為我無法弄清楚如何在頁面內運行測試:

var phantom = require('phantom');
phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open("http://test.local", function (status) {
      page.evaluate(function (tests) {
        // here I need to run my tests, which obviously doesnt work that way
        describe('DOM tests',function(){
          it('should be able to acces the page',function(){
            expect(document.body).not.to.be(undefined)
          });
        });
        return document.body
      }.bind(tests), function (html) {
        ph.exit();
      });
    });
  });
});

為了運行測試,我執行node integration-runner.js 我想到的一種解決方案是使用includeJS將mocha注入到幻像頁面中,但是對我來說這似乎有些拙劣。

我做錯了嗎?

好的,經過一番研究並與其他開發人員交談,看來創建不同的HTML文件將是最干凈的解決方案。 但是,由於我不想在每次更改webapp時都手動更新所有這些文件,因此我決定采用此好帖子中所述的injectJS解決方案。

現在,我可以將摩卡測試文件動態地注入實時站點並運行它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM