简体   繁体   中英

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

        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

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

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 ?

I have tried it using Firefox.You need to guide Firefox so that it executes in the 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. 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. 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());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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