【问题标题】:Running Selenium tests with Chrome on Ubuntu in a headless environment在无头环境中在 Ubuntu 上使用 Chrome 运行 Selenium 测试
【发布时间】:2012-10-19 02:26:32
【问题描述】:

关于这个主题有大量的帖子,但由于某种原因,我发现几乎没有与 Chrome 相关的内容(似乎最近添加了对 headless 的支持)。 测试在本地使用浏览器运行

  1. 是否可以从 Java 触发 Xvfb 显示,以便我可以在一个地方管理所有内容?我宁愿不必这样做:

    Xvfb :1 -screen 0 1024x768x24 &

通过命令行。运行显示然后将其关闭会很有用。

  1. SetEnvironmentProperty to ChromeDriver programatically 是我发现的解决方案。

问题?我无法让它在 Ubuntu 机器上运行。

我收到的错误是:

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

我正在使用的测试类:

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

    }

}

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    我切换到 chromedriver for linux,它摆脱了未终止的带引号的字符串错误http://code.google.com/p/chromedriver/downloads/list

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 2012-04-24
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多