简体   繁体   English

在 Linux 服务器上运行 Selenium WebDriver 测试

[英]Run Selenium WebDriver test on a linux server

I'm trying to run a test implemented with selenium webdriver on a Linux server with chrome and no display this my java code我正在尝试在带有 chrome 的 Linux 服务器上运行使用 selenium webdriver 实现的测试,但没有显示我的 java 代码

        System.setProperty("webdriver.chrome.driver","/home/exploit/Bureau/chromedriver");
        WebDriver driver = new ChromeDriver();  
        driver.get("https://www.google.com");
        WebElement element = driver .findElement(By.id("lst-ib"));

to run this program (jar) a start the Xvfb with the command运行这个程序 (jar) a 用命令启动 Xvfb

Xvfb :1 -screen 5 1024x768x8 &
export DISPLAY=:1.5

when I run the program I got this Exception after a bit long waiting当我运行程序时,经过漫长的等待,我得到了这个异常

12:39:53.483 [Forwarding newSession on session null to remote] DEBUG           o.a.h.i.conn.DefaultClientConnection - Connection 0.0.0.0:51411<->127.0.0.1:9069 closed
12:39:53.483 [Forwarding newSession on session null to remote] DEBUG o.a.h.i.conn.tsccm.ConnPoolByRoute - Notifying no-one, there are no waiting threads
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.9.248304,platform=Linux 3.10.0-123.13.2.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
  Command duration or timeout: 60.69 seconds
  Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
  System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-    123.13.2.el7.x86_64', java.version: '1.7.0_79'
  Driver info: driver.version: ChromeDriver
  Session ID: 6c811fab5c809544094e1f9e1d96ef6a
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:531)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:215)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:161)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
    at com.atos.esope.Extractor.extTest(Extractor.java:76)
    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:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
    at com.sun.proxy.$Proxy27.extTest(Unknown Source)
    at com.atos.esope.Program.main(Program.java:32)

and when I try to run chrome separately a got this problem当我尝试单独运行 chrome 时遇到了这个问题

Xlib:  extension "RANDR" missing on display ":1.5".
Xlib:  extension "RANDR" missing on display ":1.5".
Xlib:  extension "RANDR" missing on display ":1.5".
Xlib:  extension "RANDR" missing on display ":1.5".
Xlib:  extension "RANDR" missing on display ":1.5".
Xlib:  extension "RANDR" missing on display ":1.5".
Xlib:  extension "RANDR" missing on display ":1.5".
[3207:3207:0505/171255:ERROR:url_pattern_set.cc(240)] Invalid url pattern:         chrome://print/*
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.

the questions are :问题是:

is the problem in locating the driver or in chrome or I need some additional configuration ?是在定位驱动程序还是在 chrome 中出现问题,或者我需要一些额外的配置?

I have tried it using Firefox.You need to guide Firefox so that it executes in the Xvfb.我已经用 Firefox 试过了。你需要引导 Firefox 以便它在 Xvfb 中执行。

You can try the following:您可以尝试以下操作:

String Xport = System.getProperty("lmportal.xvfb.id", ":1");

final File firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "/usr/bin/firefox"));

FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);

firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);

Here error log Chrome failed to start: exited abnormally clearly states system was not able to start chromedriver but it was able to find it.这里错误日志Chrome failed to start: exited abnormally清楚地表明系统无法启动 chromedriver 但它能够找到它。 As you are trying to run on linux server using jenkins, just try to download and enable plugin XVFB on jenkins and use this block of code.当您尝试使用 jenkins 在 linux 服务器上运行时,只需尝试在 jenkins 上下载并启用插件 XVFB 并使用此代码块。 Hope this helps!希望这可以帮助!

            if(service == null){
                service = new ChromeDriverService.Builder()
                        .usingDriverExecutable(new File("/usr/bin/chromedriver"))// set the chromedriver path
                       .usingAnyFreePort()
                        .withEnvironment(ImmutableMap.of("DISPLAY", ":15"))
                        .withSilent(true)
                        .build();
                service.start();
            }

            System.out.println("Reading chrome driver");
            System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--headless");
            WebDriver driver = new ChromeDriver(chromeOptions);
            driver.get("https://google.com");
            driver.quit();
 }
        catch(Exception ex){
        System.out.println(ex.getMessage());
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM