英文:
Selenium code is not opening firefox browser when running selenium code in Jenkins server as Job
问题
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");
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
英文:
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
答案1
得分: 2
你可能需要考虑设置 xvfb(https://centos.pkgs.org/7/centos-x86_64/xorg-x11-server-Xvfb-1.20.4-10.el7.x86_64.rpm.html)。问题在于你的 Jenkins 服务器无法打开显示器 0 来运行。请注意,发送给 Firefox 二进制文件的参数,指定在 firefoxOptions 中显示 0,与二进制执行的 INFO 日志行相匹配。假设你正在运行一个无头服务器,这就是为什么会出现此错误的原因。当在本地运行时情况不同。
通过 xvfb,你应该能够指定一个屏幕编号并相应地设置配置,或者简单地使用 xvfb-run。
英文:
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.
答案2
得分: 1
>测试用例应该打开Firefox浏览器,但是Firefox浏览器没有打开。
为了解决这个问题,可以使用WebDriverManager来自动管理Selenium WebDriver所需的驱动程序(例如chromedriver、geckodriver等)。
在Maven项目中使用WebDriverManager
,请在你的pom.xml
中添加以下依赖(需要Java 8或更高版本)。
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.2.2</version>
</dependency>
然后在代码中添加WebDriverManager.firefoxdriver().setup();
,如下所示:
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);
在这里可以查看使用WebDriverManager
在Firefox和Chrome上运行JUnit 4
测试的基本示例:这里。
英文:
>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.
答案3
得分: 0
--display=n
是用来指代一组共享一个通用键盘和指针(例如鼠标、触控板等)的监视器集合的短语。大多数工作站通常只有一个键盘,因此只有一个显示器。多用户系统通常会有多个显示器,每个显示器在启动为该显示器提供X服务器时都会被分配一个显示编号(从0开始)。display:0
通常是本地显示,即计算机的主显示器。
英文:
--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:
- Open properties window of the Jenkins service -> Logon-> enable the checkbox
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.
>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 [tag:firefox] 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 [tag:firefox-headless] 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论