如何使用java在chrome驱动程序中运行无头模式

时间:2017-02-09 09:34:27

标签: java google-chrome selenium junit headless

这是我的Chrome驱动程序代码: -

String driverPath = driverFile.getAbsolutePath();
                System.setProperty("webdriver.chrome.driver", driverPath);

 Callable<ChromeDriver> startChromedriver = new Callable<ChromeDriver>() {
                    public ChromeDriver call() {
                        ChromeOptions Chromeoptions = new ChromeOptions();
                        Chromeoptions.addArguments("--startMaximized");


                        caps.setCapability("newCommandTimeout", 300);
                        caps.setCapability(ChromeOptions.CAPABILITY, Chromeoptions);

                        return new ChromeDriver(caps);
                    }
                };

我在终端开始了以下内容: -

Xvfb -ac :99 -screen 0 1280x1024x16 &

export DISPLAY=:99

然后在Intellij开始我的Junit测试

如何在ubuntu机器上使用chrome驱动程序在java中运行junit test?

对于Firefox,我尝试了它的工作。

apt-get update

sudo apt-get install xvfb

sudo apt-get install -y xorg xvfb dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic


sudo Xvfb :2 -ac


export DISPLAY=:2

在firefox中设置

// Setup firefox binary to start in Xvfb
String Xport = System.getProperty(
"lmportal.xvfb.id", ":2");
final File firefoxPath = new File(System.getProperty(
"lmportal.deploy.firefox.path", "/usr/bin/firefox"));
FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);

// Start Firefox driver
WebDriver driver = new FirefoxDriver(firefoxBinary, null);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://google.com/");

System.out.println("page source" + driver.getCurrentUrl());

2 个答案:

答案 0 :(得分:2)

自Chrome 59以来,您甚至不需要Xvfb。

在此处下载Chrome驱动程序:https://chromedriver.storage.googleapis.com/index.html?path=2.38/

或者在macOS上:

<dependency>  
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.8.1</version>
</dependency> 

然后在pom.xml / graddle中添加最新版本的Selenium:

String chromeDriverPath = "/Path/To/Chromedriver" ;  
System.setProperty("webdriver.chrome.driver", chromeDriverPath);  
ChromeOptions options = new ChromeOptions();  
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");  
WebDriver driver = new ChromeDriver(options); 

当然,您需要Chrome版本&gt; 59

这是Java部分:


SELECT * FROM ( 
    SELECT 
        d.created_at, d.response, 
        Avg(d.response) OVER(ORDER BY d.created_at::date rows BETWEEN 29 PRECEDING AND CURRENT row) AS rolling_average 
    FROM ( 
        SELECT 
            COALESCE(a.created_at, d.dates) AS created_at, response, a.user_id 
        FROM 
            (SELECT generate_series('2018-01-01'::date, '2018-05-31'::date, '1day'::interval)::date AS dates) d 
        LEFT JOIN 
            (SELECT * FROM answers WHERE answers.user_id = 'insert_user_id' AND ( answers.response = 0 OR answers.response = 1)) a 
        ON d.dates = a.created_at::date 
    ) d 
    GROUP BY d.created_at, d.response 
) agg WHERE agg.response IS NOT NULL 
ORDER BY agg.created_at::date

我写了一篇博客文章,详细说明如下: https://ksah.in/introduction-to-chrome-headless/

答案 1 :(得分:0)

使用redwoodHQ服务器和Linux代理。然后BrOwse到服务器驱动代理。