我试图使用restler和protractor来获取网站的API,但是当我发出2个单独的API请求时,它会返回第一个请求的结果两次。
我的功能:
var url = undefined;
var rest = require('restler');
var defered = protractor.promise.defer();
var getDataFunction = function() {
var url = 'https://example.api.testlodge.com/v1/projects/16702/runs/287053/executed_steps.json?page=1'
rest.get(url, {
headers: {
'Connection': 'close'
}
}).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
defered.reject(result.message);
//this.retry(5000); // try again after 5 sec
} else {
defered.fulfill(result);
defered.promise.then(function(item) {
console.log("fulfill", item);
});
}
});
return defered.promise;
};
var get2ndDataFunction = function() {
rest.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Hyderabad%2C%20Tel%2C%20india%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys', {
headers: {
'Connection': 'close'
}
}).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
defered.reject(result.message);
//this.retry(5000); // try again after 5 sec
} else {
defered.fulfill(result);
defered.promise.then(function(item) {
console.log("fulfill", item);
});
}
});
return defered.promise;
}

然后:
getDataFunction().then(function() {
get2ndDataFunction();
});

运行上述脚本后的结果:
fulfill { pagination: { total_entries: 22, total_pages: 2, current_page: 1, next_page: 2, previous_page: null, per_page: 20 }, executed_steps: [ { id: 12712861, step_number: 'TC01', title: 'User should be able to view agent profile', description: 'Prerequisite:
\r\nUser is on the agent page, test_steps: '1. Ensure The following are present:\r\n\r\nAgent Image\r\nMobile Phone(mandatory)\r\nAfter Hours\r\nPhone\r\nProfile text\r\nRecommendations (if applicable)', expected_result: 'User is able to view the agent
image, contact details and the profile text and/or recommendations', stored_custom_fields: [Object], actual_result: null, passed: 1, issue_tracker_ticket_number: null, position: 1, run_id: 287053, run_section_id: 375586, last_saved_by_id: 34400, custom_fields:
[], created_at: '2017-10-24T20:38:06.000Z', updated_at: '2017-10-27T04:22:20.000Z' },
fulfill { pagination: { total_entries: 22, total_pages: 2, current_page: 1, next_page: 2, previous_page: null, per_page: 20 }, executed_steps: [ { id: 12712861, step_number:
'TC01', title: 'User should be able to view agent profile', description: 'Prerequisite: \r\nUser is on the agent page, test_steps: '1. Ensure The following are present:\r\n\r\nAgent Image\r\nMobile Phone(mandatory)\r\nAfter Hours\r\nPhone\r\nProfile text\r\nRecommendations
(if applicable)', expected_result: 'User is able to view the agent image, contact details and the profile text and/or recommendations', stored_custom_fields: [Object], actual_result: null, passed: 1, issue_tracker_ticket_number: null, position: 1, run_id:
287053, run_section_id: 375586, last_saved_by_id: 34400, custom_fields: [], created_at: '2017-10-24T20:38:06.000Z', updated_at: '2017-10-27T04:22:20.000Z' },

预期结果:
fulfill { pagination: { total_entries: 22, total_pages: 2, current_page: 1, next_page: 2, previous_page: null, per_page: 20 }, executed_steps: [ { id: 12712861, step_number: 'TC01', title: 'User should be able to view agent profile', description: 'Prerequisite:
\r\nUser is on the agent page, test_steps: '1. Ensure The following are present:\r\n\r\nAgent Image\r\nMobile Phone(mandatory)\r\nAfter Hours\r\nPhone\r\nProfile text\r\nRecommendations (if applicable)', expected_result: 'User is able to view the agent
image, contact details and the profile text and/or recommendations', stored_custom_fields: [Object], actual_result: null, passed: 1, issue_tracker_ticket_number: null, position: 1, run_id: 287053, run_section_id: 375586, last_saved_by_id: 34400, custom_fields:
[], created_at: '2017-10-24T20:38:06.000Z', updated_at: '2017-10-27T04:22:20.000Z' },
{"query":{"count":1,"created":"2017-10-29T23:48:57Z","lang":"en-GB","results":{"channel":{"units":{"distance":"mi","pressure":"in","speed":"mph","temperature":"F"},"title":"Yahoo! Weather - Hyderabad, Telangana.......

我不确定它是否是Restler或承诺问题。
答案 0 :(得分:1)
以下是答案;
var url = undefined;
var rest = require('restler');
var getDataFunction = function() {
var defered = protractor.promise.defer();
var url = 'https://example.api.testlodge.com/v1/projects/16702/runs/287053/executed_steps.json?page=1'
rest.get(url, {
headers: {
'Connection': 'close'
}
}).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
defered.reject(result.message);
//this.retry(5000); // try again after 5 sec
} else {
defered.fulfill(result);
defered.promise.then(function(item) {
console.log("fulfill", item);
});
}
});
return defered.promise;
};
var get2ndDataFunction = function() {
var defered2 = protractor.promise.defer();
rest.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Hyderabad%2C%20Tel%2C%20india%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys', {
headers: {
'Connection': 'close'
}
}).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
defered2.reject(result.message);
//this.retry(5000); // try again after 5 sec
} else {
defered2.fulfill(result);
defered2.promise.then(function(item) {
console.log("fulfill", item);
});
}
});
return defered2.promise;
}

然后;
getDataFunction().then(function() {
get2ndDataFunction();
});

答案 1 :(得分:0)
protractor.promise
是一个公开的selenium-webdriver命名空间,我相信自{selenium-webdriver 3'以来已经弃用了defer
方法。没有显示您正在使用哪个版本的Protractor。请检查selenium-webdriver changelog。量角器使用this commit在量角器5中进行了这些更改。