3

I want Selenium to wait untill and unless Autoit Script is completed.

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 parallely 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
5

Simpler solution is try a waitFor() on your process, this waits until the process exits.

@Test (priority=1)
public void CreateNew() throws Exception
{       
    Process process = Runtime.getRuntime().exec("exeFiles\\CreateNew.exe");
    process.waitFor();
    Integer result = process.exitValue();
    Assert.AssertEquals(0, result) 
}

Now you can add an exit code to the AutoIt script to communicate that it passed or failed. Read about exit codes here: https://www.autoitscript.com/autoit3/docs/keywords/Exit.htm

Otherwise try something like this:

  • 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 doesnt exist break out of loop and continue with the next code

Some considerations:

  • Add a timeout to your loop in case the AutoIt script does not clean up the lockfile.
  • Add a timestamp and testname to the lockfiles name (e.g. testname_timestamp.lck) so that you can run tests in parallel
  • Clean all lockfiles when starting a testsuite
| improve this answer | |
0

My answer borrows from Paul answer

@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");
}

By adding the wait onto the process it will wait for it to end.

| improve this answer | |

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.