2017-02-26 90 views
0

我使用jasmine和量角器編寫了一些測試,使用require('child_process')執行.exe文件,然後@aftereach我將重新啓動瀏覽器。 問題是.exe文件在第一個規範中只執行一次。 這裏是在beforeEach(代碼)執行.exe文件使用節點在量角器中只運行一次

beforeEach((done) => { 
    console.log("before each is called"); 
    var exec = require('child_process').execFile; 

    browser.get('URL'); 
    console.log("fun() start"); 
    var child = exec('Test.exe', function(err, data) { 
     if (err) { 
      console.log(err); 
     } 
     console.log('executed'); 
     done(); 

     process.on('exit', function() { 
      child.kill(); 
      console.log("process is killed"); 
     }); 

    }); 

然後我寫了2種規格和在aftereach我重新啓動瀏覽器

afterEach(function() { 
     console.log("close the browser"); 
     browser.restart(); 
    }); 

回答

0

您應該使用donedone.fail方法退出異步beforeEach 。您開始執行Test.exe並立即致電完成。這可能會產生不希望的結果,因爲該過程仍可能正在執行。我不相信process.on('exit'每一個被調用。下面可能讓你開始使用來自子進程的事件發射器在正確的軌道上。

beforeEach((done) => { 
    const execFile = require('child_process').execFile; 

    browser.get('URL'); 

    // child is of type ChildProcess 
    const child = execFile('Test.exe', (error, stdout, stderr) => { 
    if (error) { 
     done.fail(stderr); 
    } 
    console.log(stdout); 
    }); 

    // ChildProcess has event emitters and should be used to check if Test.exe 
    // is done, has an error, etc. 
    // See: https://nodejs.org/api/child_process.html#child_process_class_childprocess 

    child.on('exit',() => { 
    done(); 
    }); 
    child.on('error', (err) => { 
    done.fail(stderr); 
    }); 

}); 
+0

我想你的孩子solution.The進程已退出,但它只once.In它並沒有得到執行 – user1115684

+0

第二規格這時我們就需要對你的測試的更多信息運行。我會添加一個「describe」,「beforeEach」和「it」塊的小片段。 – cnishina