开发者

Run bat file from java code and wait for it to execute TestComplete script - no can do :(

I am writing this question which is related to my previous topic:

Run bat file from java code to get desired result in txt file - no can do :( In a shortcut: i wrote a program in java that runs a bat file. This bat file runs TestComplete8 script that performs desktop application test. After test is finished, bat file generates file called result.txt and prints information about test to it. I'm stuck with another issue right now: Now from my java code i would like to wait until the bat run is finished. I do that by looping until the file called result.txt exists. Not the nicesest solution i guess but I thought it could work, also tried different solutions. What happens is that it will loop fine and wait until file exists, but testcomplete doesn't perform the test. It is very strange, because testcomplete runs, i can see that test starts, my AUT starts as well, but than nothing happens. Testcomplete is waiting for any object and doesn't click anywhere just w开发者_开发知识库aits until predefined time for action runs out. When i run the test without any waiting done in code, everything is fine. I just don't understand why nothing happens during the test when waiting is enabled and why it works fine when i just remove any do - while or waitFor(), or even i tried running it in seperate threads. :(

I have a feeling that it may be somehow related to the OS and have something to do with processes as it runs something like a bat as process and than bat runs it's child process as testcomplete or sth like that. Thanks for any answers

Source code as asked: Right now i was trying a solution with modified bat file:

@ECHO OFF
"C:\Program Files (x86)\Automated QA\TestComplete 8\Bin\TestComplete.exe" "C:..." /r /p:projname PathToApp="C:\...p" Login=... Password=B1  /t:"KeywordTests|..." /exit

and the code to run and wait in latest version is:

        new Thread(new Runnable() {
        public void run() {
            File file = new File("D:\\");
            int exitValue = -1;

            try {
                Process process = Runtime.getRuntime().exec(batch, null, file);
                while (true) {
                    try {
                        exitValue = process.exitValue();
                        System.out.println(exitValue);
                        break;
                    } catch (IllegalThreadStateException e) {
                        // e.printStackTrace();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        System.out.println("Waiting for process...");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();


The most likely cause, without knowing more about the problem, is a common one faced when launching external processes from Java. When launching an external process three streams are created between the parent and child process, input, output, error.

You can liken these to System.in, System.out and System.err. If the parent process (Java) does not actively consume the data on the out and error streams the child process may block as the OS will reach a buffer limit on the stream and prevent any more being written until it is consumed. This is quite likely if your script writes to standard out or standard error.

I would recommend using apache commons-exec to handle Java process launching.

Here's a code sample that I know works.

    CommandLine commandLine = new CommandLine( "TestComplete8.bat" );
    commandLine.addArgument( ... );
    commandLine.addArgument( ... );

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue( 0 );
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    executor.setStreamHandler( new PumpStreamHandler( outputStream, errorStream ) );

    try
    {
        executor.execute( commandLine );
    }
    catch ( ExecuteException e )
    {
        // TODO: ...
    }
    catch ( IOException e )
    {
        // TODO: ...
    }

Then you can examine the output/error streams if you wish when execute returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