3

I want Selenium to wait until and unless an AutoIt script is completed.

Right now whats happening is when I run the TestNG.xml file it runs all the @Test Priority wise and within 5 sec the TestNg output console shows all the @Test are passed. While my AutoIt scripts are still running in parallel in the 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");
}

The AutoIt code 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

2 Answers 2

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
6
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.

Your Answer

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

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