从java代码运行bat文件并等待它执行TestComplete脚本 - 没有可以做:(
我正在写这个与我之前的主题相关的问题:
从java代码运行bat文件以在txt文件中获得所需的结果 - 不能做:( 简而言之:我用java编写了一个运行bat文件的程序。此bat 文件运行TestComplete8 脚本来执行桌面应用程序测试。测试完成后,bat 文件会生成名为 result.txt 的文件,并向其中打印有关测试的信息。 我现在遇到了另一个问题:现在从我的java代码中我想等到bat运行完成。我通过循环直到名为 result.txt 的文件存在来做到这一点。我想这不是最好的解决方案,但我认为它可以工作,也尝试了不同的解决方案。发生的情况是,它将正常循环并等待文件存在,但 testcomplete 不执行测试。这很奇怪,因为 testcomplete 运行,我可以看到测试开始,我的 AUT 也开始,但什么也没有发生。 Testcomplete 正在等待任何对象,并且不会单击任何地方,只是等待直到预定义的操作时间用完。当我运行测试而无需等待代码完成时,一切都很好。我只是不明白为什么在启用等待时测试期间没有任何反应,以及为什么当我删除任何 do - while 或 waitFor() 时它工作正常,甚至我尝试在单独的线程中运行它。 :(
我有一种感觉,它可能与操作系统有关,并且与进程有关,因为它运行类似 bat 的进程,而不是 bat 运行它的子进程作为 testcomplete 或类似的东西。 感谢您的回答
源代码如下: 现在我正在尝试使用修改后的bat文件的解决方案:
@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
并且在最新版本中运行和等待的代码是:
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();
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 waits 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不了解更多问题的情况下,最可能的原因是从 Java 启动外部进程时遇到的常见问题。当启动外部进程时,在父进程和子进程之间创建三个流:输入、输出、错误。
您可以将它们比作 System.in、System.out 和 System.err。如果父进程 (Java) 不主动消耗输出流和错误流上的数据,则子进程可能会阻塞,因为操作系统将达到流上的缓冲区限制,并在消耗之前阻止更多写入。如果您的脚本写入标准输出或标准错误,则很可能出现这种情况。
我建议使用 apache commons-exec 来处理 Java 进程启动。
这是我知道有效的代码示例。
然后,如果您愿意,您可以在执行返回时检查输出/错误流。
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.
Then you can examine the output/error streams if you wish when execute returns.