Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver - Issue with FirefoxDriver: Error: cannot open display: :0.0

I made a test case in selenium which automatically opens Firefox and do its login stuffs. I made this using java programming through Eclipse.& i tested it is working fine in my windows7 system.

And Now, the problem is A cron job starts this same program in the server, which throws the following error when trying to open 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

I have installed xvfb & selenuim in my server. I checked they are running. And i followed this tutorial

& my testCase Example is

        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);  
}
like image 544
Dan Avatar asked Nov 24 '22 03:11

Dan


1 Answers

Use this when you run your code on a local machine:

private WebDriver driver = null;

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

And this when you run your code on a remote server:

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);
}
like image 125
barak manos Avatar answered Jun 04 '23 01:06

barak manos