java - Selenium Webdriver 无法访问浏览器异常

标签 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.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 和 TestClass 2)。调用第二类浏览器时会抛出上述错误。这是我的完整项目

基类:

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();
    }
}

如果有人能让我知道我做错了什么,我将非常感激

我的 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

来测试这一点

Reference

编辑

Video

Gist

关于java - Selenium Webdriver 无法访问浏览器异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29020119/

相关文章:

java - 在 FOR 中声明变量一次

java - 比较两个 char 数组并创建不匹配值索引的 Int 数组

eclipse - 如何使用固定 IP 地址或系统变量设置 DOCKER_HOST?

Java Swing 使用另一个文件中的组件打开一个新按钮

python - 无法从网页中抓取标题

java - 在 Java 中使用 JSch exec 从 ArrayList 执行命令列表

java - 我怎样才能使 HashSet 作为 HashMap 的参数?

java - Eclipse JDT : How do I auto-move dependencies of a statement as well, 同时移动语句

python - 默认调用父构造函数?

java - Selenium |命名空间错误: Failed to execute 'evaluate' on 'Document' | Contains unresolvable namespaces