简体   繁体   中英

Running Selenium tests with Chrome on Ubuntu in a headless environment

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). The tests work running locally with a browser .

  1. 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:

    Xvfb :1 -screen 0 1024x768x24 &

Via the command line. It'd be useful to run a display, then shut it down.

  1. SetEnvironmentProperty to ChromeDriver programatically is what I found to be the solution.

The problem? I can't get it to work on an Ubuntu machine.

The error I receive is:

/home/ubuntu/myproject/conf/chromedriver: 1: Syntax error: Unterminated quoted string

The test class I'm using:

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"));

    }

}

我切换到适用于Linux的chromedriver,它摆脱了无休止的带引号的字符串错误http://code.google.com/p/chromedriver/downloads/list

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