Reputation: 11
I'm trying to get the API of a website using restler and protractor, but when I make 2 seperate API requests, it would return the result of the first request twice.
My function:
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;
}
Then:
getDataFunction().then(function() {
get2ndDataFunction();
});
Results after running the above scripts:
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' },
Expected Results:
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.......
I'm not sure if it's Restler or promise issue.
Upvotes: 0
Views: 212
Reputation: 11
Below is the answer;
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;
}
Then;
getDataFunction().then(function() {
get2ndDataFunction();
});
Upvotes: 1
Reputation: 5016
The protractor.promise
is an exposed selenium-webdriver namespace and I believe the defer
method has been deprecated since selenium-webdriver 3. It is not shown which version of Protractor you are using. Please check the selenium-webdriver changelog. Protractor took these changes in Protractor 5 with this commit.
Upvotes: 0