Galet
Galet

Reputation: 6309

How to run headless mode in chrome driver using java

Here is my chrome driver code:-

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

I have started the following in terminal:-

Xvfb -ac :99 -screen 0 1280x1024x16 &

export DISPLAY=:99

and then started my Junit test in Intellij

How do I run junit test in java using chrome driver on ubuntu machine?

For Firefox, I have tried and its working.

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

Setup in 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());

Upvotes: 2

Views: 7694

Answers (2)

ksahin
ksahin

Reputation: 226

Since Chrome 59 you don't even need Xvfb.

Download Chrome driver here : https://chromedriver.storage.googleapis.com/index.html?path=2.38/

Or on macOS :

brew install chromedriver

Then add a recent version of Selenium in your pom.xml/graddle:

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

Of course you will need a Chrome version > 59

And here comes the Java part:

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

I wrote a blog post with detailed instruction here : https://ksah.in/introduction-to-chrome-headless/

Upvotes: 4

AaronBDC
AaronBDC

Reputation: 118

use redwoodHQ server and agent for Linux .then BrOwse to server to drive agent .

Upvotes: 0

Related Questions