DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Running Selenium Tests With Chrome Headless

Running Selenium Tests With Chrome Headless

Learn how to use Java to execute tests in a headless Google Chrome browser and make testing your web applications a little easier.

Elias Nogueira user avatar by
Elias Nogueira
CORE ·
Jun. 17, 17 · Web Dev Zone · Tutorial
Like (7)
Save
Tweet
47.58K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Before Google Chrome 59, headless execution had to be done by third-party headless browsers like PhantomJS, SlimerJS, TrifleJS, Nightmare, and HTMLUnit.

The "problem" is that these headless browsers emulate some engines, but not V8 (the Chrome engine).

Headless Browser Engine
PhantomJS QtWebKit
SlimerJS Gecko (Firefox)
TrifleJS Trident (IE)
Nightmare Electron

When we talk about testing, it's necessary to simulate Google's real engine, as both Internet users and web devs tend toward using Google Chrome.

How to Do it With Java

As Google Chrome ships with headless execution in version 59 (as you can see here) we can tell ChromeDriver the options before the execution.

In the code below I added two options through ChromeOptions: headless and the window-size.

// imports omitted
public class ChromeHeadlessTest {

    @Test
    public void testExecution() throws IOException {
        System.setProperty("webdriver.chrome.driver", "<chromedrier_path>");

        // Add options to Google Chrome. The window-size is important for responsive sites
        ChromeOptions options = new ChromeOptions();
        options.addArguments("headless");
        options.addArguments("window-size=1200x600");

        WebDriver driver = new ChromeDriver(options);
        driver.get("http://seleniumhq.org");

        // a guarantee that the test was really executed
        assertTrue(driver.findElement(By.id("q")).isDisplayed());

        driver.quit();
    }
}

The headless option will tell to Google Chrome to execute in headless mode. The window-size is a way to control the responsiveness (and allows your site be displayed, like a mobile site, if you have not set a window size).

You can see some other ways to inform the ChromeOptions here.

Google Chrome

Published at DZone with permission of Elias Nogueira. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Composable Architecture
  • DZone's Article Submission Guidelines
  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Java Hashtable, HashMap, ConcurrentHashMap: Performance Impact

Comments

Web Dev Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo