Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify long list of similar if statements?

I work as an automation engineer for my company. Recently, I wrote a piece of code that my manager absolutely would not accept.

I was asked to write some scripts for test cases involving different pieces of the GUI. The part of the code my manager would not accept was an if/else statement meant to check the current language of the prompt in the GUI.

I've been instructed to use Sikuli, and as such, it is very important that I know what language the application is currently set to so my scripts can click the correct buttons (which change depending on the language).

My thoughts were that the code iterates through the if/else statement and then points to the correct button. Example: The if/else statement determines that the "ok" button is currently the Suomi translation, so it will than click the correct button.

Here is an example of my code:

switch (button) {
case "ok":
    if (s.exists("imagerepo/language/catalan_ok.png") != null) {
                            s.click("imagerepo/language/catalan_ok.png");
                        } else if (s.exists("imagerepo/language/suomi_ok.png") != null) {
                            s.click("imagerepo/language/suomi_ok.png");
                        } else if (s.exists("imagerepo/language/italian_ok.png") != null) {
                            s.click("imagerepo/language/italian_ok.png");
                        } else if (s.exists("imagerepo/language/portuguese_ok.png") != null) {
                            s.click("imagerepo/language/portuguese_ok.png");
                        } else if (s.exists("imagerepo/language/english_ok.png") != null) {
                            s.click("imagerepo/language/english_ok.png");
                        } else if (s.exists("imagerepo/language/dutch_ok.png") != null) {
                            s.click("imagerepo/language/dutch_ok.png");
                        } else if (s.exists("imagerepo/language/spanish_ok.png") != null) {
                            s.click("imagerepo/language/spanish_ok.png");
                        } else if (s.exists("imagerepo/language/french_ok.png") != null) {
                            s.click("imagerepo/language/french_ok.png");
                        } else if (s.exists("imagerepo/language/latina_ok.png") != null) {
                            s.click("imagerepo/language/latina_ok.png");
                        } else if (s.exists("imagerepo/language/chinese_ok.png") != null) {
                            s.click("imagerepo/language/chinese_ok.png");
                        }
break;
...etc..

My only gripe with the above code is that it is pretty ugly. Functionally it does exactly what I'd like it to, 100% of the time.

EDIT: I figure that having a switch that adapts to the potentially changing button would be better than having 10 switches for the same button. Arguably, against what I just said, if I'm writing the scripts, I will always know what language the system is going to be in.

If this is an example of poor code, what could I do instead to determine which "form" of the button I need to press?

It would be worth noting now that the answer I'm looking for does not actually pertain to testing at all, but rather, how do I optimally perform the function of that if/else block above?

like image 788
jagdpanzer Avatar asked Jan 13 '16 15:01

jagdpanzer


1 Answers

Something like:

String[] languages = {
  "catalan_ok.png",
  "suomi_ok.png",
  //...
}

for (String base : languages) {
  String file = String.format("imagerepo/language/%s", base);
  if (s.exists(file) != null) {
    s.click(file);
    break;
  }
}

perhaps? Not tested.

like image 64
Vlad Avatar answered Nov 04 '22 17:11

Vlad