Forum Discussion

ritesh_chauhan's avatar
ritesh_chauhan
Contributor
10 years ago

Calling python script from Test Complete

Hi



I am looking to call python script from Test complete.

Is this possible?



If not, then can I call a python script from a jscript?

Means that, this jscript will be called into the test complete which in turns finally call that python script.



is this possible?



If still not, is there any other possible way?





Looking forward to someones reply.



Regards,

Ritesh Chauhan

5 Replies

  • karkadil's avatar
    karkadil
    Valued Contributor
    Sys.OleObject("WScript.Shell").Run("C:\\Python27\\python.exe C:\\myfile.py");
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Because your Python script doesn't explicitly specify the file folder, it creates the file in the current working folder, which in case of TestComplete is the project folder (where the .mds file is).



    To create a file in a specific folder, you can do any of these:



    1) Use the full file path in your Python script.



    2) Use the aqFileSystem.SetCurrentFolder method to set the current working folder:



    aqFileSystem.SetCurrentFolder("C:\\");

    Sys.OleObject("WScript.Shell").Run("C:\\Python27\\python.exe C:\\test.py");





    3) Specify the working folder in the command line:



    Sys.OleObject("WScript.Shell").Run("cmd /c cd C:\\ && C:\\Python27\\python.exe C:\\test.py");



    Note: If you use Windows Vista+ and create the file in C:\, you need to run TestComplete as an Administrator.

  • Hello Gena,



    Thanks for your reply.

    I tried this but didn't worked out for me.



    Sys.OleObject("WScript.Shell").Run("C:\\Python27\\python.exe C:\\myfile.py");





    I have attached a sample python script (its in txt format, please change this into.py as this format was not attachable) which i tried to execute via TestComplete. This script generates a .txt file.



    My python file: test.py

    Path where it is placed: C:\\

    Output of this python file: test.txt



    I placed this test.py file in C:\ and then used below function in TestComplete as you suggested



    Sys.OleObject("WScript.Shell").Run("C:\\Python27\\python.exe C:\\test.py");





    But the output file test.txt didn't get generated.



    Please advice.



    Regards,

    Ritesh Chauhan


  • Thanks a lot Helen!



    It worked :)





    Here is the code:



    function Test()

    {

    aqFileSystem.SetCurrentFolder("C:\\");

    Sys.OleObject("WScript.Shell").Run("C:\\Python27\\python.exe C:\\test.py");

    Sys.OleObject("WScript.Shell").Run("cmd /c cd C:\\ && C:\\Python27\\python.exe C:\\test.py");

    }





    Regards,

    Ritesh Chauhan 

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Glad to help!



    Just one note - your code runs the Python script twice. Use either SetWorkingFolder + Run("C:\\..."), or just Run("cmd /c ..."), not both.