Forum Discussion

aafreen_jamadar's avatar
aafreen_jamadar
Occasional Contributor
11 months ago

Python conditional statements

Hi! Has anyone been facing an issue with conditional statements using python?

I am to execute a code which checks for a file in a particular folder and deletes it if found. Also, I have been having issues using inputs from the keyboard. The code terminates whenever an input from the keyboard is required.

 

PS: any examples with if-else statements (python only) would be appreciated.

16 Replies

  • Conditional statements in Python , including if, elif, and else, are fundamental for controlling the flow of a program. With the if statement, you can execute a block of code if a specified condition is true. The elif statement allows you to check multiple conditions sequentially, providing an alternative path when the initial condition isn't met. Finally, the else statement is used to define what happens when none of the preceding conditions are true. These statements are essential for building decision-making structures in your code, enabling you to perform different actions based on varying circumstances. Proper indentation is crucial in Python as it determines the scope of code blocks, so pay close attention to indentation when using these conditional statements.

     
  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Do you have example code of it not working?

     

    What version of TestComplete are you using?

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      I am currently on version 15.52.2.7. 

      Following is the code with the keyboard input:

        def start():
            hwndSource = Aliases.I_Software.HwndSource_LogonWindow
            comboBox = hwndSource.LogonWindow.ProfilesComboBox
            comboBox.DropDown()
            hwndSource. Keys("a[Enter]")
            comboBox.Keys("[Enter]")
            time.sleep(30)

      As far as conditional statements go, I haven't been able to figure out a way to script it.

      (I am assuming testcomplete has a different syntax/ built in functions that I'm not used to)

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I would like to see the "Python conditional statements" that is not working for you, as you started your first sentence with this question.

     

    In the coding you have provided, you are clicking on the dropdown of comboBox, then entering 'a' followed by 'return' on the hwndSource, then entering 'return' on the comboBox. This doesn't deem to be consistent.

     

     

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      Here's the code for the conditional statements:

      def Test2():
        I_Software = Aliases.I_Software
        button = I_Software.HwndSource_HomeScreenWindow.HomeScreenWindow_.RemoveImagesButton
        if aqObject.CheckProperty(button.Image, "Enabled", cmpEqual, True):
          imageSelector = I_Software.HwndSource_ImageSelector.ImageSelector
          button.ClickButton()
          imageSelector.SelectAllButton.ClickButton()
          imageSelector.OKButton.ClickButton()
        else:
          pass
        homeScreen = I_Software.HwndSource_HomeScreenWindow.HomeScreenWindow_
        homeScreen.ImportImageButton.ClickButton()
        I_Software.dlgImportImage.OpenFile("C:\\Users\\Test Complete\\Desktop\\Samples\\Samples\\Indian.jpg")
        I_Software.HwndSource_MessageBox.MessageBox.OKYesButton.ClickButton()
        listView = homeScreen.GalleryList
        listView.HScroll.Pos = 0
        listView.DblClickItem(0)

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If the property value of "Enabled" is True for button.Image, then the statement is true. Which means it will perform the following,

    imageSelector = I_Software.HwndSource_ImageSelector.ImageSelector
    button.ClickButton()
    imageSelector.SelectAllButton.ClickButton()
    imageSelector.OKButton.ClickButton()

    else, pass.

     

    What's the exact issue with the conditional statement?

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      The problem here is that it performs the statements mentioned above if the button is enabled, but terminates the code if it is not.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you get the following appearing,

    Then change the project settings, on error to continue running,

    The results will look like this afterwards,

     

    This reason it fails - "If the verification succeeds, the method posts a success message to the test log; otherwise it posts a failure message."

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      This is what I see now. the digital import object is present in the object browser and the image was imported before I changed the settings to 'continue running'.

      Suddenly, it does not recognize the object.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Because the checkpoint has failed, it's not performing the condition in the if statement.

     

    If you debug, and step through your coding. You will see exactly what it's doing.

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      The is the point, It is only looking for the condition in the if statement.

      It is not doing anything after that. It doesn't execute the statements in else and anything after that at all.

       

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Each of the log statements corresponds to the line of code,

     

    The 'else' part of your statements just has 'pass' and nothing else.

     

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      It is compiling the following command:

      homeScreen.ImportImageButton.ClickButton().

      This was after the else statement which implies that 'pass' worked. 

      But then, why is it not going beyond that?

      It says dlgImportImage does not exist but I do see it in the object browser.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    This is how I see your code,

     

    def Test2():
    	button = Aliases.I_Software.HwndSource_HomeScreenWindow.HomeScreenWindow_.RemoveImagesButton
    	
    	if aqObject.CheckProperty(button.Image, "Enabled", cmpEqual, True):
    		imageSelector = Aliases.I_Software.HwndSource_ImageSelector.ImageSelector
    		button.ClickButton()
    		imageSelector.SelectAllButton.ClickButton()
    		imageSelector.OKButton.ClickButton()
    		
    	Aliases.I_Software.HwndSource_HomeScreenWindow.HomeScreenWindow_.ImportImageButton.ClickButton()
    	
    	Aliases.I_Software.dlgImportImage.OpenFile("C:\\Users\\Test Complete\\Desktop\\Samples\\Samples\\Indian.jpg")
    	Aliases.I_Software.HwndSource_MessageBox.MessageBox.OKYesButton.ClickButton()
    	
    	listView = Aliases.I_Software.HwndSource_HomeScreenWindow.HomeScreenWindow_.GalleryList
    	listView.HScroll.Pos = 0
    	listView.DblClickItem(0)

     

     

    If TestComplete is not able to find the object on screen, then it means your actions are not correctly defined or your name mappings may have changed. Debug and step through the code, at the point of failure, check the name mapping and ensure TestComplete is able to identify the correct object.

     

    Also, you might need to have a wait statement, for dlgImportImage to appear.

    • aafreen_jamadar's avatar
      aafreen_jamadar
      Occasional Contributor

      How do I check if the name mapping has changed?

      I just can't seem to understand what's causing this.