小编典典

org.apache.http.conn.HttpHostConnectException:连接到localhost:19538 [localhost / 0:0:0:0:0:0:0:0:1]失败:连接被拒绝(连接被拒绝)

jenkins

我有一个运行jenkins服务器的Linux机器,并从git中提取脚本并开始执行

我有一台Windows pc,从中打开jenkins网址说xyz:8080并尝试从Windows框中执行脚本,但是它给出了以下错误。

使用phantomjs运行时也会发生同样的情况。

我现在正在尝试在Firefox中运行

  -------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
1516800459520   geckodriver INFO    geckodriver 0.19.1
1516800459526   geckodriver INFO    Listening on 127.0.0.1:15327
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.526 s
[INFO] Finished at: 2018-01-24T08:28:00-05:00
[INFO] Final Memory: 28M/241M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project com.perfect.wp: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
[ERROR] org.testng.TestNGException:
[ERROR] 
[ERROR] An error occurred while instantiating class buyer.testcases.solicitation.CreateSolicitation: null
[ERROR] at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:422)
[ERROR] at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
[ERROR] at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
[ERROR] at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
[ERROR] at org.testng.TestClass.getInstances(TestClass.java:99)
[ERROR] at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
[ERROR] at org.testng.TestClass.init(TestClass.java:77)
[ERROR] at org.testng.TestClass.<init>(TestClass.java:42)
[ERROR] at org.testng.TestRunner.initMethods(TestRunner.java:423)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:250)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:220)
[ERROR] at org.testng.TestRunner.<init>(TestRunner.java:161)
[ERROR] at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:578)
[ERROR] at org.testng.SuiteRunner.init(SuiteRunner.java:185)
[ERROR] at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
[ERROR] at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
[ERROR] at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
[ERROR] at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
[ERROR] at org.testng.TestNG.runSuites(TestNG.java:1144)
[ERROR] at org.testng.TestNG.run(TestNG.java:1115)
[ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:281)
[ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:121)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
[ERROR] Caused by: java.lang.ExceptionInInitializerError
[ERROR] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[ERROR] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[ERROR] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[ERROR] at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[ERROR] at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
[ERROR] at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
[ERROR] ... 25 more
[ERROR] Caused by: org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:15327 [localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
[ERROR] Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
[ERROR] System info: host: 'phfqwptestauto', ip: '10.5.1.175', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.30.3.el6.x86_64', java.version: '1.8.0_152'
[ERROR] Driver info: driver.version: PCDriver
[ERROR] at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
[ERROR] at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
[ERROR] at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)
[ERROR] at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
[ERROR] at commonutils.pageobjects.utils.PCDriver.invokeBrowser(PCDriver.java:71)
[ERROR] at commonutils.pageobjects.utils.PCDriver.<clinit>(PCDriver.java:52)
[ERROR] ... 31 more
[ERROR] Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:15327 [localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
[ERROR] at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
[ERROR] at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
[ERROR] at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
[ERROR] at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
[ERROR] at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
[ERROR] at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
[ERROR] at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
[ERROR] at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
[ERROR] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
[ERROR] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
[ERROR] at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:138)
[ERROR] at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:86)
[ERROR] at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:101)
[ERROR] at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
[ERROR] at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
[ERROR] at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
[ERROR] ... 37 more
[ERROR] Caused by: java.net.ConnectException: Connection refused (Connection refused)
[ERROR] at java.net.PlainSocketImpl.socketConnect(Native Method)
[ERROR] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
[ERROR] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
[ERROR] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
[ERROR] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
[ERROR] at java.net.Socket.connect(Socket.java:589)
[ERROR] at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
[ERROR] at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
[ERROR] ... 52 more
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
Build step 'Execute shell' marked build as failure
Sending e-mails to: wp.qa@perfect.com
Finished: FAILURE

PCDriver类别:

public class PCDriver implements WebDriver {

    public static WebDriver driver;
    // static log4jClass log = new log4jClass();

    static {
        PCDriver.invokeBrowser(ReadConfig.getInstance().getBrowser());

    }

    public static synchronized WebDriver invokeBrowser(String browser) {

        switch (browser) {

        case "firefox":
            File pathBinary = new File("//home//chiefs//firefox//firefox");
            FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
            //DesiredCapabilities desired = DesiredCapabilities.firefox();
            DesiredCapabilities cap = new DesiredCapabilities();

            FirefoxOptions options = new FirefoxOptions();
            cap.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));
            cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
            System.setProperty("webdriver.gecko.driver",
                    ReadConfig.getInstance().getDriverPath().toString() + "geckodriver");
            driver = new FirefoxDriver(options);
            driver.get(ReadConfig.getInstance().getApplicationUrl());
            // log.info("Browser Invoked");
            break;

        case "htmlunit":
            DesiredCapabilities capHtml = DesiredCapabilities.htmlUnit();
            capHtml.acceptInsecureCerts();
            capHtml.setJavascriptEnabled(true);
            /*
             * System.setProperty("webdriver.gecko.driver",
             * ReadConfig.getInstance().getDriverPath().toString() + "geckodriver.exe");
             */
            driver = new HtmlUnitDriver(capHtml);
            driver.get(ReadConfig.getInstance().getApplicationUrl());
            // log.info("Browser Invoked");
            break;

