title: Running Selenium tests with Chrome on Ubuntu in a headless environment
body: <p>There's a massive amount of postings on this topic, but for some reason, I've found little relating to Chrome (it seems the support was recently added for headless). <strong>The tests work running locally with a browser</strong>.</p>
  
  <ol>
  <li><p>Is it possible to trigger an Xvfb display from Java so I can manage everything in one place? I'd rather not have to do:</p>
  
  <p>Xvfb :1 -screen 0 1024x768x24 &amp;</p></li>
  </ol>
  
  <p>Via the command line. It'd be useful to run a display, then shut it down.</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/7722615/setenvironmentproperty-to-chromedriver-programatically">SetEnvironmentProperty to ChromeDriver programatically</a> is what I found to be the solution. </li>
  </ol>
  
  <p>The problem? I can't get it to work on an Ubuntu machine.</p>
  
  <p>The error I receive is:</p>
  
  <pre><code>/home/ubuntu/myproject/conf/chromedriver: 1: Syntax error: Unterminated quoted string
  </code></pre>
  
  <p>The test class I'm using:</p>
  
  <pre><code>import java.io.File;
  
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.chrome.ChromeDriverService;
  
  import play.Logger;
  import utils.LocalInfo;
  
  import com.google.common.collect.ImmutableMap;
  
  public class TestCI {
  
      private WebDriver driver;
  
      @SuppressWarnings("deprecation")
      public TestCI(String url) {
  
          if (!LocalInfo.isEC2()) {
  
              Logger.info("Running tests by opening a browser...");
  
              System.setProperty("webdriver.chrome.driver", "conf/chromedriver");
  
              setWebDriver(new ChromeDriver());
  
          } else {
  
              Logger.info("Running tests headlessly...");
  
              String xPort = System.getProperty("Importal.xvfb.id", ":1");
  
              ChromeDriverService service = new ChromeDriverService.Builder()
                      .usingChromeDriverExecutable(new File("conf/chromedriver"))
                      .usingAnyFreePort()
                      .withEnvironment(ImmutableMap.of("DISPLAY", xPort)).build();
  
              setWebDriver(new ChromeDriver(service));
  
          }
  
          getWebDriver().get("http://www.google.com");
  
          try {
              someUITest();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          getWebDriver().close();
  
      }
  
      public WebDriver getWebDriver() {
          return driver;
      }
  
      public void setWebDriver(ChromeDriver driver) {
          this.driver = driver;
      }
  
      public void someUITest() throws Exception {
  
          getWebDriver().findElement(By.name("q"));
  
      }
  
  }
  </code></pre>