Forum Discussion

John_Laird's avatar
John_Laird
Contributor
9 years ago

code snippet throws exception where same logic in script routine does not

My goal is to read a text file and set it to a variable in my keyword test. Simple. Got it done but noted an inconsistency between script routines and code snippets and wanted to understand why...

 

So I have this line of code in a script routine:

def TestCopyFile():
  KeywordTests.SwitchConfiguration.Variables.desired_olist = aqFile.ReadWholeTextFile("C:\\imports\\exportCompare\\lictest.txt", aqFile.ctANSI)

I have the same code in a 'run code snippet' operation (without the 'def' line of course):

KeywordTests.SwitchConfiguration.Variables.desired_olist = aqFile.ReadWholeTextFile("C:\\imports\\exportCompare\\lictest.txt", aqFile.ctANSI)

The script routine consistently executes without error but the code snippet always throws this exception to the TC log:

An error occurred while executing the code snippet: SyntaxError
invalid syntax

If I take out this variable setting logic from the code snippet operation:

KeywordTests.SwitchConfiguration.Variables.desired_olist = 

Then the snippet does execute without error. Not terribly useful though since nothing gets assigned. I can work around this but seems odd because that variable string is generated with the "Select Variable..." button so I would think it would be legal.

 

My question is, is setting a variable this way in a code snippet expected to cause an exception? Is this me doing something wrong, a bug, or a limitation?

 

Thanks,

John

  • Since you are doing a keyword test anyways, is there a reason why you are not setting the variable using the "Set Variable Value" keyword? When you do so, you have the option to set the value to the result of a code expression. You can then enter in the code expression your aqFile part of the code and that should work.  See the attached screenshot for reference.

     

     

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Since you are doing a keyword test anyways, is there a reason why you are not setting the variable using the "Set Variable Value" keyword? When you do so, you have the option to set the value to the result of a code expression. You can then enter in the code expression your aqFile part of the code and that should work.  See the attached screenshot for reference.

     

     

     

    • John_Laird's avatar
      John_Laird
      Contributor

      I was running the code snippet and then setting the variable in the next operation to 'Last Operation Result'. Your solution is better...didn't see that drop down item. Thanks.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Glad I could help!  Hopefully, this works for you.