I have this below piece of code that generates me multiple orders in Pre-request Script section.
I dont need to have anything in "Body" section. But sending just [{}] in the body gives me 400 error.
var guid = (function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function() {
return 'HolCal' + s4() + '-' + s4() + '-' + s4() + '-' +
s4();
};
})();
var requestKeyNum = 2;
var orders = [];
for (var i = 0; i < requestKeyNum; i++) {
var key = guid();
orders.push({
"key": key,
"tradeDate": "2019-07-03",
"settleDate": "2019-07-04",
"transactionCode": "B",
"fundingCurrencySecurity" :{
"secId":1894823,
"secType1": "CASH",
"secType2":"NA",
"secType3":"NA",
"secType4":"NA",
"assetClass":"C",
"exchangeCode":"",
"tradeCurrencyCode":"USD",
"maturityDate":null,
"mortgageClass":null,
"investIdType":"D",
"investId":"9999USD",
"clearingHouseCode":null,
"settlementLocation":"PHY",
"expirationDate":null,
"issueCountry":""
}
})
}
postman.setEnvironmentVariable("orders", JSON.stringify(orders));
How must your generated body look like?
I assume, that you want so send you generated orders in your body.
If its true, you must reference to the orders variable in your body. Use {{orders}} in your body panel to do this.
Related
var totalList_grps = element.all(by.css('p.group-name-text'));
totalList_grps.getText().then(function(text){
console.log('Total list of joined groups : ' + text);
});
Tried the above code for printing list of group names.
Got Output :Total list of joined groups : Party,Innovation,capsLock,Gym,Sunrisers
AW,Big Boss.
Now i need to search for a particular name using if else condition and i tried the second set of code, but its not displaying any output not even a error.
totalList_grps.getText().then(function(itemList) {
expect(itemList).toContain('Big Boss');
});
Here is developers code
1) use by.cssContainingText():
var bigBoss = element(by.cssContainingText('p.group-name-text', 'Big Boss'));
// then you can call click(), getText(), getAttribute('') on found element as following:
bigBoss.click();
2) use elements.filter():
var bigBoss = element.all(by.css('p.group-name-text'))
.filter(function(it){
return it.getText().then(function(txt){
console.log('txt: ' + txt);
return txt === 'Big Boss' || txt.includes('Big Boss');
});
})
.first();
3) use await with combination of if/else
var allNames = element.all(by.css('p.group-name-text'));
var length = await allNames.count();
var matchedIndex = -1;
for(var i=0;i<length;i++) {
var name = await allNames.get(i).getText();
if (name === 'Big Boss' || name.includes('Big Boss')) {
matchedIndex = i;
console.log('matchedIndex = ' + matchedIndex);
break;
}
}
var bigBoss = allNames.get(matchedIndex);
We can implement option 3 without using await, but the code will be not easy readable and more complex than current.
FYI, If you want to use await/async, you need to disable protractor promise management (know as control flow). You can't use both in your code at same time.
In Newman I want to test to ensure that the response code is correct, response time is reasonable and response values are correct.
In some cases, due to network hiccups or other system conditions, some requests might end up with timeouts or incorrect values that will resolve if the same request was processed a few seconds later.
in such cases, I would like to retry the exact request x times with a Y timeout between requests.
If an iteration pass after a retry, I would like the Newman exit code to be 0 (successful run).
After few hours I had ended up with a function like this:
function retryOnFailure(successCode, numberOfRetrys) {
var key = request.name + '_counter';
var execCounter = postman.getEnvironmentVariable(key) || 1;
var sleepDuration = 1000;
var waitUntilTime = new Date().getTime() + sleepDuration;
if (responseCode.code !== successCode && execCounter <= numberOfRetrys) {
while (new Date().getTime() < waitUntilTime) {
// Do Nothing -> Wait
}
console.log('Retrying: ' + request.name + '\nGot: ' + responseCode.code + ' Expected: ' + successCode + '\nWaited: ' + sleepDuration / 1000 + 'sec \nRetry Number: ' + execCounter + ' of ' + numberOfRetrys);
execCounter++;
postman.setEnvironmentVariable(key, execCounter);
postman.setNextRequest(request.name);
}
}
Usage:
retryOnFailure(404, 4);
You can setup a request workflow like this:
Create a collection with a request, then:
In the pre-request tab you can implement a counter:
// Counter for number of requests
var counter = environment.counter ? _.parseInt(environment.counter) + 1 : 1;
postman.setEnvironmentVariable("counter", counter);
Your tests tab would look like this:
const code = (responseCode.code === 200);
if (code === 200 && environment.counter < X) {
// Stop execution
tests["Status code is 200"] = code;
postman.setNextRequest();
}
else {
// retry the same request
postman.setNextRequest("Name of this request");
}
A timeout for the request itself can be configured with the newman CLI:
newman run myCollection.json --timeout-request Y
Here is the reusable function for the same
postmanFunctions.common.retryOnFailure(predicate,retryCount,waitbetweenRetrySec,ReroutetorequestifNeeded ,postmanAssertions);
predicate function decides success or failure
assertion function has all postman assertion
if reroute is blank then after retry attempts assertions gets executed.
Flexible polling with retrycount and waittime(if predicate passed no more
polling/reflow)
There is a maxflow counter(env var) which limits the number of flow jumps to
avoid infinite loop
Store the below function in Globals or env:
() => {
var sleep = (sleepDuration) => {
var startTime = new Date().getTime();
while (new Date().getTime() - startTime < sleepDuration) {}
}
var sleepByAsyncDelayTime = () => {
var sleepDuration = postman.getEnvironmentVariable('asyncDelayTime') || 0;
sleep(sleepDuration);
}
var retryOnFailure = (predicate, numberOfRetrys, sleepDuration, reRouteRequestName, postmanAssertions) => {
var retryCountPerReq_key = request.name + '_retry_count';
var retryCountPerReq = pm.environment.get(retryCountPerReq_key) || 0;
var reflowCountPerReq_key = request.name + '_reflow_count';
var reflowCountPerReq = pm.environment.get(reflowCountPerReq_key) || 0;
var totalReflowCount_key = 'totalReflowCount';
var totalReflowCount = pm.environment.get(totalReflowCount_key) || 0;
var maxReflowCounter = postman.getEnvironmentVariable('maxReflowCounter') || 0;
var maxReflowCounterPerReq = postman.getEnvironmentVariable('maxReflowCounterPerReq') || 0;
function clearAndExit() {
pm.environment.unset(retryCountPerReq_key);
pm.environment.unset(reflowCountPerReq_key);
postmanAssertions();
}
function retry() {
sleep(sleepDuration);
pm.environment.set(retryCountPerReq_key, ++retryCountPerReq);
postman.setNextRequest(request.name);
}
function reFlow() {
if (totalReflowCount < maxReflowCounter && reflowCountPerReq < maxReflowCounterPerReq) {
pm.environment.unset(retryCountPerReq_key);
pm.environment.set(totalReflowCount_key, ++totalReflowCount);
pm.environment.set(reflowCountPerReq_key, ++reflowCountPerReq);
postman.setNextRequest(reRouteRequestName);
} else clearAndExit();
}
if (predicate()) clearAndExit();
else if (retryCountPerReq < numberOfRetrys) retry();
else if (reRouteRequestName != '') reFlow();
else clearAndExit();
}
return {
common: {
sleepByAsyncDelayTime,
sleep,
retryOnFailure
}
};
}
Here is my retry function that I define in collection pre-request script. It only works when tests are executed via collection :
Utils = {
wait: function (that, sleepDuration){
that.setTimeout(() => {}, sleepDuration);
},
withRetry: function(that, expectedHttpStatus, maxNumberOfTries, sleepBetweenTries, businessRetryConditionCallBack, endRetryCallback){
if (!that.pm.environment.get("collection_tries")) {
that.pm.environment.set("collection_tries", 1);
}
if (((that.pm.response.code != expectedHttpStatus) || businessRetryConditionCallBack())
&& (that.pm.environment.get("collection_tries") <= maxNumberOfTries)) {
var tries = parseInt(that.pm.environment.get("collection_tries"), 10);
that.pm.environment.set("collection_tries", tries + 1);
Utils.wait(that, sleepBetweenTries, maxNumberOfTries);
that.postman.setNextRequest(that.request.name);
} else {
if(businessRetryConditionCallBack()){
// On ne passe pas à la requête suivante
that.postman.setNextRequest(null);
}
that.pm.environment.unset("collection_tries");
endRetryCallback();
}
}
};
And here is how to use it in request on pre-request or test scripts :
var expectedHttpStatus = 200;
var maxNumberOfTries = 5;
var sleepBetweenTries = 5000;
Utils.withRetry(this, expectedHttpStatus, maxNumberOfTries, sleepBetweenTries, function(){
// Retry business condition callback
return pm.response.json().length <= 0;
}, function(){
// End retry callback
pm.test("Has one result", function () {
pm.expect(pm.response.json().length).to.equals(0);
});
});
This code will retry request as long as (http statut is different from expectedHttpStatus or businessRetryConditionCallBack is true) AND maxNumberOfTries is not reached and .
When http statut condition is true and maxNumberOfTries is reached, a check is done to verify businessRetryConditionCallBack. If not true, collection execution is stopped.
I want to make some dynamic controllers based on the top-level keys of a JSON object where the second level keys become variables for the view. Is this possible?
E.g.
App.dat = { "First": {Var1: "x", Var2: "y"}, "Second": {Var3: "z"}}
I then want to have a FirstController, SecondController, etc. created automatically and then be able to access {{Var1}}, {{Var2}} and {{Var3}} in my view.
The routes work fine:
App.Router.map(function() {
this.route('data');
for(var key in App.dat) {
this.route(key);
}
});
But I can't get the controllers to work. The closest I came was with some hacky eval code:
for(var key in App.dat) {
console.log("App." + key + "Controller = Ember.Controller.extend();");
eval("App." + key + "Controller = Ember.Controller.extend();");
Ember.$.each(eval("App.dat."+key), function(kkey,v) {
console.log("App." + key + "Controller." + kkey + " = '" + v + "';");
eval("App." + key + "Controller." + kkey + " = '" + v + "';");
});
}
That results in the following evaluations:
App.FirstController = Ember.Controller.extend();
App.FirstController.Var1 = "x";
App.FirstController.Var2 = "y";
App.SecondController = Ember.Controller.extend();
App.FirstController.Var2 = "z";
But I'm clearly missing something because I can't access {{x}},{{y}} or {{z}} from the view. I am aware the correct way to instantiate variables for the view is with a constructor object like:
App.FirstController = Ember.Controller.extend({Var1: "x", Var2: "y"});
But that isn't practical and I can't figure out where the constructor object gets stored.
Thanks in advance.
You can override Ember.DefaultResolver's resolveController method. Which is responsible for lookuping controller classes.
App = Ember.Application.create({
Resolver: Ember.DefaultResolver.extend({
resolveController: function(parsedName) {
var props = Ember.get(App.dat, parsedName.name);
if (!Ember.isNone(props)){
return Ember.Controller.extend(props);
}
return this._super(parsedName);
}
})
});
jsbin
After some careful bug hunting, the following code works fine:
for(var key in App.dat) {
properties = Array();
Ember.$.each(eval("App.dat."+key), function(kkey,v) {
properties.push(kkey + ": '" + v + "'")
});
console.log("App." + key + "Controller = Ember.ObjectController.extend({" +properties + "})");
eval("App." + key + "Controller = Ember.ObjectController.extend({"+ properties + "})");
}
Is there a less hacky way to do this than by eval()ing everything? Remember this data comes from JSON, so I can't imagine this would be safe in a real app.
So from what I understand in Couchbase is that one can sort keys* by using
descending=true
but in my case I want to sort by values instead. Consider the Twitter data in json format, my question is What it the most popular user mentioned?
Each tweet has the structure of:
{
"text": "",
"entities" : {
"hashtags" : [ ... ],
"user_mentions" : [ ...],
"urls" : [ ... ]
}
So having used MongoDB before I reused the Map function and modified it slightly to be usable in Couchbase as follows:
function (doc, meta) {
if (!doc.entities) { return; }
doc.entities.user_mentions.forEach(
function(mention) {
if (mention.screen_name !== undefined) {
emit(mention.screen_name, null);
}
}
)
}
And then I used the reduce function _count to count all the screen_name occurrences. Now my problem is How do I sort by the count values, rather than the key?
Thanks
The short answer is you cannot sort by value the result of you view. You can only sort by key.
Some work around will be to either:
analyze the data before inserting them into Couchbase and create a counter for the values you are interested by (mentions in your case)
use the view you have to sort on the application size if the size of the view is acceptable for a client side sort.
The following JS code calls a view, sorts the result, and prints the 10 hottest subjects (hashtags):
var http = require('http');
var options = {
host: '127.0.0.1',
port: 8092,
path: '/social/_design/dev_tags/_view/tags?full_set=true&connection_timeout=60000&group=true',
method: 'GET'
}
http.request(
options,
function(res) {
var buf = new Buffer(0);
res.on('data', function(data) {
buf += data;
});
res.on('end', function() {
var tweets = JSON.parse(buf);
var rows = tweets.rows;
rows.sort( function (a,b){ return b.value - a.value }
);
for ( var i = 0; i < 10; i++ ) {
console.log( rows[i] );
}
});
}
).end();
In the same time I am looking at other options to achieve this
I solved this by using a compound key.
function (doc, meta) {
emit([doc.constraint,doc.yoursortvalue]);
}
url elements:
&startkey=["jim",5]&endkey=["jim",10]&descending=true
I am using jquery tool for tab Ui,
Now I want to keep tab selected on page reload. Is there any way to do that? below is my code
$(function() {
// setup ul.tabs to work as tabs for each div directly under div.panes
$("ul.tabs").tabs("div.panes > div");
});
Here is a simple implementation of storing the cookie and retrieving it:
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
Then, to save/retrieve cookie data with jQuery UI Tabs:
$(function() {
// retrieve cookie value on page load
var $tabs = $('ul.tabs').tabs();
$tabs.tabs('select', getCookie("selectedtab"));
// set cookie on tab select
$("ul.tabs").bind('tabsselect', function (event, ui) {
setCookie("selectedtab", ui.index + 1, 365);
});
});
Of course, you'll probably want to test if the cookie is set and return 0 or something so that getCookie doesn't return undefined.
On a side note, your selector of ul.tabs may be improved by specifying the tabs by id instead. If you truly have a collection of tabs on the page, you will need a better way of storing the cookie by name - something more specific for which tab collection has been selected/saved.
UPDATE
Ok, I fixed the ui.index usage, it now saves with a +1 increment to the tab index.
Here is a working example of this in action: http://jsbin.com/esukop/7/edit#preview
UPDATE for use with jQuery Tools
According the jQuery Tools API, it should work like this:
$(function() {
//instantiate tabs object
$("ul.tabs").tabs("div.panes > div");
// get handle to the api (must have been constructed before this call)
var api = $("ul.tabs").data("tabs");
// set cookie when tabs are clicked
api.onClick(function(e, index) {
setCookie("selectedtab", index + 1, 365);
});
// retrieve cookie value on page load
var selectedTab = getCookie("selectedtab");
if (selectedTab != "undefined") {
api.click( parseInt(selectedTab) ); // must parse string to int for api to work
}
});
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays === null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
Here is a working (unstyled) example: http://jsbin.com/ixamig/12/edit#preview
Here is what I see in Firefox when inspecting the cookie from the jsbin.com example:
This is what worked for me... at least I haven't run into any issues yet:
$('#tabs').tabs({
select: function (event, ui)
{
$.cookie('active_tab', ui.index, { path: '/' });
}
});
$('#tabs').tabs("option", "active", $.cookie('active_tab'));
I'm using: jQuery 1.8.2, jQuery UI 1.9.1, jQuery Cookie Plugin.
I set the "path" because in C# I set this value in a mvc controller which defaults to "/". If the path doesn't match, it wont overwrite the existing cookie. Here is my C# code to set the value of the same cookie used above:
Response.Cookies["active_tab"].Value = "myTabIndex";
Edit:
As of jQuery UI 1.10.2 (I just tried this version, not sure if it's broken in previous versions), my method doesnt work. This new code will set the cookie using jQuery UI 1.10.2
$('#tabs').tabs({
activate: function (event, ui) {
$.cookie('active_tab', ui.newTab.index(), { path: '/' });
}
});
The easiest way to survive between page refresh is to store the selected tab id in session or through any server-side script.
Only methods to store data on client side are: Cookies or localStorage.
Refer to thread: Store Javascript variable client side