Closing Browser After Failed Test Closing Browser After Failed Test selenium selenium

Closing Browser After Failed Test


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


If you are using Ruby and Test Unit, you could have something like this -

if you are sharing the wedriver browser session for multiple tests, and need to close the browser only at the end

def self.shutdown    @driver.quit    assert_equal [], @verification_errorsend

or if you want to close the browser after each individual test

def teardown    @driver.quit    assert_equal [], @verification_errorsend


This may not be the apt solution, but i did this as workaround for the problem.

instead of Try{}catch... use throws: throws java.lang.AssertionError and quit the browser in the catch block, e.g:

public void testMethod() throws java.lang.AssertionError{Assert.assertTrue(condition,Message);}catch(java.lang.AssertionError e){e.printstactrace();browser.quit();Assert.fail();}