Selenium Webdriver - FirefoxDriver 问题:错误:无法打开显示::0.0

问题描述 投票:0回答:3

我用 selenium 做了一个测试用例,它会自动打开 Firefox 并执行登录操作。我通过 Eclipse 使用 java 编程做了这个。我测试它在我的 windows7 系统中工作正常。

现在,问题是 cron 作业在服务器中启动相同的程序,在尝试打开 Firefox 时会抛出以下错误:

Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
 ------------- Standard Error -----------------
 org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055       after 45000 ms. Firefox console output:
 Error: cannot open display: :0.0
 Error: cannot open display: :0.0

    at             org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnectio n.java:118)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:197)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:190)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
    at com.lo.test.selenium.AssignCampaignTestCase.<clinit>(AssignCampaignTestCase.java:42)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:266)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:375)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1420)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:848)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1899)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:800)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:809)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
  ------------- ---------------- ---------------

    Caused an ERROR
 null

我已经在我的服务器中安装了 xvfb 和 selenuim。我检查过他们正在运行。 我遵循了本教程

我的

testCase
的例子是

    package com.lo.test.selenium; 
    import static org.junit.Assert.assertTrue;
    import static org.junit.Assert.fail;
    
    import java.util.Properties;
    import java.util.ResourceBundle;
    
    import javax.mail.Flags;
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.NoSuchProviderException;
    import javax.mail.Session;
    import javax.mail.Store; 
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;
 
   public class MyExampleTestCase {

  private static ResourceBundle rb = ResourceBundle.getBundle("global-messages");
  static WebDriver driver = new FirefoxDriver();  //Make it static in order to make one    
      instance of this class(helps to avoid opening of 2 browsers at once)

  @Test

  public void campaignEmailTestCase() throws InterruptedException { 
 MyExampleTestCase emTesObj=new MyExampleTestCase ();  
 String fName="test LO";
    Integer LeadId=570903;
    String campaignName="2Get"; 
    String SubjName="Welcome";   //NOTE: No problem,Type without 
   double quotes. It will work for you 
    emTesObj.doLogin(); 
    emTesObj.goToLeadsListPage();
    emTesObj.searchByFirstName(fName);//without this also,it will work but Gives you the correct 
     navigation on UI
    emTesObj.waitBeforePageLoads(LeadId.toString()); //pass the Id attribute which you aspect to 
     see after the page is loaded
    emTesObj.assignCampaign(LeadId, campaignName);  
    emTesObj.readRecentEmail(SubjName);  
}
java selenium-webdriver junit
3个回答
1
投票

在本地计算机上运行代码时使用此功能:

private WebDriver driver = null;

public void open() throws Exception
{
    driver = new FirefoxDriver();
}

当您在远程服务器上运行代码时:

private WebDriver driver = null;

public void openHeadless() throws Exception
{
    FirefoxBinary binary = new FirefoxBinary(new File("/usr/local/bin/firefox"));
    binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
    driver = new FirefoxDriver(binary,null);
}

0
投票

如果您使用 xvfb-run 命令以无头模式运行 Selenium 节点,如下所示...

$ xvfb-run -a -s "-screen 0 1280x1600x24" java -jar selenium-server-standalone.jar -role node

...那么您可能会因为xvfb进程崩溃而遇到相同的症状。在这种情况下,进程树如下所示:

├─sh -e /proc/self/fd/9
│   └─xvfb-run /usr/bin/xvfb-run -a -s -screen 0 1280x1600x24 java -jar selenium-server-standalone.jar -role node
│       ├─Xvfb :104 -screen 0 1280x1600x24
│       │   └─4*[{Xvfb}]
│       └─java -jar selenium-server-standalone.jar -role node
│           └─27*[{java}]
默认情况下,此树中的

Xvfb 进程会在每次浏览器启动时重新启动(请参阅 -reset 选项文档),并且由于某种原因存在内存泄漏。因此,当您在数天和数小时内使用该节点时,内存泄漏会导致 Xvfb 进程崩溃,但具有 Selenium 节点的 java 进程保持不变。在这种情况下,Selenium 启动的浏览器无法打开显示,因为 xvfb 未运行,并且您会看到错误消息。

要解决此问题,您需要关闭 xvfb 重新启动行为,这是通过 -noreset 选项完成的:

$ xvfb-run -a -s "-screen 0 1280x1600x24 -noreset" java -jar selenium-server-standalone.jar -role node

-1
投票

如果您将 selenium 与 maven 一起使用,只需更新 pom.xml 文件中的依赖项即可解决问题。这对我有用。 如果您使用的是测试单元,请将使用的 jar 更新到最新版本:)

© www.soinside.com 2019 - 2024. All rights reserved.