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)
- screen pops up
- screen b pops up
- 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
Post a Comment