I want the Script to Openfile.exe
only if the previous one i.e CreateNew.exe
has finished executing.
Right Now whats happening is, when I run TestNG.xml
file it runs all the @Test
priority wise and within 5 sec TestNg
output console shows all the @Test
are passed.
While my AutoIT scripts are still running in parallel in background.
The code is as follows:
@Test (priority=1)
public void CreateNew() throws Exception
{
Runtime.getRuntime().exec("exeFiles\\CreateNew.exe");
}
@Test (priority=2)
public void OpenaFile() throws Exception
{
Runtime.getRuntime().exec("exeFiles\\OpenaFile.exe");
}
And the code of AutoIt file is as Follows:
createnew()
Func createnew()
Sleep(2000)
Run("Mspaint.exe")
WinWaitActive("Untitled - Paint")
Send("!f")
Sleep(1000)
Send("n")
Sleep(2000)
WinClose("Untitled - Paint")
EndFunc ;==>createnew
A possible solution for this can be:-
- In your Java/Selenium code create a temparory lockfile
- Start Autoit.exe
- In your Java/Selenium code write a loop to check if lockfile still exists
- Last step in the AutoIt code removes the lockfile
- If the lockfile doesn't exist break out of loop and continue with the next code
Now does anyone know how to create and use this lockfile in code?
-
That process.waitFor(1, TimeUnit.MINUTES) is helping, but it will PASS the script even when the AutoIt script is failed.Sandeep– Sandeep2016年07月28日 05:17:11 +00:00Commented Jul 28, 2016 at 5:17
3 Answers 3
My answer borrows from NarendraC's answers.
@Test (priority=1)
public void CreateNew() throws Exception
{
Process p = Runtime.getRuntime().exec("exeFiles\\CreateNew.exe");
p.waitFor();
}
@Test (priority=2, dependsOnMethods={"CreateNew"})
public void OpenaFile() throws Exception
{
Runtime.getRuntime().exec("exeFiles\\OpenaFile.exe");
}
The issue, from what I could tell, was that the first method exited prior to completion. By adding the wait onto the process it will wait for it to end. This will, I believe, give you the desired result.
-
That process.waitFor(1, TimeUnit.MINUTES) is helping, but it will PASS the script even when the AutoIt script is failed.Sandeep– Sandeep2016年07月28日 05:00:56 +00:00Commented Jul 28, 2016 at 5:00
-
Well, depending on how the AutoIT script fails, you can assign the result of the p.waitFor() to an int which will give the result. I would recommend a new question if you need further details.Paul Muir– Paul Muir2016年07月28日 12:14:35 +00:00Commented Jul 28, 2016 at 12:14
-
nice elaboration paul ! voted up :)Narendra Chandratre– Narendra Chandratre2016年07月30日 06:54:20 +00:00Commented Jul 30, 2016 at 6:54
Even why cant you try "dependsOnMethods", This will be useful in that sense.
Check out below example for use:
@Test (priority=2, dependsOnMethods={"CreateNew"})
-
because the first will finished before the .exe is finished and the second will be started instantly.Niels van Reijmersdal– Niels van Reijmersdal2016年07月27日 13:52:53 +00:00Commented Jul 27, 2016 at 13:52
-
Oh, Its my bad. Can you try - Keep openFile.exe on Priority=1 and createNew on Priority=2. and then implement dependsOnMethods, It will definitely give you colorful result. Give a tryNarendra Chandratre– Narendra Chandratre2016年07月27日 14:01:46 +00:00Commented Jul 27, 2016 at 14:01
-
@NielsvanReijmersdal Solvable by waiting for the process to end.Paul Muir– Paul Muir2016年07月27日 15:09:32 +00:00Commented Jul 27, 2016 at 15:09
I think a better (though more complicated) way of doing it, instead of thread sleep, is to use a messaging queue and publish/subscribe model.
-
how would the java code communicate with the AutoIt code to implement a publish/subscribe model? Doesnt that only work within one single piece of code.Niels van Reijmersdal– Niels van Reijmersdal2016年07月27日 13:56:59 +00:00Commented Jul 27, 2016 at 13:56
Explore related questions
See similar questions with these tags.