How can selenium close browser be used multiple times in TestNG?

342    Asked by Amitraj in QA Testing , Asked on Apr 26, 2022

I have two scripts trying to run using testng.


In 1st script launching browser at the end closing the browser.

In the second script again launching the browser and closing at the end.

But getting below error.


org.testng.TestNGException: 

Cannot instantiate class hotelsScripts.TS03_Hotelstours_StarGrade

    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)

    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:382)

    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:295)

    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:118)

    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:183)

    at org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:128)

    at org.testng.TestRunner.init Methods(TestRunner.java:416)

    at org.testng.TestRunner.init(TestRunner.java:242)

    at org.testng.TestRunner.init(TestRunner.java:212)

    at org.testng.TestRunner.(TestRunner.java:166)

    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:143)

    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:272)

    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:587)

    at org.testng.SuiteRunner.init(SuiteRunner.java:161)

    at org.testng.SuiteRunner.(SuiteRunner.java:114)

    at org.testng.TestNG.createSuiteRunner(TestNG.java:1290)

    at org.testng.TestNG.createSuiteRunners(TestNG.java:1277)

    at org.testng.TestNG.runSuitesLocally(TestNG.java:1131)

    at org.testng.TestNG.run(TestNG.java:1048)

    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)

    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)

    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

Caused by: java.lang.reflect.InvocationTargetException

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

    at java.lang.reflect.Constructor.newInstance(Unknown Source)

    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)

    ... 21 more

Caused by: org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms

Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46'

System info: host: 'ADMINIB-UKGT3G2', ip: '9.78.194.222', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'

Driver info: driver.version: FirefoxDriver

    at org.openqa.selenium.internal.SocketLock.lock(SocketLock.java:99)

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:80)

    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)

    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:119)

    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:218)

    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:211)

    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:207)

    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:120)

    at hotelsScripts.TS01_Hotels_Amenties.(TS01_Hotels_Amenties.java:17)

    at hotelsScripts.TS03_Hotelstours_StarGrade.(TS03_Hotelstours_StarGrade.java:18)

    ... 26 more


For the selenium close browser, I will suggest: If possible avoid just closing and opening browser if there is not such a requirement Understand the driver.close() and driver.quit() driver.close() will close the current window and driver instance will be present driver.quit() will close all the windows and cleans up the driver instance too To resolve your error: Agree with Niels, Lets see if increase in wait will work Do not close browser after each tests



Your Answer

Interviews

Parent Categories