Webdriver Unable to connect to host 127.0.0.1 on p

2019-01-02 19:51发布

I have box where I run tests. It seems like Jenkins would ssh in and execute commands described in the specific job that's running.

Here I am trying to run my Selenium Webdriver tests, but it tells me that I have errors in launching firefox. The end idea is to run the webtests on this box entirely, and also take some screenshots of errors.

I am using selenium-java-2.25.jar, firefox 10, linux OS.

The funny thing is that I can ssh in manually into the box, temporarily copy the magic cookie from another user on the box (to get an X tunnel), do an export DISPLAY=mydisplay:1.0, then launch my selenium tests using ant. And this will bring up firefox and the tests just fine.

There are various threads here that seem to have the exact same problem, and I think I've tried most of them. Here is what I've done:

  • Reboot the box, log back in with VNC.

  • Placed a bash script in Jenkins to run before running selenium tests. The bash script basically just does an export DISPLAY=mydisplay:1.0. It also executes xclock. This works as I can see xclock being displayed in the VNC.

  • iptables have been turned off

  • firefox is correctly located in /usr/bin/firefox

  • sshd_config shows having X11Forwarding as true.

  • Supposedly downgrading firefox helped some people, but I do not wish to do this. Webdriver should support FF 10 anyway.

However, none of the above solves the problem.

It doesn't seem like port 7055 on localhost even exists:

netstat -an | grep 7055 - Nothing prints

This is what my /etc/hosts says:

  1 127.0.0.1               localhost.localdomain localhost
  2 ::1             localhost6.localdomain6 localhost6

Perhaps it has something to do with localhost:7055 not existing? I am unsure where to go from here. still, why does the error output say it's looking for display: :0.0 when I've specified mydisplay:1.0?

And finally the error output I receive:

[testng] org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
[testng] Xlib: connection to ":0.0" refused by server
[testng] Xlib: No protocol specified
[testng] 
[testng] Error: cannot open display: :0.0
[testng] Xlib: connection to ":0.0" refused by server
[testng] Xlib: No protocol specified
[testng] 
[testng] Xlib: connection to ":0.0" refused by server
[testng] Xlib: No protocol specified
[testng] 
[testng] Xlib: connection to ":0.0" refused by server
[testng] Xlib: No protocol specified
[testng] 
[testng] Error: cannot open display: :0.0
[testng] 
[testng]    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:109)
[testng]    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:245)
[testng]    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:109)
[testng]    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:185)
[testng]    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:178)
[testng]    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:174)
[testng]    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)
[testng]    at com.test.webtest.browser.BrowserFactory.createBrowser(BrowserFactory.java:24)
[testng]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[testng]    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[testng]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[testng]    at java.lang.reflect.Method.invoke(Method.java:601)

29条回答
宁负流年不负卿
2楼-- · 2019-01-02 20:29

Me too had the same problem but issue is resolved after downgrading firefox version to 35.0.1 and my selenium version is 2.43

查看更多
高级女魔头
3楼-- · 2019-01-02 20:30

This is how it's behaved when I used static property of IWebDriver and call it from multiple test methods.

public class LanguageMenu
{
    private static IWebDriver drv;

    static LanguageMenu()
    {
       drv = Driver.Instance;
    }
    ...

    public static void English()
    {
        drv.FindElement(By.Id("mvc_lang_en"));
        el.Click();
    }

    public static void Rusian()
    {
        ...
    }

    ...
}

Like

 [TestMethod]
 public void Language_SwitchTo_English()
 {
     LanguageMenu.English();
     Assert.IsTrue(ContactPage.IsAt("Contact"));
 }

 [TestMethod]
 public void Language_SwitchTo_Rusian()
 {
     LanguageMenu.English();
     Assert.IsTrue(ContactPage.IsAt("Контакт"));
 }

Solution

For each calling test methods create new instance of Driver

 private static void English()
 {
            var drv = Driver.Instance;
            var el = drv.FindElement(By.Id("mvc_lang_en"));
            el.Click();
 }
查看更多
孤独寂梦人
4楼-- · 2019-01-02 20:30

I had the same issue with firefox 38.

After using following version dependencies, I could resolve the issue.

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.0</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.53.0</version>
    </dependency> 
查看更多
ら面具成の殇う
5楼-- · 2019-01-02 20:31

I resolved this issue by downgrading my Firefox to an older version that had previously worked well with Selenium-WebDriver. In my case, I had to downgrade back to Firefox 18 and this version worked with Selenium 2.27

Here is the link to get older versions of firefox: https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/

查看更多
梦该遗忘
6楼-- · 2019-01-02 20:31

Just install Xvnc Plugin in Jenkins. The problem should be solved.

查看更多
人气声优
7楼-- · 2019-01-02 20:31

Update selenium jars if our selenium script is not executing. Currently i am using selenium-java-2.43.0-srcs

Now it is working fine

查看更多
登录 后发表回答