myku
myku

Reputation: 1

using the same webdriver for 2 tests in row

I'm trying to run a few test in row using the same WebDriver instance. After each test I'm executing: driver.close(); First test passed but for second I received an error when try to call driver.get(URL) :

org.openqa.selenium.remote.SessionNotFoundException: no such session
  (Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 227 milliseconds
Build info: version: '2.46.0', revision: '61506a4624b13675f24581e453592342b7485d71', time: '2015-06-04 10:22:50'
System info: host: 'PL-WAR-MOB-057', ip: '10.48.0.91', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\myczkp01\AppData\Local\Temp\scoped_dir11412_2137}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=44.0.2403.107, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 7e6052e461fdf1ca134deb59b7debb72
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:306)

There is a similar problem Using same Webdriver 2 times but it hasn't been solved.

Upvotes: 0

Views: 383

Answers (3)

Manu
Manu

Reputation: 2301

You cannot use same WebDriver for 2 tests in row, if you close the driver using driver.close(), the reason being simple that it will quit the browser if it is the last window open as can be found in its documentation.

Close the current window, quitting the browser if it's the last window currently open.

You cannot use driver after quit.

Upvotes: 1

virendra chaudhary
virendra chaudhary

Reputation: 179

Your browser has been closed . So you have to start the browser again .

You can do following things.

driver = new FirfoxDriver();
driver.get(url);

Upvotes: 1

chrisc
chrisc

Reputation: 434

It's generally recommended not to share state like the browser and other objects between test cases - this stops you from having dependencies where one test relies on another, and you wont be sure what state each test leaves your browser in.

It would be best practice to initialize the driver instance, and close it, in the corresponding Before and After test methods which require an instance of WebDriver.

Driver.Close() will close the currently open window, quitting the browser if it is the last window open.

Upvotes: 0

Related Questions