1

I run all my scripts through a .suite-file roughly in the following form:

_include("variables.sah");
_include("functions.sah");

$a = 0;
while($a<3) {
    try {
        _navigateTo($loginpage);
        login($user, $password);
        myFunction();
        $a = 3
    }
    catch (e){
        _navigateTo($loginpage);
        login($user, $password);
        //undo changes made by myFunction()
        ...
        $a++;
        if($a<3) {
            _log("Try again");
        }
        else {
            _log("Skip to next script");
        }
    }
}

function myFunction() {
    //do this
    ...
}

Now all this runs perfectly fine, except for one thing: it doesn't repeat when it encounters a missing element which under normal circumstances would abort all scripts. It simply ignores the error and moves on to the next line of the suite. How do I make my script retry up to 2 times before moving on, if I don't know which part (if any) is going to fail and when?

hiyosilver
  • 27
  • 5

1 Answers1

1

Your code looks fine I guess.

One thing I can think of is that the exception is thrown in the catch block.

I made a simple script which works as intended:

var $errors = 0;

function trySet() {
  try {
    _setValue(_textbox("does not exist"), "");
  } catch ($e) {
    $errors++
    _alert($errors);
  }
}


for (var $i = 0; $i < 3; $i++) {
  trySet();
}

Better figure out where exactly your script runs into problems and handle them with separate try-catch blocks accordingly. How you handle the exceptions is up to you but I guess it would be something like:

try {
  login()
} catch ($e) {
  // login failed, try again
}
try {
  myfunction()
catch($e) {
  revertMyFunction()
  //try again
}

Maybe define your own exceptions to differently react to errors, have a look at this for more info on custom exceptions: Custom Exceptions in JavaScript

Regards Wormi

Community
  • 1
  • 1
globalworming
  • 757
  • 1
  • 5
  • 33
  • Hi globalworming, you are spot on with the separate try-catch blocks. That was my original intention aswell. There are three issues: 1. I have no way of knowing which parts are going to fail (it's mostly to do with timeouts because the test system can be slow and unreliable). 2. Even if I did, I have no idea how I would specifically adress these issues (do I need to id them somehow? How would I know what the error is 'called', exactly?). 3. It would mean a lot of annoying extra work for me to set this up for every single script. :) – hiyosilver Apr 24 '14 at 07:34
  • 1
    Sahi is throwing objects like {"message":"_setValue [...] Error: The first parameter [...] not found [...]","debugInfo":"[..]"}); You can identify specific errors by matching the message like $e.message.match("not found"). You can throw your own exceptions as well, with easier identification. What exactly you should do on errors.. depends. I try to update my answer to address this. – globalworming Apr 24 '14 at 08:22
  • Hey, sorry for opening this up again, but I don't think my problem warrants its own question: do you have an idea how I could force my try-catch to ignore assertion failures? That is, if the script encountered no errors besides assertion failures, to ignore those and don't trigger the catch? – hiyosilver Apr 25 '14 at 14:49
  • Mh, going through my logs again, it seems that assertion failures are intermittently caught and then not caught. There doesn't seem to be any pattern to it, sometimes the catch is triggered sometimes it's not... but thank you for making the effort once again. I was already strongly suspecting that I would have to live with this. – hiyosilver Apr 28 '14 at 07:24