2016-05-13 44 views
2

我正在從事UI自動化測試項目,我需要向我的服務器發送ajax請求,但是在Nighwatch.js中不能接受一些香草javascript和jQuery函數的功能,如何在nightwatch.js中發送ajax獲取請求

所以如果任何人有任何經驗,發送Ajax請求服務器在nightwatch.js環境,那麼請給我一些信息/建議。

+0

也許用xhr? https://developer.mozilla.org/pl/docs/XMLHttpRequest – us3r

+0

我嘗試從給定的網址代碼,但我得到以下錯誤。 「XMLHttpRequest未定義」 –

回答

1

經過長時間的研究,我發現request.js是一個節點模塊,我通過安裝"request" node module解決了我的問題。安裝後,我可以在Nightwatch環境中對我的服務器執行「GET」和「POST」請求。我正在寫一塊像魅力一樣工作的代碼。

/* jshint expr: true */ 
module.exports = { 
'@tags' : ['book'], 
beforeEach : function (client) { 

}, 
after : function (client) { 
    client.end(); 
}, 
wGroup: { 
    book_url: "https://example.myApi.mycompany.in" 
}, 

userSettings: Array(), 

"Get all settings": function (client, done) { 
    var widget = this.wGroup; 
    client.getreq(widget.book_url + "/api/store", widget, function (response) { 
     client.assert.equal(response.statusCode, 200, "201 Created"); 
     var objects = response.body.objects; 
     client.userSettings = objects; 
     console.log('Found number of settings: ' + client.userSettings.length); 
     client.end(); 
    }); 
}, 

"Remove settings": function (client, done) { 
    var widget = this.wGroup; 
    var objects = client.userSettings; 
    for(i=0; i<objects.length; i++) { 
     var obj = objects[i]; 
     console.log('Removing user settings id ' + obj.id); 
     client.deletereq(widget.book_url: l + "/api/store" + obj.id, widget, function (resp) { 
      client.assert.equal(resp.statusCode, 204, "204 Created"); 
      client.end(); 
     }); 
    } 
    }, 
};