skip to Main Content

I have the below code in Selenium/Java test class. Now, this code I have pushed to GitHub.
Also, I have set up the Jenkins job to execute the same code (in the Jenkins job I have pointed the code to GitHub).
The Jenkins job is triggering fine and started executing the test, but throwing below error while opening the browser.
The test case is supposed to open the Firefox browser, but the Firefox browsing is not opening.

So, my question is, whether the below selenium code is correct if I want to execute the test case in Jenkins job (Jenkins server is running in Cento7.4 OS).

NOTE: In the same CentOS VM, I am able to execute the same (below) selenium code in eclipse and it’s able to open the Firefox browser and open the URL without any issues.
The issue is coming only if I try to run the same code in the Jenkins server as a Jenkins job.

Selenium code

System.setProperty("webdriver.gecko.driver",  "geckodriver");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--display=0");
    
WebDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("https://www.facebook.com");

Jenkins job output

Running TestSuite

Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
1597912923234   mozrunner::runner   INFO    Running command: "/bin/firefox" "-marionette" "--display=0" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileFz0Zr2"
Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Running without a11y support!
Error: cannot open display: 0
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.972 sec <<< FAILURE!

Results :

Failed tests:   loginTest4(com.training.browsers.LinuxTest): invalid argument: can't kill an exited process

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

[ERROR] There are test failures.

xauth list output

[root@localhost ~]# xauth list
localhost.localdomain/unix:0  MIT-MAGIC-COOKIE-1  4eb74af687f2dbc022ef03617614456e
#ffff#6c6f63616c686f73742e6c6f63616c646f6d61696e#:0  MIT-MAGIC-COOKIE-1  4eb74af687f2dbc022ef03617614456e

3

Answers


  1. You may want to look into setting up xvfb (https://centos.pkgs.org/7/centos-x86_64/xorg-x11-server-Xvfb-1.20.4-10.el7.x86_64.rpm.html). The problem is that your Jenkins server cannot open display 0 to run in. Notice the parameters being sent in to the firefox binary specifying display 0 in your firefoxOptions match the INFO log line for the binary execution. Assuming that you are running a headless server and this is why you get this error. The same is not the case when running locally.
    With xvfb you should be able to specify a screen number and set your configurations accordingly or simply use xvfb-run.

    Login or Signup to reply.
  2. The test case is supposed to open the Firefox browser, but the Firefox browsing is not opening.

    To resolve this issue, use WebDriverManager to automate the management of the drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.
    To use WebDriverManager in a Maven project, add the following dependency in your pom.xml (Java 8 or upper required).

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.2.2</version>
    </dependency>
    

    Then simply add WebDriverManager.firefoxdriver().setup(); in your code as shown below:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    WebDriverManager.firefoxdriver().setup();
    
    FirefoxOptions firefoxOptions = new FirefoxOptions();  
    firefoxOptions.addArguments("--display=0");
    WebDriver driver = new FirefoxDriver(options);
    

    See the basic examples of running JUnit 4 tests on Firefox and Chrome using WebDriverManager here.

    Login or Signup to reply.
  3. --display=n

    The phrase display is used to refer to collection of monitors that share a common keyboard and pointer e.g. mouse, tablet, etc. Most workstations tend to only have one keyboard, and therefore, only one display. Multi-user systems, frequently have several displays and each display on a machine is assigned a display number (beginning at 0) when the X server for that display is started. display:0 is usually the local display i.e. the main display of the computer.


    Using Jenkins

    When Jenkins executes the batch file, Jenkins slave runs as service in the background for every program it initiates. So normally you won’t be able to visualize the Firefox browser spinning up but in the task manager you can see Jenkins opens several Firefox processes in background.


    Solution

    There are a couple of approaches to address this issue as follows:

    If you are using Jenkins as a windows service you need to Allow service to interact with desktop. Steps:

    • In windows service select the service of Jenkins:

    JenkinsService

    • Open properties window of the Jenkins service -> Logon-> enable the checkbox Allow service to interact with desktop

    AllowServiceToInteract

    In this approach autolaunch of dbus-daemon works when under an X11 session, else it is disabled because there’s no way for different applications to establish a common instance of the dbus daemon.

    You can find a relevant detailed discussion in Jenkins : Selenium GUI tests are not visible on Windows

    The other approach would be to run Jenkins from command prompt as java -jar jenkins.war instead of the windows installer version.

    Another approach will be to use RemoteWebDriver. From Jenkins, make sure there is a machine where the selenium tests can run. On this server you spin up the browser.

    You can find a relevant detailed discussion in How to launch chrome browser from Jenkins directly instead of using code in eclipse

    An alternative would be to invoke browser by setting setHeadless as true through an instance of FirefoxOptions() class as follows:

    FirefoxOptions options = new FirefoxOptions();
    options.setHeadless(true);
    WebDriver driver = new FirefoxDriver(options);
    driver.get("https://www.google.com/");
    

    You can find a relevant detailed discussion in How to make Firefox headless programmatically in Selenium with Python?


    invalid argument: can’t kill an exited process

    This error is commonly observed in the two following cases:

    • When you are executing your tests as a root/admin user. It is recommended to execute your tests as a non-root/non-admin user.
    • When there is an incompatibility between the version of the binaries you are using.

    You can find a detailed discussion in WebDriverException: Message: invalid argument: can’t kill an exited process with GeckoDriver, Selenium and Python on RaspberryPi3

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search