java - Modifying try-catch statements to reduce clunky code -


i've been working sikulix try atdd. code works when i 1 working it. transferring below code else counter-productive irrespective of how comment code.

int numoftries; while (!isfinishstage && numoftries != 3) {                 numoftries++;                 try {                     temp = new pattern("imgs/img1.png").similar(0.9f);                     s.wait(temp, 1);                     s.find(temp);                     s.hover(temp);                     isfinishstage = true;                     break;                 }catch (findfailed ff1) {                     try {                     temp = new pattern("imgs/img2").similar(0.5f);                     s.wait(temp, 1);                     s.find(temp);                     s.hover(temp);                     isfinishstage = true;                     break;                 } catch (findfailed ff2) {              try{                     temp = new pattern("imgs/img3");                     s.wait(temp, 1);                     s.find(temp);                     s.click(temp);                 } catch (findfailed ff3) {                     continue;              }         }     } } 

a findfailed exception thrown once pattern/image cannot matched against on screen (similarity adjusts tolerance level). current gui automating, there 3 possible scenarios (where piece of code comes play)

  1. screen pops up
  2. screen b pops up
  3. neither 1 or 2, instead 'next' pops up

thus check screen a, if not check screen b, if not check next, if not, repeat cycle until exceed number of tries — meaning test has failed.

with way sikuli works or atleast how i've been interpreting it, have perform various loops through multiple try-catch statements seems little off putting.

ps: idea behind above code get work. if there ambiguity let me know can clarify.

the following code (i think) equivalent code:

int numoftries; while (!isfinishstage && numoftries < 3) {     numoftries++;     if (trypattern(s, "imgs/img1.png", 0.9f) ||         trypattern(s, "imgs/img2", 0.5f)) {         isfinishstage = true;     } else {         // note third "attempt" inconsistent         // others because don't set isfinishedstage.         trypattern(s, "imgs/img3", 1.0f)     } }  private boolean trypattern(someclass s, string path, float similarity) {     try {         pattern temp = new pattern(path);         if (similarity != 1.0f) {             temp = temp.similar(similarity);         }         s.wait(temp, 1);         s.find(temp);         s.hover(temp);         return true;     } catch (findfailed ff) {         return false;     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -