Forum Discussion

diwakar_sh12's avatar
diwakar_sh12
Occasional Contributor
2 years ago

Issue with Data-Driven Loop, script is not gracefully exiting on error

We have a requirement to create multiple accounts. All the test data are being passed from an excel sheet, for that a data-driven loop is used. 

Each row in the Excel sheet is treated as a different test case.

 

Issue Detail:-

If one test case failed due to test data or any other issue, the script should mark that particular test case( row of an excel sheet) as failed and should pick the next row for execution. The problem is here that if the Playback setting is set as Continue on error, then in this case script is not exiting that case and continuing with the next steps.

If we set "Stop current item" in the playback setting, its moves to the next test case but the script is not entering any values in the next case and getting failed due to the "Operation interrupted by the user" error, and all the further test cases also got failed due to similar issues.

 

Please suggest a way to resolve this issue. Thanks

 

Sample Code for reference

 

code is similar to below

 

dataVar=DDT.ExcelDriver("c:\data\testdata.xls","Account",True)

while not dataVar.EOF():

    try:

      aqTestCase.Begin(dataVar.Value[1])

      login()

     fillDataInAccountForm(dataVar)

     logoff()

     aqTestCase.End()

   except Exception as e:

      Log.error("Error Occured"+str(e))

   dataVar.next()

 

 

 

13 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Hi diwakar_sh12, not sure if you saw my previous response, but FindElement() does not throw an error. It either returns the Object or None. You need to raise an exception if the object is not found, for the try...except statement to work

     

    For example, in the following code, Log.Message("Error") will never been called if FindElement() returns None

     

    def test1():
        obj = None
        try:
            obj = Sys.Browser("chrome").Page("*").FindElement("//a[@title='Contact Me']")
        except:
            Log.Message("Error") # Never called!
        Log.Message("Continue...")

     

    You need to do something like this

     

    def test1():
        try:
            DoSomething()
        except Exception as ex:
            Log.Message(ex)
    
    def DoSomething():
        obj = None
        obj = Sys.Browser("chrome").Page("*").FindElement("//a[@title='Contact Me']")
        if obj is None:
            raise Exception("Object not found")

     

    • diwakar_sh12's avatar
      diwakar_sh12
      Occasional Contributor

      Thanks, rraghvani for your input, I missed your earlier comment. You have suggested a good point here, let me try and come back to you on this.

       

      Thanks,

      Diwakar

       
       
       
    • diwakar_sh12's avatar
      diwakar_sh12
      Occasional Contributor

      Hi rraghvani,

       

      Based on your suggestion I've tweaked my code. I've removed Try and Except block and used the OnLogError event to handle the error. I've added a project variable to increase error count ( we can not reset Log.errCount value), based on this project variable value I'm making the decision to exit the loop or to enter another function in the loop. I'm resetting this project variable value at the start of every test case.

       

      Thanks,

      Diwakar

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    Do you expect all of this data to work or do you have some planned failures?

    • diwakar_sh12's avatar
      diwakar_sh12
      Occasional Contributor

      Hi Marsha_R,

       

      Thanks for looking into this issue.

      We are expecting all data to be worked however during execution some may fail in validation. In a failure scenario, our expectation is to fail that particular test case and pick the next in the row.

      Thanks,

      Diwakar

       

      • Marsha_R's avatar
        Marsha_R
        Champion Level 3

        I don't know your syntax but it sounds like your Try and Next are working ok together, but when you hit the Exception, it doesn't know how to go on. I would try putting another Next under the Exception and see what happens.

         

         

         

         

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You are using the FindElement() method which either returns the Object that matches the search condition or returns None if condition not matched. FindElement() does not throw an error.

     

     

  • Hi diwakar_sh12 ,

     

    I am not that proficient with python, but you might want to put the try/except block inside of the while loop, that way it can continue with the next entry if an error is raised. I'm not certain of the exact syntax/format, but something like this might work:

     

      while not ddv.EOF
        aqTestCase.Begin("Test Case" + ddv.Value[0]);
        try:
          openApp()
          clickObject(contactUs)
          waitTillPageLoads()
          fillContactForm(ddv)      
        except:
          Log.Error("Error Occured")
        closeBrowser(Project.Variables.browser)    
        aqTestCase.End()
        ddv.Next()

     

    I hope this helps.

    • diwakar_sh12's avatar
      diwakar_sh12
      Occasional Contributor

      Hi Chris,

       

      Thanks for looking into this issue. Actually, I've already tried that approach however no luck. What is happening here, if there are 10 objects on a page, and an error occurred at the second object then the script marks the step as a failure but at the same time it continues to perform the action on other objects on the page, it should skip that iteration completely and pick another in a loop. 

       

      If you have any other solution to achieve this please let me know, I'll tweak my code accordingly.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    During design, you need to decide how your want your automation to behave if a failure occurs. Such that,

     

    1. If an error occurs, continue with remaining steps?
    2. If an error occurs, stop automation?
    3. If number of errors logged, is greater than number of errors allowed, stop current test and move on to the next test?
    4. If an error occurs, stop current test and move onto the next test?

    Within my automation project, I use name mappings and I also use the Find method for dynamic objects. If a UI Object is not found, for whatever reasons, I stop the current test and move onto the next test. Once the automation is finished, I can then report on the number of tests passed/failed. I can then afterwards look into the failed tests.