HGF
HGF

Reputation: 51

Why isn't my close browser code working in Java RFT

I'm trying to use the following code to close any open IE Browsers in RFT (as a clean up step, before executing the Test Scripts)

        public void testMain(Object[] args {

         RootTestObject root = RootTestObject.getRootTestObject();   

        {
           // Find all TestObjects with a class of HTML Browser
        TestObject[] browsers = find(atDescendant(".class","Html.HtmlBrowser"));
        ProcessTestObject browserProcess;

        System.out.println(browsers.length); //print the number of browsers found

        for (int j = 0, j<browsers.length; j++)
        {
           System.out.println(broswers[j].getProperties()); //print the properties of all browsers found
        }   



        for (int i=0;i<browsers.length;i++)
        {
            // Get the process of the browser
            browserProcess = browsers[i].getProcess();

            // Close the browser
            browserProcess.kill();

            // Unregister the browser object
            browserProcess.unregister();
        }
    }
}

It runs without error, but the browser(s) remain open.

I then added BOTH print statements to verify that it does find the browser, and my console shows that it does indeed find AND print their properties.

But for some reason, it won't perform the ".kill" action, can anyone tell me what I'm doing wrong?

Thanks in advance

Upvotes: 0

Views: 586

Answers (2)

Alessandro Da Rugna
Alessandro Da Rugna

Reputation: 4695

I used to kill the browser invoking the taskkill command from Java Runtime. IE was not a very friendly browser to work with.

The code was something like this

boolean closeBrowsers() {
    try {
        Runtime.getRuntime().exec("taskkill /f /t /im iexplore.exe");
        return true;
    } catch (IOException e) {
        System.out.println("cannot close browser");
        e.printStackTrace();
    }
    return false;
}

Upvotes: 0

Prakash
Prakash

Reputation: 752

See if this works for you

void closeBrowsers()
{
    TestObject browsers[] =  find(atChild(".class","Html.HtmlBrowser"));//Browser object wud be found at child level

    System.out.println("Browsers found "+ browsers.length);
    for(TestObject browser:browsers)
    {
        ((BrowserTestObject)browser).close();//use the inbuilt close method
    }

    //checking again 
     browsers =  find(atChild(".class","Html.HtmlBrowser"));

     System.out.println("After closing  found "+ browsers.length + " browsers still running");

     //need to try some other approach now..we will figure out something

     unregister(browsers);

}

Upvotes: 0

Related Questions