Forum Discussion
The problem is that the "does not exist" is not a JavaScript code exception so it's not going to fail as a code exception. This is why the "catch" block isn't catching it.
The other problem is that you don't have any checking in your code to detect whether or not you actually found the item you are looking for before you click it. If that's your desire, try this (altered/added code in bold):
try {
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();
}
catch (error) {
Log.Error("Category group error occured: " + error.message);
ClickMyAccount();
Logoff();
}
- frank_vanderstr9 years agoContributor
Thanks, I think that is what I am looking for.
My next question is how to best exit the current script and move to the next one, since so far even if the exception is caught, my script attempts to perform later steps.
- tristaanogre9 years agoEsteemed Contributor
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.
- frank_vanderstr9 years agoContributor
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()
}