How to run a request only once with many iterations in Postman - postman

I tried to look everywhere, but no luck so far. I have a collection with 6 requests that run automatically, but I need the last request to run only once at the end of the last iteration.
I tried to use this code in Pre-request Script of Request 5
if (pm.info.iteration === 0) {
postman.setNextRequest("Request 6")
} else {
postman.setNextRequest("Request 1")
}
and expected that the Request 6 would run only once after there is no more iteration to run, but I am doing something wrong since it is still not working.

you kind of mixed up pm.info.iterationCount and pm.info.iteration:
pm.info.iteration The value of the current iteration
pm.info.iterationCount The total number of iterations that are scheduled to run
See the documentation: Scripting with request info
You want to execute "Request 6" at the very end, this means, when iteration and iterationCount are equal.
Note that iteration start at 0, so you need to add + 1 when comparing it to iterationCount.
Also, if this condition does not match, you need to set the nextRequest to null.
"Request 1" will be executed automatically by the Collection Runner. If you set the nextRequest to "Request 1", you will end up in an endless loop.
The following will do you what you want, you can put it as pre-request script or test of "Request 5".
if (pm.info.iterationCount == pm.info.iteration + 1) {
postman.setNextRequest("Request 6")
} else {
postman.setNextRequest(null)
}

Related

My request won't repeat itself after executing

So I have a request in Postman that will create a User for me. I want to run it automatically for a certain amount of time to save time preparing datas. I try using setNextRequest but it doesn't work. I have tried to put it in Pre-request Script, Tests tab of the request itself as well as the Collection but it just won't run. I have certainly sure that all of my item is saved, none of them has orange dots.
var count = 0;
while(count < 5){
postman.setNextRequest("CreatePersonWithUser");
console.log("This is the " + count + " time");
count++;
}

I m inserting my data uthrough page item using request process it gives an error fetch more then one row please give me a solution

var a = $v('P1995_LUMBER');
if ((a = '1')) {
apex.submit({
request: "CREATE",
set: {
LUMBER: "P1995_LUMBER",
LST_NME: "P1995_LST_NME",
FST_NME: "P1995_FST_NME",
},
});
} else if (a != '1') {
apex.submit({
request: "Update",
set: {
LUMBER: "P1995_LUMBER",
LST_NME: "P1995_LST_NME",
FST_NME: "P1995_FST_NME",
},
});
} else {
alert("bang bang");
}
Couple of things:
JavaScript's equality check is either == or === (more details here). (a = '1') assign '1' to the variable.
It seems like you're not using the apex.submit process correctly. Typically, you would set the item's value
e.g.:
apex.page.submit({
request: "SAVE",
set: {
"P1_DEPTNO": 10,
"P1_EMPNO": 5433
}
} );
Although, by looking at your JavaScript code, I would say you don't even need to use JavaScript.
Whenever you submit a page, all items on it are automatically sent to the server-side. You can then reference them using bind variables. You could then simply have two process, one for the Create and one for the Update, each having the corresponding insert/update statement using the different items on your page.
Usually what you will see is a page with two buttons for Create/Edit. They will have a server-side condition so that only the correct one is displayed.
Try creating a Form type page (form with report) using the wizard, and you'll see how everything is done.
Without seeing the page and the code you're using it's hard to tell what your issue really is, more details would be required.
That code does not have any sql in it so it is impossible to diagnose why you are encountering a TOO_MANY_ROWS exception. Run the page in debug mode and check the debug data - it should show you what statement is throwing the exception. If you need more help, post a proper reproducible case, not a single snipped of code without any context.

Attempting to put a delay in a loop in Postman

I'm trying to put a 1 second delay using setTimeout(()=>{},1000) in the Pre-request Script for a Postman POST call.
var moment = require('moment');
var tap1TimeStr = pm.environment.get("now");
var tap1TimeMoment = moment(tap1TimeStr,"YYYY-MM-DDTHH:mm:ss");
var expTap2Time = tap1TimeMoment.add(2, 'minutes').format("YYYY-MM-DDTHH:mm:ss");
console.log("Tap 2 timestamp should be: " + expTap2Time);
var timestamp;
var timecheck = false;
while(!timecheck)
{
setTimeout(() => {},1000);
timecheck = moment.utc().isSame(expTap2Time);
console.log("timecheck: " + timecheck);
timestamp = moment.utc().format("YYYY-MM-DDTHH:mm:ss");
}
console.log("Timestamp is now: " + timestamp);
pm.environment.set("now", timestamp);
But it doesn't seem to work and I can see that the console.log line is being printed far more frequently than 1sec. And the exercise here is to send the "Tap 2" POST exactly 2mins after the first POST (tracked by the 'now' variable). Also, it seems like Postman takes a fair bit of time before it even starts executing this particular script.
Edit: The main requirement here is to send the "Tap 2" POST request exactly 2mins AFTER the "Tap 1" POST request. HOW best to implement that? Espcially if setTimeout() is non-blocking and thus probably can't be used in a loop.
Anyone has any ideas?
setTimeout() takes a callback function which is executed after the specified delay so only what happens in the callback function will happen after that delay.
setTimeout(() => {
console.log("This will be executed after 1 second");
}, 1000);
console.log("This will immediately be executed");
setTimeout() is asynchronous and non-blocking so JavaScript will call set timeout but not wait for 1 second for it to return and instead immediately move on to the next instruction. Only after that 1 second has passed the callback passed to setTimeout() will be scheduled and executed. Have a look at this YouTube video for a good explanation of what's going on.

