Forum Discussion

mortenb123's avatar
mortenb123
Contributor
5 years ago
Solved

python exception on ClickItem() not caught

I have some usb smartcards that switches adressing based on windows discovery, so they can show up ending with either 0 or 1, but I'm unable to catch when the clickitem() does not exists, it always fails.

    for reader in ["Gemalto USB Smart Card Reader {n}", "Gemalto IDBridge CT7xx {n}"]:
      for n in range(2):
        myreader = reader.format(n=n)
        try:  
          app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.ClickItem(myreader)
        except Exceptions as e:
          if n>=1:
            Log.Error(f"{e}")
          continue
        else:
Log.Message(f"found {myreader}") break

Thanks

  • Just to follow up. I rewrote the test to use dropdown and choose that is fetched:

     

     

      #Selects the 'Gemalto USB Smart Card Reader 1' item of the 'ComboboxOperatorCardReader' combo box.
      reader = "Gemalto USB Smart Card Reader"
      app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.Click()
      ComboBox = app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader
      for i in range(ComboBox.wItemCount):
        Item = aqString.GetListItem(ComboBox.wItemList, i)
        if reader in Item:
          Log.Message(f"found {Item}")
          app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.ClickItem(Item)
          break
    
      reader = "Gemalto IDBridge CT7xx"
      ComboBox = app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader
      # focus object missing name, Accessibility Insight gives error
      ComboBox.Click(335, 13)
      Sys.Keys("[Tab]")
      for i in range(ComboBox.wItemCount):
        Item = aqString.GetListItem(ComboBox.wItemList, i)
        if reader in Item:
          Log.Message(f"found {Item}")
          app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.ClickItem(Item)
          break 
    

     

    But in selenium try/except catches everything, must easier with exception handling.

3 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    > it always fails.

    More details as for your definition of 'fails', please? Error message from the test log if exists.

     

    > app.MainWindow.[...].ComboboxOperatorCardReader.ClickItem(...)

    As long as the ComboboxOperatorCardReader object exists and is of combo-box type (or any other object type that provides .ClickItem() method) no exception is expected here.

    Exception is a critical failure of the code been executed. In your case, code can be executed without failure. But the certain item might not exists and thus cannot be clicked. In this case you will get an error in the test log, but this is not a reason for exception throwing.

    What you need to do is check first if the required item to be clicked exists and proceed appropriately.

     

  • Just to follow up. I rewrote the test to use dropdown and choose that is fetched:

     

     

      #Selects the 'Gemalto USB Smart Card Reader 1' item of the 'ComboboxOperatorCardReader' combo box.
      reader = "Gemalto USB Smart Card Reader"
      app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.Click()
      ComboBox = app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader
      for i in range(ComboBox.wItemCount):
        Item = aqString.GetListItem(ComboBox.wItemList, i)
        if reader in Item:
          Log.Message(f"found {Item}")
          app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.ClickItem(Item)
          break
    
      reader = "Gemalto IDBridge CT7xx"
      ComboBox = app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader
      # focus object missing name, Accessibility Insight gives error
      ComboBox.Click(335, 13)
      Sys.Keys("[Tab]")
      for i in range(ComboBox.wItemCount):
        Item = aqString.GetListItem(ComboBox.wItemList, i)
        if reader in Item:
          Log.Message(f"found {Item}")
          app.MainWindow.TabControl2.TabControlConfig.ItemsControl.ComboboxOperatorCardReader.ClickItem(Item)
          break 
    

     

    But in selenium try/except catches everything, must easier with exception handling.

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      Thank you for the update and it is good to know that the problem is solved.

       

      in selenium try/except catches everything

      Selenium (like any other assertion-based library with atomic verifications) architecturally is designed around exceptions and throws exceptions on every failed line of code. Then one has two options: either handle possible expected exceptions (and do not see a problem unless carefully reading test log for details) or have failed test and the necessity to fix it and re-run.

      TestComplete, on the contrary, does not throw exceptions on failed actions over UI elements but instead posts a relevant (and clearly visible) message to the test log. What happens next depends on the test project settings: test may be aborted or continued. For some non-critical problems with access to UI elements in a long-playing tests the latter setting gives a chance to end the test and get result that can be reported (in an urgent situations). And then the test may be corrected and executed a-new.

      Also, possibility to continue is quite useful during test development.

       

      Just my $0.02 :)