2

Please note that I am not a java guru. I might not use the right terminology, I learn java inside RFT on the fly. What is described below works exactly as stated.


In ruby we can code like

(code to execute) if (condition)

I want to do the same so my RFT (Rational Functional Tester) code is easy to read. I am going to call my custom functions in a way that looks like

        findANDclick(new String[]{"id", "menuButton"});
        findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); 
        findANDclick(new String[]{"src", ".*cycle_templates.*"});   

But the whole RFT script needs to finish and don't execute any other code in case of any of the findANDclick functions 'failed'. The function searches for an object with in html page and if it doesn't find any it throws new Exception via

throw new Exception("findANDclick: the object was not found");

so the instance of findANDclick ONLY throws an error so the next findANDclick is executed. But it makes no sense to continue as look for next object if the previous was not found and clicked on.

I was thinking that I can have a variable continue set to true and in case the exception is thrown the findANDclick will update it to false. Then I can do something like

    if (continue) { findANDclick(new String[]{"id", "menuButton"});}
    if (continue) { findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); }   
    if (continue) { findANDclick(new String[]{"src", ".*cycle_templates.*"});   }

it would be great if I can do something like

        { findANDclick(new String[]{"id", "menuButton"}); } if (continue)
        { findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); } if (continue)
        { findANDclick(new String[]{"src", ".*cycle_templates.*"}); } if (continue)
Radek
  • 13,813
  • 52
  • 161
  • 255

5 Answers5

5

That code is not pretty. Why not simply catch the Exception? That is how they are meant to be used. So something like:

try { 
   findANDclick(new String[]{"id", "menuButton"});
   findANDclick(new String[]{"src", ".*homeicon_calendar.*"});    
   findANDclick(new String[]{"src", ".*cycle_templates.*"});   
}
catch (Exception e) {
}

If any of the findANDclick calls fail, the subsequent ones will not be executed...

Btw, continue is a reserved word in Java, so you cannot use it as a variable name.

Mathias Schwarz
  • 7,099
  • 23
  • 28
  • I updated my question to make clearer that the exception is handled and thrown inside findANDclick. So it will never ends with an error. – Radek Jul 01 '11 at 07:15
  • @Radek In Java when the method `throws` an exception the next method can't be executed. So, Mathias's answer deserves your attention, although it doesn't look like you expected. – MockerTim Jul 01 '11 at 07:20
  • @MockerTim: maybe I am not describing the screnario well enough. The findANDclick throws an exception inside `try`. Then it finishes and next findANDclick is executed. My question here is how to make sure in a `nicely readable` way that the 'parent' script that calls findANDclick will finish – Radek Jul 01 '11 at 07:30
  • @Radek See my updated answer/comment. I think that this solution is *clear* and is *shortest* one. – MockerTim Jul 01 '11 at 07:45
  • 1
    Empty catch block?? You really mean to ignore NullPointerException, IllegalArgumentException, etc., with NO feedback to the developer? – artbristol Jul 01 '11 at 10:57
  • 1
    The code does exactly what the poster asked for. He asked for a specific way to rewrite the control flow of code he already (almost) had and this is that way. – Mathias Schwarz Jul 01 '11 at 11:46
  • @Mathias Empty `catch (Exception ex)` blocks are the devil's work when debugging. Even in test code. – artbristol Jul 01 '11 at 12:56
  • You point is valid but completely irrelevant to the question that was asked. – Mathias Schwarz Jul 02 '11 at 19:58
2

No, you cannot do this.

However, if the method throws an exception, why do you still need to check the continue flag then? It will stop anyways if the findANDclick fails.