cypress .next at end of list yields '' but I need to break instead

I am writing Cypress tests for an application that has a dynamic group/list of items. Each item has a details link that when clicked creates a popup with said details. I need to close that popup and move to the next item in the group.
I first tried to use the .each() command on the group and then would .click({multiple:true}) the details but the popup would cover the the next click. Adding {foce:true} does allow all of the popups to display but I don't think that is in the spirit of how the application should function.
My latest attempt has been to create a custom command using .next() to iterate through the group. This works but when .next() reaches the end of the group it yields "" and so the test ultimately fails.
The actual error I get is:
Expected to find element: ``, but never found it. Queried from element: <div.groups.ng-star-inserted>
the .spec.ts
describe('Can select incentives and view details', () => {
it('Views incentive details', () => {
cy.optionPop('section#Incentives div.groups:first')
})
})
the index.ts
Cypress.Commands.add('optionPop', (clickable) => {
cy.get(clickable).find('[ng-reflect-track="Estimator, open_selection_dial"]').click()
cy.get('mat-dialog-container i.close').click()
cy.get(clickable).next().as('clicked').then(($clicked) => {
//fails at .next ^ because '' is yielded at end of list
cy.wrap($clicked).should('not.eq','')
})
cy.optionPop('#clicked')
})
You basically have the right idea, but it might work better in a plain JS function rather than a custom command.
function openPopups(clickables) {
if (clickables.length === 0) return // exit when array is empty
const clickable = clickables.pop() // extract one and reduce array
cy.wrap(clickable)
.find('[ng-reflect-track="Estimator, open_selection_dial"]').click()
cy.get('mat-dialog-container i.close')
.should('be.visible') // in case popup is slow
.click()
// wait for this popup to go, then proceed to next
cy.get('mat-dialog-container')
.should('not.be.visible')
.then(() => {
openPopups(clickables) // clickables now has one less item
})
}
cy.get('section#Incentives div.groups') // get all the popups
.then($popups => {
const popupsArray = Array.from($popups) // convert jQuery result to array
openPopups(popupsArray)
})
Some extra notes:
Using Array.from($popups) because we don't know how many in the list, and want to use array.pop() to grab each item and at the same time reduce the array (it's length will control the loop exit).
clickables is a list of raw elements, so cy.wrap(clickable) makes the individual element usable with Cypress commands like .find()
.should('be.visible') - when dealing with popup, the DOM is often altered by the click event that opens it, which can be slow relative to the speed the test runs at. Adding .should('be.visible') is a guard to make sure the test is not flaky on some runs (e.g if using CI)
.should('not.be.visible').then(() => ... - since you have some problems with multiple overlapping popups this will ensure each popup has gone before testing the next one.

If else condition, that else condition should fail instead of pass in postman

I want to write a if else condition so that if the value doesn't matches then it will go to else condition and i'm able to get that.
But the thing the test case is getting passed after going to else condition, but my requirement is that if the flow reaches else condition the test case should fail.
Can anyone help me in this.
I don't know anything about the context here so this is just a simple example of a test that fails if it goes down a certain path:
let a = 124
pm.test("Check that the value matches", () => {
if (a === 123) {
pm.expect(a).to.equal(123)
}
else {
pm.expect.fail(`Value didn't equal 123 - Value was ${a}`)
}
})
I created a variable a with the value 124 - The if condition is saying if a equals 123 go down this path and assert against that value. If it's anything else, it will show a fail using pm.expect.fail().
Without knowing more about what you're trying to do, I can't comment about the need to have an if/else to fail a Test. If the assertion or pm.expect() statement is not met, it's going to fail the test for you and show the reason why in the Test Results:
let a = 124
pm.test("Check that the value matches", () => {
pm.expect(a).to.equal(123)
})