Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WeDriver FirefoxDriver cannot be used after quit() was called

i am having issues in acquiring correct driver instance. following is my setup

public class SeleniumBase{
    public static WebDriver  driver;
      public static void setUp(url,browser,port){
        driver = new FirefoxDriver();
      }
      public static void tearDown(){      
        driver.manage().deleteAllCookies();
        driver.close();
        driver.quit();         
      }
    }

public class BuildTest extends SeleniumBase{

    @BeforeClass
        public static void seleniumSetup(){
        try{
            // read properties
            url = prop.getProp("baseUrl");
            browser = prop.getProp("browser");
            port = prop.getProp("port");
            }
            SeleniumBase.setUp(url,browser,port);
            waitForLoginPage();
            App.login();
        }
    @AfterClass
        public static void seleniumTearDown(){
            App.logOut();
            SeleniumBase.tearDown();
        }
}

@RunWith(Suite.class)
    @Suite.SuiteClasses(
            {                                               
                Test1.class,
                Test2.class
            })

public class SmokeSuite {


}

now, for Test1.class everything works fine but when Test2.class is invoked from the suite, new driver instance is created with the setUp method, but App.Login() throws error saying "The FirefoxDriver cannot be used after quit() was called"

is anything going wrong in my setup/teardown..?

like image 262
aeinstein83 Avatar asked Nov 11 '22 16:11

aeinstein83


1 Answers

As the comments on your question already mention, your setUp() and tearDown() methods as well as your WebDriver instance are static. So once you call driver.quit(), your driver couldn't be used any more. A new driver needs to be acquired.

However, you do not use JUnit's @Before and @After annotations but rather @BeforeClass and @AfterClass. So I guess you have multiple tests in your Test2 class, the driver quits after the first one and is not reinitialized before the second test.

Better make WebDriver, setUp() and tearDown not static and use @Before and @After in your test-classes. Then your problems should go away.

like image 59
Martin Höller Avatar answered Nov 14 '22 22:11

Martin Höller