[英]Selenium code is not opening firefox browser when running selenium code in Jenkins server as Job
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).我在 Selenium/Java 测试 class 中有以下代码。现在,我已将此代码推送到 GitHub。此外,我还设置了 Jenkins 作业来执行相同的代码(在 Jenkins 作业中,我已将代码指向 GitHub)。 The Jenkins job is triggering fine and started executing the test, but throwing below error while opening the browser.
Jenkins 作业触发良好并开始执行测试,但在打开浏览器时抛出以下错误。 The test case is supposed to open the Firefox browser, but the Firefox browsing is not opening.
测试用例本应打开Firefox浏览器,但Firefox浏览器打不开。
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).所以,我的问题是,如果我想在 Jenkins 作业(Jenkins 服务器运行在 Cento7.4 OS)中执行测试用例,下面的 selenium 代码是否正确。
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.注意:在同一个 CentOS VM 中,我能够在 eclipse 中执行相同的(以下)selenium 代码,它能够打开 Firefox 浏览器并打开 URL,没有任何问题。 The issue is coming only if I try to run the same code in the Jenkins server as a Jenkins job.
只有当我尝试在 Jenkins 服务器中运行与 Jenkins 作业相同的代码时,问题才会出现。
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 xauth list
output[root@localhost ~]# xauth list
localhost.localdomain/unix:0 MIT-MAGIC-COOKIE-1 4eb74af687f2dbc022ef03617614456e
#ffff#6c6f63616c686f73742e6c6f63616c646f6d61696e#:0 MIT-MAGIC-COOKIE-1 4eb74af687f2dbc022ef03617614456e
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 ).您可能需要研究设置 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.
问题是您的 Jenkins 服务器无法打开显示 0 以运行。请注意发送到 firefox 二进制文件的参数,在您的 firefoxOptions 中指定显示 0 与二进制文件执行的 INFO 日志行匹配。 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.
使用 xvfb,您应该能够指定屏幕编号并相应地设置您的配置,或者简单地使用 xvfb-run。
The test case is supposed to open the Firefox browser, but the Firefox browsing is not opening.
测试用例本应打开Firefox浏览器,但Firefox浏览器打不开。
To resolve this issue, use WebDriverManager to automate the management of the drivers (eg chromedriver, geckodriver, etc.) required by Selenium WebDriver .要解决此问题,请使用WebDriverManager自动管理Selenium WebDriver所需的驱动程序(例如 chromedriver、geckodriver 等)。
To use WebDriverManager
in a Maven project, add the following dependency in your pom.xml
(Java 8 or upper required).要在 Maven 项目中使用
WebDriverManager
,请在pom.xml
中添加以下依赖项(需要 Java 8 或更高版本)。
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.2.2</version>
</dependency>
Then simply add WebDriverManager.firefoxdriver().setup();
然后简单地添加
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 .请参阅此处使用
WebDriverManager
在 Firefox 和 Chrome 上运行JUnit 4
测试的基本示例。
--display=n
The phrase display is used to refer to collection of monitors that share a common keyboard and pointer eg 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.
多用户系统通常有多个显示器,并且在启动该显示器的 X 服务器时,机器上的每个显示器都会分配一个显示器编号(从 0 开始)。
display:0
is usually the local display ie the main display of the computer. display:0
通常是本地显示器,即计算机的主显示器。
When Jenkins executes the batch file, Jenkins slave runs as service in the background for every program it initiates.当 Jenkins 执行批处理文件时,Jenkins slave 为它启动的每个程序在后台作为服务运行。 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.
所以通常你无法看到 Firefox 浏览器正在旋转,但在任务管理器中你可以看到 Jenkins 在后台打开了几个 Firefox 进程。
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 .如果您将 Jenkins 用作 windows 服务,则需要Allow service to interact with desktop 。 Steps:
脚步:
Allow service to interact with desktop
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.在这种方法中, dbus-daemon的自动启动在 X11 session 下工作,否则它被禁用,因为不同的应用程序无法建立 dbus 守护进程的公共实例。
You can find a relevant detailed discussion in Jenkins: Selenium GUI tests are not visible on Windows
你可以在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.另一种方法是从命令提示符运行 Jenkins 作为
java -jar jenkins.war
而不是 windows 安装程序版本。
Another approach will be to use RemoteWebDriver .另一种方法是使用RemoteWebDriver 。 From Jenkins, make sure there is a machine where the selenium tests can run.
从 Jenkins,确保有一台机器可以运行 selenium 测试。 On this server you spin up the firefox browser.
在此服务器上,您启动firefox浏览器。
You can find a relevant detailed discussion in How to launch chrome browser from Jenkins directly instead of using code in eclipse
您可以在如何直接从 Jenkins 启动 chrome 浏览器而不是使用 eclipse 中的代码中找到相关的详细讨论
An alternative would be to invoke firefox-headless browser by setting setHeadless
as true
through an instance of FirefoxOptions()
class as follows:另一种方法是通过
FirefoxOptions()
class 实例将setHeadless
设置为true
来调用firefox-headless浏览器,如下所示:
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?
您可以在How to make Firefox headless in Selenium with Python? 中找到相关的详细讨论?
This error is commonly observed in the two following cases:在以下两种情况下通常会观察到此错误:
You can find a detailed discussion in WebDriverException: Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium and Python on RaspberryPi3
您可以在WebDriverException 中找到详细的讨论:消息:参数无效:无法使用 GeckoDriver 终止退出的进程,Selenium 和 Python on RaspberryPi3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.