Closing a browser after a failed test

I run tests using WebDriver, when the test fails, the browser does not close. On a Windows machine, this is a huge problem because I have multiple instances of IEDriver that are still running in the background.

I tried the try / catch statement, which also does not work. 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 tests using the testng.xml file and would like to close the browser regardless of whether 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; } } 
+4
source share
4 answers

You did not indicate which framework you are using to run your tests, but one way to handle this is to use the equivalent of a post-test annotation to do this. JUnit calls this annotation @After , while TestNG calls it @AfterMethod . The method annotated with annotation after the test will run after each method annotated with @Test , regardless of the state of passing / failing the test. If you expect to use the same driver instance in your test methods, most test runners have an @AfterClass annotation or similar annotation that will work at the end of all @Test methods in the class.

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

 public class ClickAddMedication { // NB 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"); } } 
+5
source

If you use Ruby and Test Unit, you might have something like this -

if you use a wedriver browser session for several tests and should close the browser only at the end

 def self.shutdown @driver.quit assert_equal [], @verification_errors end 

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

 def teardown @driver.quit assert_equal [], @verification_errors end 
0
source

This may not be an apt solution, but I did it as a workaround for the problem.

instead of Try{}catch ... use throws : throws java.lang.AssertionError and close the browser in the catch block, for example:

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

driver.close () should do the trick for you. Or by setting a boolean flag, and when the test fails, the boolean flag is true.

And then using driver.close () or System.exit (1) should work!

Let me know if you have problems with the method described above.

0
source

All Articles