Java Selenium Webdriver无法访问BrowserException

Java Selenium Webdriver无法访问BrowserException,java,eclipse,selenium,automation,testng,Java,Eclipse,Selenium,Automation,Testng,我得到这个错误: org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died. Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35' System info: host: 'HOME', ip: '10.1.11.12

我得到这个错误:

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'HOME', ip: '10.1.11.121', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:425)
    at org.openqa.selenium.By$ByName.findElement(By.java:299)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at com.tests.TestClass2.test2(TestClass2.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:7055 [/127.0.0.1] failed: Connection refused: connect
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
    ... 29 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    ... 44 more
我的环境:

OS:       Windows 7
JDK:      javac 1.8.0_31
FireFox:  ESR 31.4.0
Eclipse:  4.4.2.20150219-0708
Selenium: selenium-2.45.0
Testng:   6.8.6.20141201_2240
我使用一个测试套件文件来运行2个类(TestClass1和TestClass2)。在为第二类调用浏览器时,它抛出上述错误。这是我的完整项目

基类:

public class BaseClass {

    public static WebDriver driver = new FirefoxDriver();

    public static boolean implicitwait(long time) {
        try {
            driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @BeforeClass
    public static boolean LaunchBrowser() throws InterruptedException {
        implicitwait(50000);
        try {
             System.setProperty("webdriver.firefox.bin",
             "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
            driver.navigate().to("https:google.com");
            driver.manage().window().maximize();
            implicitwait(5000);
        } catch (Exception e) {
            return false;
        }
        System.out.println("Launching Browser & Navigating To Page");
        return true;
    }

    @AfterClass
    public void CloseBrowser() {
        driver.close();
        System.out.println("Closing Browser");
    }
}

Test Class:

@Test
public class TestClass extends BaseClass {

    public void test1(){
        System.out.println("TEST 1");
        driver.findElement(By.name("q")).sendKeys("ABC");
        driver.findElement(By.name("btnG")).click();
    }
}


Test Class2

@Test
public class TestClass2 extends BaseClass {

    public void test2(){
        System.out.println("TEST 2");
        driver.findElement(By.name("q")).sendKeys("DEF");
        driver.findElement(By.name("btnG")).click();
    }
}
如果有人能让我知道我做错了什么,我将不胜感激

My Testng.xml:

<test name="Run Test Class In Firefox">
    <classes>
        <class name="com.tests.TestClass" />
        <class name="com.tests.TestClass2" />
    </classes>
</test>



使用
@BeforeSuite
@AfterSuite
而不是
@BeforeClass
@AfterClass

@AfterClass
public void CloseBrowser() {
    driver.close();
    System.out.println("Closing Browser");
}
基本上在第一个类之后关闭当前浏览器实例,因此,驱动程序实例对于运行下一个类的测试无效。您可以通过注释掉
@AfterClass

@AfterClass
public void CloseBrowser() {
    driver.close();
    System.out.println("Closing Browser");
}

编辑


Saifur,感谢您的快速回复,根据您的建议,我使用了BeforeSuite和AfterSuite,但效果不理想。发生的情况是:浏览器在第一个测试用例之前打开,然后两个测试的操作一起执行。根据我的代码,第一次测试应键入ABC并关闭浏览器,但它键入“ABCDEF”&然后浏览器关闭请告诉我我做错了什么:(,我想为每个testcase打开/关闭浏览器。我的testng.xml说:我现在真的不清楚您要做什么。在两个类之间共享同一个实例?我只希望每个类分别调用和关闭浏览器。我的代码(如上所述)非常简单。我只是无法使用testng.xml分别调用和关闭浏览器。我只是在上面提供了我的testng.xml代码。我以为您希望在任何地方都使用相同的浏览器实例。但是,如果您希望在每个类中打开和关闭,那么
BeforeClass
AfterClass
都很好。只要尝试
driver.quit()
而不是
driver.close();
如果是那么简单,我就不会问问题了。事实上,我已经尝试了所有3个选项,但都不起作用:driver.quit();driver.close();或WindowsUtils.tryToKillByName(“firefox.exe”);WindowsUtils.tryToKillByName(“javaw.exe”);使用BeforeSuite和AfterSuite没有帮助,请帮助:(