        case "chrome":
            DesiredCapabilities capChrome = DesiredCapabilities.chrome();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("headless");
            capChrome.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
            capChrome.setJavascriptEnabled(true);
            capChrome.acceptInsecureCerts();
            System.setProperty("webdriver.chrome.driver",
                    ReadConfig.getInstance().getDriverPath().toString() + "chromedriver.exe");
            driver = new ChromeDriver(capChrome);
            driver.get(ReadConfig.getInstance().getApplicationUrl());
            driver.manage().window().setSize(new Dimension(1440, 900));
            break;

        case "ie":
            DesiredCapabilities capIE = new DesiredCapabilities();
            capIE.setPlatform(Platform.WINDOWS);
            capIE.acceptInsecureCerts();
            capIE.setJavascriptEnabled(true);
            capIE.setCapability("ignoreZoomSetting", true);
            capIE.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

            capIE.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, false);
            System.setProperty("webdriver.ie.driver",
                    ReadConfig.getInstance().getDriverPath().toString() + "IEDriverServer.exe");
            driver = new InternetExplorerDriver(capIE);
            driver.get(ReadConfig.getInstance().getApplicationUrl());

            /*
             * case "edge": DesiredCapabilities capEdge = DesiredCapabilities.edge();
             * capEdge.setPlatform(Platform.WIN10); capEdge.acceptInsecureCerts();
             * capEdge.setJavascriptEnabled(true); EdgeOptions options = new EdgeOptions();
             * options.setPageLoadStrategy("eager");
             * System.setProperty("webdriver.edge.driver",
             * ReadConfig.getInstance().getDriverPath().toString() +
             * "MicrosoftWebDriver.exe"); driver = new EdgeDriver(options);
             */


              case "phantomjs": 
                 DesiredCapabilities capPhantom =new DesiredCapabilities(); 
                 ArrayList<String> cliArgsCap = new ArrayList<String>(); 
                 cliArgsCap.add("--web-security=false");
                 cliArgsCap.add("--proxy=10.5.1.175:1024");

              cliArgsCap.add("--ssl-protocol=any");
              cliArgsCap.add("--ignore-ssl-errors=true");
              cliArgsCap.add("--webdriver-loglevel=NONE");
              cliArgsCap.add("--load-images=true"); //
              capPhantom.setBrowserName("PhantomJs");
              capPhantom.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
             // capPhantom.setCapability("trustAllSSLCertificates", true);
                          capPhantom.setCapability("screen-resolution", "1280x1024");

              capPhantom.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,cliArgsCap);
                capPhantom.setPlatform(Platform.LINUX);
              capPhantom.setCapability("proxy", "10.5.1.175:1024");

              capPhantom.setJavascriptEnabled(true);
              capPhantom.setCapability("takesScreenshot", true);
              capPhantom.setCapability("phantomjs.binary.path",ReadConfig.getInstance().getDriverPath().toString() + "phantomjs");
              driver = new PhantomJSDriver(capPhantom);
  driver.manage().timeouts()
                .pageLoadTimeout(60, TimeUnit.SECONDS)
                .implicitlyWait(60, TimeUnit.SECONDS);
                driver.manage().window().maximize();
              driver.get(ReadConfig.getInstance().getApplicationUrl());

        }

        return driver;
    }

    public void close() {
        driver.close();

    }

    public static WebDriver getDriver() {
        return driver;
    }

    public static void acceptAlert() {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();
    }

    public static void dismissAlert() {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().dismiss();
    }

    public static void waitForPageLoad() {
        ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        };
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(pageLoadCondition);
    }

Pom.xml

project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.perfect</groupId>
    <artifactId>com.perfect.wp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>webprocure</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <useSystemClassLoader>false</useSystemClassLoader>

                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>

    </build>
    <packaging>jar</packaging>

    <name>com.perfect.wp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-htmlunit-driver</artifactId>
            <version>2.52.0</version>
        </dependency>

        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.2</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.codeborne</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.4.4</version>
        </dependency>
    </dependencies>
</project>

Jenkins Maven配置屏幕快照链接:

http://prntscr.com/i4wn8h
http://prntscr.com/i4wnj4


阅读 1505

收藏
2020-07-25

共1个答案

小编典典

根据错误跟踪日志Maven未能执行目标,如下所示:

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project com.perfect.wp: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process

其结果org.testng.TestNGException如下:

[ERROR] org.testng.TestNGException:
[ERROR] 
[ERROR] An error occurred while instantiating class buyer.testcases.solicitation.CreateSolicitation: null

其结果org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException如下:

org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:15327 [localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)

这是driver.version:您的WebDriver实例未被检测到的结果,如下所示:

[ERROR] Driver info: driver.version: PCDriver

造成这种情况的唯一原因是,按您的 开关(浏览器) ,你没有带设置的 系统属性 通过 System.setProperty()

解决方案:

一旦使用 switch (browser) 并定义 case "firefox" 等等,您需要进行System Property如下设置:

  • case "firefox":

    System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
    

    //configuration starts here

  • case "chrome":

    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    

    //configuration starts here

  • case "ie":

    System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
    

    //configuration starts here

  • case "phantomjs":

    File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
    

    System.setProperty(“phantomjs.binary.path”,path.getAbsolutePath());
    //configuration starts here

  • 确保 /etc/hosts 您的 Linux Box 包含以下条目:

    127.0.0.1               localhost.localdomain localhost
    
2020-07-25