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(); }
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.
- tristaanogre8 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_vanderstr8 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()
}
- tristaanogre8 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.
Related Content
Recent Discussions
- 13 hours agodhundley