3

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?

1
  • That process.waitFor(1, TimeUnit.MINUTES) is helping, but it will PASS the script even when the AutoIt script is failed.
    – Sandeep
    Jul 28, 2016 at 5:17

3 Answers 3

6

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.

3
  • That process.waitFor(1, TimeUnit.MINUTES) is helping, but it will PASS the script even when the AutoIt script is failed.
    – Sandeep
    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
    Jul 28, 2016 at 12:14
  • nice elaboration paul ! voted up :) Jul 30, 2016 at 6:54
1

Even why cant you try "dependsOnMethods", This will be useful in that sense.

Check out below example for use:

@Test (priority=2, dependsOnMethods={"CreateNew"})
3
  • because the first will finished before the .exe is finished and the second will be started instantly. 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 try Jul 27, 2016 at 14:01
  • @NielsvanReijmersdal Solvable by waiting for the process to end.
    – Paul Muir
    Jul 27, 2016 at 15:09
0

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.

1
  • 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. Jul 27, 2016 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.