Forum Discussion
Well, the function you posted is handling it's exception and then doing stuff... you need to determine, then, if, when it exits, if the next function should execute. You need the code to return some sort of indicator that, yes, we can keep going... So... that said... you might want to try something like:
try {
var successful = false;
var browser = Sys.Browser("*");
var Page1 = browser.Page("*");
props = new Array("contentText", "className", "Visible", "VisibleOnScreen");
values = new Array(categoryname, "categoryButton", true, true);
var category = Page1.FindChild(props, values, 30);
if (!category.Exists) throw Error("Category " + categoryname + " not found");
category.Click();
Page1.Wait();
successful = true;
}
catch (error) {
Log.Error("Category group error occured: " + error.message);
ClickMyAccount();
Logoff();
}
finally {
return successful;
}
Then, in whatever code is calling that function, do a check like
if (!myFunction()) throw Error('blah');
Or something else... basically, tell TestComplete exactly what you want to do if the function does not return successful. This is actually a pretty fundamental key to a well constructed test suite/framework... that you build this kind of detection in to determine if each integral step of a test case is successful and, if it's not, mark the case as failed and "clean up" and then move on, either to the next test case or end the project.
Would I be able to use a While loop during all my function calls? I would prefer to umbrella all the function call checking under 1 conditional as opposed to calling an IF after every function call, since some scripts can have upwards of 30 calls.
Would something along these lines work?
MyScript() {
while(successful != false)
var successful = functioncall1()
var successful = functioncall2()
var successful = functioncall3()
}
- tristaanogre9 years agoEsteemed Contributor
Well, see, it's a loop... so it will CONSTANTLY repeat... I wouldn't use "while" at all... these are individual conditions...
I mean, there are MANY ways of achieve the same effect but it comes down to instructing TestComplete that a) the function failed and b) what to do when it fails. Returning "successful" is one such thing.
What I do is I have each step in my test case as an object in an array. I then iterate the array (using a "for" loop) and call the "execute" method on each step... if the method is successful, I continue... if it's not, I call "break" and break out of the loop and then clean up the test case.
So... that's another way of doing it... each one of your "functions" could represent an array item that you can then loop through the length of the array and break if unsuccesful. Proof of concept...function test1() { Log.Message('step 1'); return false } function test2() { Log.Message('step 2'); return true; } function bar() { var myArray = []; myArray.push(test1); myArray.push(test2); for (var i = 0; i < myArray.length; i++) { if (!myArray[i]()) break; } }Running the bar function will result in my test log only showing that step 1 was run. If I change step 1 to return true, both functions execute.
- frank_vanderstr9 years agoContributor
I think I see what you're saying and how I can apply it to my current script. Thanks I will give this a try.
- frank_vanderstr9 years agoContributor
It seems whenever I do a array.Push(function()) my script stops executing after the function call.
- tristaanogre9 years agoEsteemed Contributor
leave the parenthesis out of your push statement... so it would be
myArray.push(function)
Now, if you're using the same function each time and need to have different parameters in each call, then it's a bit different. What you need to do, then, is create a function that will act as an object constructor. The constructor would take the parameters you need, assign them to properties, and then you would create new objects as each item in your array. It would look something like
function myTest(testValue) { this.testValue = testValue this.execute = function () { Log.Message('This is my test ' + testValue); return true; }; } function bar() { var myArray = []; myArray.push(new myTest('test1')); myArray.push(new myTest('test2')); ... for (var i = 0; i < myArray.length; i++) { if (!myArray[i].execute()) break; } }This is just a POC... depending upon what you want to do is how the constructor, etc., will be implemented.