Forum Discussion

mengbinhao's avatar
mengbinhao
Contributor
2 years ago

Script can not goto else branch

Hi All,

   I use Javascript for the automation of my target Desktop application,  Below is my code snapshot,  what confuses me is if the result of condition 1 is false (TC throw error: window1 does not exist),  then the code did not go to the else if condition,  it still executes doSomething1(), Is there anything that I was missing? Please give me some guidance. Thank you in advance.

 

if (window1.VisibleOnScreen) {
  doSomething1()
} else if (window2.VisibleOnScreen) {
  doSomething2()
} else {
  doSomething3()
}

 

 

  • In order to check the property value of VisibleOnScreen, the object window1 must exist. You can test via the following code,

    if (window1.Exists) {
      if (window1.VisibleOnScreen) {
        doSomething1()
      } else if (window2.VisibleOnScreen) {
        doSomething2()
      } else {
        doSomething3()
      }
    } else {
      Log.Warning("window1 does not exist.")
    }

     ...also need to check the existence of window2.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    In order to check the property value of VisibleOnScreen, the object window1 must exist. You can test via the following code,

    if (window1.Exists) {
      if (window1.VisibleOnScreen) {
        doSomething1()
      } else if (window2.VisibleOnScreen) {
        doSomething2()
      } else {
        doSomething3()
      }
    } else {
      Log.Warning("window1 does not exist.")
    }

     ...also need to check the existence of window2.