2

Such a junit :

    @Test
    public void testA {
//some code here...
}

    @Test
    pulic void testB {
//some code here...
}   
    @After
    public void closeBrowsers() throws Exception {
selenium.stop();
}

Here is the question : closeBrowsers() method called after every test method; in that case it is called twice and i got "Wrong test finished." from JUnit. I need a junit method/annotation which will be called after all tests finised (just called once after all tests finished), is it possible ?

Also i tried to check if selenium is up or not in closeBrowsers() but no way i couldn't find any solution.

P.S : I 've read this one : How to close a browser on a selenium RC server which lost it's client

but i couldn't understand the solution and also currently http://www.santiycr.com.ar/djangosite/blog/posts/2009/aug/25/close-remaining-browsers-from-selenium-rc blog side is down

Community
  • 1
  • 1
Altug
  • 1,723
  • 3
  • 19
  • 27
  • @AfterClass needs static method. Look at that please : public class xxxTest extends SeleneseTestCase { @Test public void testaaa() throws Exception { try { selenium.setSpeed("100"); ..... } } } If I use @AfterClass then testaaa() needs to be static and in that case i can't use selenium variable... Any solution for this ? Thanks. – Altug Jul 28 '10 at 06:05
  • You can make your selenium variable static – Sergii Pozharov Jul 28 '10 at 10:05

2 Answers2

3

Use the @AfterClass annotation.

http://junit.sourceforge.net/doc/faq/faq.htm#organize_3

Reflux
  • 2,929
  • 4
  • 26
  • 27
3

You can make your selenium variable static, initialize it in @BeforeClass static method and cleanup in @AfterClass:

public class ...... {

  private static Selenium selenium;

  @BeforeClass
  public static void initSelenium() {
     selenium = new DefaultSelenium(...); // or initialize it in any other way
  }

  @Test
  public void testA {...}

  @Test
  pulic void testB {...}

  @AfterClass
  public static void closeBrowsers() throws Exception { 
    selenium.stop(); 
  }
}
Sergii Pozharov
  • 17,366
  • 4
  • 29
  • 30
  • While that works if all your tests are contained in one class, that wont work if you have tests in separate class files (and not using a Suite thereby accepting the default behavior of running all tests in project). For that, we would need a AfterSuite annotation or a custom hack to do similar. – djangofan Feb 09 '13 at 00:07
  • The question was exactly about using browser in one class, if you need to reuse the same browser for multiple tests of course you will need something else - base class for all tests, inject browser instance, etc - there are so many options – Sergii Pozharov Feb 14 '13 at 11:10