我在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&amp; selenuim在我的服务器上。我查看他们正在跑步。 我followed this tutorial
&安培;我的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);
}
答案 0 :(得分: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);
}
答案 1 :(得分: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未运行且您看到错误消息。
要解决此问题,您需要关闭由 -noreset 选项完成的xvfb重启行为:
$ xvfb-run -a -s "-screen 0 1280x1600x24 -noreset" java -jar selenium-server-standalone.jar -role node
答案 2 :(得分:-1)
如果您正在使用selenium和maven,只需更新pom.xml文件中的依赖项即可解决问题。它对我有用。 如果您使用的是测试单元,请更新用于最新版本的jar:)