Forum Discussion

PetGin's avatar
PetGin
Occasional Contributor
7 months ago

Unexpected behavior executing if, elif, else statement in Python

I wrote an if, elif, and else routine to checkpoint a header name and write the appropriate password into a required password field. When I ran the script routine it resulted in unexpected behavior. The first expression (checkpoint function) resulted in true but instead of executing the associated code block (touch & keys) and dropping out it ran the next elif, which as expected failed and then it ran the statements from the previous if statement. It completed the task but ultimately failed the test.

I’ve run similar if statements before without issue. Please see attached code snippet and log.

Any thoughts as to why this would be happening would be appreciated.

  • headerDeviceName can only contain "SR-506-0801" OR "SR506-0701" text, is that correct?

    Since you are calling check property for both text, on lines 1 and 2. If the verification succeeds, the method posts a success message to the test log; otherwise it posts a failure message. Hence, check property is failing on "SR506-0701", line 28.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I'm guessing it's relating to RefreshMappingInfo method, as this clears the cached object reference list. The new list is probably being generated, but the second statement is called and the object has not appeared in the list yet. Either remove the method or add a delay.

    You also have two functions doing the same thing. Why not have just the one function and pass in the parameter string, to perform check property?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    headerDeviceName can only contain "SR-506-0801" OR "SR506-0701" text, is that correct?

    Since you are calling check property for both text, on lines 1 and 2. If the verification succeeds, the method posts a success message to the test log; otherwise it posts a failure message. Hence, check property is failing on "SR506-0701", line 28.

    • PetGin's avatar
      PetGin
      Occasional Contributor

      rraghvaniI rewrote the routine based on your suggestions and it's now working as expected. 

      def checkSRName_WritePW():
          # Check attached device name to write the correct factory default password.
          if checkPoint_deviceName("SR-506-0801", factPass1") == True:
              Log.Message("Password write successful")
          elif checkPoint_deviceName("SR506-0701", "factPass2") == True:
              # Write factory default password.
              Log.Message("Device name found")    
          else:
              # Log a message on failure.
              Log.Message("Device name not found")
      
      
      def checkPoint_deviceName(device_name, password):
          # Checks whether the 'Text' property in the object contains the controller name.
          check_device_string = aqObject.CheckProperty(Aliases.Device.processTacocomfortControl.connectedControllerGroupView.headerDeviceName, "Text", cmpContains, device_name)
          if check_device_string:
              # Write factory default password.
              Aliases.Device.processTacocomfortControl.connectedControllerGroupView.deviceSettingsHeadingGroup.passwordRequiredDialogPasswordField.Touch()
              Aliases.Device.processTacocomfortControl.connectedControllerGroupView.deviceSettingsHeadingGroup.passwordRequiredDialogPasswordField.Keys(password)
              Log.Message("Password write successful")
              return True
          else:
              Log.Message("Password write unsuccessful")
              return False

       

      • rraghvani's avatar
        rraghvani
        Champion Level 3

        Note, if line 3 is not true, then line 15 aqObject.CheckProperty will log a failure and will stop your automation - depending on your project configurations.

        Try using aqObject.CompareProperty method instead, as this will return either true or false and not stop the automation - providing you don't use lmError

  • PetGin's avatar
    PetGin
    Occasional Contributor

    Good points. I'll remove the RefreshMappingInfo for now and try it again. As the function it was initially for clarity but once I get this working I can update it to use a single function and pass in the appropriate parameter string. Thanks

  • PetGin's avatar
    PetGin
    Occasional Contributor

    rraghvaniunfortunately even with the RefreshMappingInfo commented out I get the exact same reults.