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
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.
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 yourpom.xml
(Java 8 or upper required).Then simply add
WebDriverManager.firefoxdriver().setup();
in your code as shown below:See the basic examples of running
JUnit 4
tests on Firefox and Chrome usingWebDriverManager
here.--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:
Allow service to interact with desktop
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.
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 firefox browser.
An alternative would be to invoke firefox-headless browser by setting
setHeadless
astrue
through an instance ofFirefoxOptions()
class as follows:invalid argument: can’t kill an exited process
This error is commonly observed in the two following cases: