Ask a Question

Calling python script from Test Complete

ritesh_chauhan
Contributor

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 5
karkadil
Valued Contributor

RE: Calling python script from Test Complete

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

RE: Calling python script from Test Complete


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


HKosova
Moderator

RE: Calling python script from Test Complete

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.

Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
ritesh_chauhan
Contributor

RE: Calling python script from Test Complete

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
Moderator

RE: Calling python script from Test Complete

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.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
cancel
Showing results for 
Search instead for 
Did you mean: