Forum Discussion

aghanem's avatar
aghanem
New Contributor
14 years ago

How can I run .pjs using c#

Hi,

I have a ready test project (.pjs) and I'm now in the process of writing a console which will be confgireud to run the test on a schedules task.I was wondering how can I proghrmatically run the (.pjs) file using .net classes ? shall I use the connect apps dlls ? if so , then how ? I also want to dynamically pass the path of the .exe application that is being tested from my c# to the .pjs test package, is that possible ?



Thanks a lot for your help.

3 Replies

  • Hi Anas,


    You can run TestComplete from your C# code in the same way as you would run any other application. See the code below. A possible alternative is to run TestComplete via COM, but, I think, simulating a command-line launch is simpler in your case.


    To pass the tested executable's path to the test, you can use environment variables (again, see the code below :-). As an alternative approach, you can create a text file with the needed file path in Notepad and then, read the file contents from your TestComplete script. But for me, using an environment variable seems to be easier in your case.


    Here is the C# code for running TestComplete via the command line. It runs TestComplete, loads the "My Test Project.mds" file in it and then, runs the Script | Unit1 | Test1 script routine. For more information on TestComplete command-line arguments, see http://smartbear.com/support/viewarticle/10998/. In my example, I used a project file (.mds), but you can also open project suite files (.pjs) in the same way.




    // Create a process object

    System.Diagnostics.Process process = new System.Diagnostics.Process();


    // Specify the executable file name

    process.StartInfo.FileName = "C:\\Program Files\\Automated QA\\TestComplete 8\\Bin\\TestComplete.exe";

               

    // Specify command-line arguments.

    // Don't forget to include the parameters that contain spaces in quotes.

    string ProjectPathArg = "\"E:\\Work\\Test\\My Test Project\\My Test Project\\My Test Project.mds\"";

    string MoreCmdLineArgs = "/run \"/project:My Test Project\" /test:\"Script|Unit1|Test1\"";

    process.StartInfo.Arguments = ProjectPathArg +" " + MoreCmdLineArgs;

               

    // Pass data via the MyVariable environment variable

    process.StartInfo.EnvironmentVariables.Add("MyVariable", "C:\\Windows\\Notepad.exe");

    process.StartInfo.UseShellExecute = false; // Assign false to this property to use environment variables


    // Run the process

    process.Start();


    Here is the way you can use the MyVariable environment variable in your TestComplete test:




    Sub Test1

      ' Get the value of your environment variable

      Value = aqEnvironment.GetEnvironmentVariable("MyVariable")

      Log.Message Value


      ' Modify the tested app properties

      ' We clear the Path property as MyVariable contains the full path to the executable 

      TestedApps.MyTestedApp.Path = ""  

      TestedApps.MyTestedApp.FileName = Value

     

      ' Run the tested app

      TestedApps.MyTestedApp.Run  

    End Sub


    A few words on using the environment variable: this variable contains the path to the exe to be launched. To do this, I'd suggest creating a tested application in your TestComplete project, modifying its properties and then, running this application using the TestedApps.AppName.Run method.

    It does not matter what application you add to the project (you can add, for example, Notepad), since the application's path and name will be changed before the run. However, I'd suggest that you give the tested app item some meaningful name for easier reference from tests. For instance, in my example, I added notepad.exe and then, replaced the tested app's original name (notepad) with MyTestedApp. See the attached image.

  • aghanem's avatar
    aghanem
    New Contributor
    Hey Alex,

    Thanks a lot for the great answer! You are really a wonderful guy.



    Regards,