I have two scripts trying to run using testng.

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

But getting below error.

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.<init>(TestNGClassFinder.java:128)
    at org.testng.TestRunner.initMethods(TestRunner.java:416)
    at org.testng.TestRunner.init(TestRunner.java:242)
    at org.testng.TestRunner.init(TestRunner.java:212)
    at org.testng.TestRunner.<init>(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.<init>(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: '', 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.<init>(RemoteWebDriver.java:119)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
    at hotelsScripts.TS01_Hotels_Amenties.<init>(TS01_Hotels_Amenties.java:17)
    at hotelsScripts.TS03_Hotelstours_StarGrade.<init>(TS03_Hotelstours_StarGrade.java:18)
    ... 26 more

3 Answers 3


Unable to bind to locking port 7054

The error means a browser or selenium instance is still running and it cannot create a new one.

Make sure you do a driver.quit() call to make sure the browser and Selenium cleans up. Just closing the windows will not help.

Maybe you need try a little pause at the end of script1 to let Selenium exit gracefully, although the timeout of 45 seconds should be enough I guess.

See this Stack overflow answer for more info.

  • seems to be you are incredibly correct.
    – ChathuD
    Jan 13, 2017 at 17:41

Is it mandatory for you to close browser after first test really? when you need browser for second & immediate next test?

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

  • 1
    Closing window each time to ensure tests don't interact with each is important. Avoiding this will lead to hard-to-fix errors down the road Oct 15, 2016 at 12:34

It appears that your browsers arn't closing gracefully or conflicting.

My initial suggestion would be to shift your browser handling into a TestBase or similar class. Each of your test scrips can then call it. This will eliminate the potential for conflicts to occur as you are handling browser instances from one place. And also makes it easier to maintain moving forward.

@BeforeClass(alwaysRun = true)
        public void init() {
        WebDriver driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(this.waitSeconds, TimeUnit.SECONDS);
            wait=new WebDriverWait(driver, waitSeconds);

When closing, I would then use a small check of the status of the browser before running the .quit(). Again this stops trying to close something that isn't there!

@AfterClass(alwaysRun = true)
    public void driverQuitter(){
        if(driver != null){
            logger.info("Closing browser after TestClass");
            logger.error("Driver is null at AfterClass (TestBase)");
        logger.info("Teardown - Exiting");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.