Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Browser After Failed Test

I am running tests with WebDriver, when a test fails, the browser does not close. On a Windows machine this is a huge problem because I then have several instances of the IEDriver still running in the background.

I have tried a try/catch statement which doesn't seem to work either. If the test fails the browser still remains open. Any help would be greatly appreciated.

The try catch statement looks something like this:

try
{
   Assert.something(something something dark side);
   driver.quit();
}
catch(Exception e)
{
   System.out.println(e)
   driver.quit();
}

My complete code is below:

public class ClickAddMedication 
{
Browser browser = new Browser();

public void addMedication(String driverName)
{
    //Open Browser and navigate to page
    WebDriver driver = browser.getDriver(driverName);
    driver.manage().window().maximize();
    driver.get("http://someIP:8080/hmp_patient/index.html");

    //Click Add Medication button
    WebElement addBtn = driver.findElement(By.id("add-btn"));
    addBtn.click();

    //Verify Add Medication page has loaded successfully
    WebElement rxBtn = driver.findElement(By.className("icon-rx"));
    WebElement otcBtn = driver.findElement(By.className("icon-otc"));
    WebElement herbBtn = driver.findElement(By.className("icon-herb"));

    Assert.assertEquals(true, rxBtn.isDisplayed());
    Assert.assertEquals(true, otcBtn.isDisplayed());
    Assert.assertEquals(true, herbBtn.isDisplayed());

    driver.quit();

}

@Test(groups = {"functionalTests.FF"})
public void test_AddMedication_FF()
{
    addMedication("firefox"); 
}
@Test(groups = {"functionalTests.iOS"})
public void test_AddMedication_iOS()
{
    addMedication("iOS");
}
}

I run the tests with a testng.xml file and would like to have the browser close regardless of whether or not the test passes.

Below is my Browser class:

public class Browser 
{
public WebDriver getDriver(String driverName)
{
    WebDriver driver = null;
    if(driverName == "firefox")
    {
        driver = new FirefoxDriver();
    }
    else if(driverName == "chrome")
    {
        File chromeFile = new File ("C:/webdrivers/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", chromeFile.getAbsolutePath());
        driver = new ChromeDriver();
    }
    else if(driverName == "ie")
    {
        File ieFile = new File("C:/webdrivers/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", ieFile.getAbsolutePath());
        driver = new InternetExplorerDriver();
    }
    else if(driverName == "iOS")
    {
        try 
        {
            driver = new RemoteWebDriver(new URL("http://localhost:3001/wd/hub"), DesiredCapabilities.ipad());
        } catch (MalformedURLException e) 
        {

            e.printStackTrace();
        }
    }


    return driver;

}
}
like image 711
DarthOpto Avatar asked Dec 21 '12 18:12

DarthOpto


People also ask

How do I close a browser in TestNG?

close() will close the current window and driver instance will be present. driver. quit() will close all the windows and cleans up the driver instance too.

How do you manage to re run only failed test cases?

By overriding retry() method of the interface in your class, you can control the number of attempts to rerun a failed test case. Now if we run TestNG. xml we see failed test case is executed one more time as we gave retry count= 1. Test case is marked as failed only after it reaches max retry count.


1 Answers

You haven't mentioned what framework you're using for executing your tests, but one way to handle this would be to use the equivalent of an "after test" annotation to accomplish this. JUnit calls this annotation @After while TestNG calls it @AfterMethod. The method annotated with the after test annotation will run after each method annotated with @Test, regardless of the pass/fail state of the test. If you are expecting to use the same driver instance across test methods, most test runners have an @AfterClass annotation or similar, which will run at the end of all @Test methods in the class.

As an example, you'd want to do something like the following (note the promotion of the driver variable to a member variable in the class):

public class ClickAddMedication 
{
    // N.B. For this approach to work, you *must* have the "driver"
    // variable here. Having it as a member variable of the class is
    // what allows the addMedication() method to access it for manipulating
    // the browser, and the tearDown() method to access it for closing
    // the *same* *browser* *instance*.
    WebDriver driver;
    Browser browser = new Browser();

    public void addMedication(String driverName)
    {
        //Open Browser and navigate to page
        driver = browser.getDriver(driverName);
        driver.manage().window().maximize();
        driver.get("http://someIP:8080/hmp_patient/index.html");

        //Click Add Medication button
        WebElement addBtn = driver.findElement(By.id("add-btn"));
        addBtn.click();

        //Verify Add Medication page has loaded successfully
        WebElement rxBtn = driver.findElement(By.className("icon-rx"));
        WebElement otcBtn = driver.findElement(By.className("icon-otc"));
        WebElement herbBtn = driver.findElement(By.className("icon-herb"));

        Assert.assertEquals(true, rxBtn.isDisplayed());
        Assert.assertEquals(true, otcBtn.isDisplayed());
        Assert.assertEquals(true, herbBtn.isDisplayed());
    }

    @AfterMethod
    public void tearDown()
    {
        driver.quit();
    }

    @Test(groups = {"functionalTests.FF"})
    public void test_AddMedication_FF()
    {
        addMedication("firefox"); 
    }

    @Test(groups = {"functionalTests.iOS"})
    public void test_AddMedication_iOS()
    {
        addMedication("iOS");
    }
}
like image 62
JimEvans Avatar answered Nov 04 '22 13:11

JimEvans