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?

  • That process.waitFor(1, TimeUnit.MINUTES) is helping, but it will PASS the script even when the AutoIt script is failed. – Sandeep Jul 28 '16 at 5:17
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.

| improve this answer | |
  • That process.waitFor(1, TimeUnit.MINUTES) is helping, but it will PASS the script even when the AutoIt script is failed. – Sandeep Jul 28 '16 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 '16 at 12:14
  • nice elaboration paul ! voted up :) – NarendraC Jul 30 '16 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"})
| improve this answer | |
  • because the first will finished before the .exe is finished and the second will be started instantly. – Niels van Reijmersdal Jul 27 '16 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 – NarendraC Jul 27 '16 at 14:01
  • @NielsvanReijmersdal Solvable by waiting for the process to end. – Paul Muir Jul 27 '16 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.

| improve this answer | |
  • 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 Jul 27 '16 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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