wjans
  • 10,009
  • 5
  • 32
  • 43
  • No, it will not stop. The `throw Exception` sends an error message to the log. The instance of findANDclick finishes but I want the 'parent' that calls findANDclick to finish as well. Is that clearer? – Radek Jul 01 '11 at 07:11
  • 1
    So the exception is handled inside the `findANDclick` method already? – wjans Jul 01 '11 at 07:13
  • @wjans No! `findANDclick() throws Exception`. It doesn't handle it inside. – MockerTim Jul 01 '11 at 07:17
  • @MockerTim As far as I understand from the question and comments, in his scenario no exception is thrown, but there is an exception handled somewhere inside the `findANDclick`? – wjans Jul 01 '11 at 07:33
  • @Radek Ok, so you throw the exception yourself inside `findANDclick` but it's not propagated to the code where you call this method? – wjans Jul 01 '11 at 07:50
  • @wjans: it looks like. I don't know that I supposed to work this way :-) The `throw` is logged so I thought everything was ok. – Radek Jul 01 '11 at 07:53
  • If you do throw the exception correctly, and you don't catch it inside your `findANDclick` implementation, then execution should stop correctly witthout invoking consequent calls, and no checking for continue flag would be required. – wjans Jul 01 '11 at 08:00
  • @wjans: I thought you solved it :-) but if I don't catch the exception inside findANDclik I get `handled exception type Exception` error so I cannot run the script. – Radek Jul 01 '11 at 08:11
  • You should declare `findANDclick` with `throws Exception` and put all your calls to it in one `try` block. – wjans Jul 01 '11 at 08:15
  • I have all calls to `findANDclick in one try block. How do I declare it with `throws Exception`? – Radek Jul 01 '11 at 08:32
  • 1
    Add it to your method declaration, something like this: `private void findANDclick(String [] args) throws Exception {`. – wjans Jul 01 '11 at 08:35
  • @Radek My example answers your last question. Read it attentively. – MockerTim Jul 01 '11 at 10:43
  • I know I'm a bit late to the party, but RFT automatically catches and logs exceptions if they propagate to the highest level. That's the source of this confusion. – Yamikuronue Mar 14 '12 at 14:43
1

Radek! Try to execute the following example, just to clarify how the Java exception handling works.

public class ExceptionHandling {
    /*Method that throws exception*/
    static void methodOne() throws Exception {
        System.out.println("methodOne();");
        try {
            throw new Exception();
        } catch (Exception e) {
            System.out.println("Exception caught in methodOne(), worked up, and thrown again.");
            throw new Exception();
        }
    }

    static void methodTwo() {
        System.out.println("methodTwo();");
    }

    public static void main(String[] args) {
        try {
            methodOne();
            methodTwo();
        } catch (Exception ex) {
            System.out.println("Exception caught in main()!");
        }
    }
}

Output of this example:

methodOne();
Exception caught in methodOne(), worked up, and thrown again.
Exception caught in main()!

It shows that the second method is never executed, if the first one throws an exception.

P.S. This should be a comment. But it's clearer in rich formatting.

Solution suggested by Mathias Schwarz

try { 
   findANDclick(new String[]{"id", "menuButton"});
   findANDclick(new String[]{"src", ".*homeicon_calendar.*"});    
   findANDclick(new String[]{"src", ".*cycle_templates.*"});   
} catch (Exception e) {
   // Workup exception somehow.
}

has advantages to which you aspire (from Java developer's point of view):

  1. It's short;
  2. It's clear for Java developer.

Disadvantage is also clear: ugly exception handling construction.

But in Java you can't avoid it. It's language rules.

Community
  • 1
  • 1
MockerTim
  • 2,475
  • 1
  • 25
  • 31
0

You can't use if. You can use a do/while loop that more or less provides the syntax you are looking for.

do { findANDclick(new String[]{"id", "menuButton"}); } while (continue)
do { findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); } while (continue)
do { findANDclick(new String[]{"src", ".*cycle_templates.*"}); } while (continue)

BTW, like someone mentioned above, continue is a bad choice for a variable name.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
0

I think you should try this:

    String[][] params = {
        {"id", "menuButton"},
        {"src", ".*homeicon_calendar.*"},
        {"src", ".*cycle_templates.*"}
    };

    for (String[] param : params) {
        if ( findANDclick(param) ) break;
    }
ABueno
  • 1
  • 1