简体   繁体   English

在无头环境中的Ubuntu上使用Chrome运行Selenium测试

[英]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). 关于此主题的帖子很多,但由于某种原因,我发现与Chrome的关系不大(似乎最近增加了对无头的支持)。 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? 是否可以从Java触发Xvfb显示,以便我可以在一处管理所有内容? I'd rather not have to do: 我宁愿不必这样做:

    Xvfb :1 -screen 0 1024x768x24 & Xvfb:1-屏幕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. 我发现可以通过编程SetEnvironmentProperty设置为ChromeDriver

The problem? 问题? I can't get it to work on an Ubuntu machine. 我无法在Ubuntu计算机上使用它。

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

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

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