top of page
  • Writer's picturekhyati sehgal

How to run Selenium tests in headless mode.

Quick, pace, speed are some synonyms that describe us. We, humans, want pace in each and every act of ours. We want to speed up things as much as we can do. So we try experimenting with things as we try to search for betterment of work, as we are growing in automating things, replacing human acts to programs or machinery.

If I talk about selenium, then the major time which is spent in the framework is at execution. And if I go one step deeper then the initial point where the execution takes time is when it is trying to open browser be it Chrome, IE, or any other. So just to wipe out this pain point there is one such browser that works in headless mode and is compatible with selenium as well.

How can we use it?

Let’s talk in code.

private WebDriver driver;
 private String baseUrl = "http://www.google.com";
 @Before
 public void setUp() throws Exception {
 System.out.println("Begin Setup");
 driver = new HtmlUnitDriver();
 driver.get(baseUrl);
 driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
 } 

What this piece of code will do is, it will create a webdriver instance for you. And as mentioned the instance would be of HTML UNIT DRIVER.

Lets understand this line-by-line.

private WebDriver driver;

This is when you are making a driver instance.

private String baseUrl = “http://www.google.com”

Here you are calling a web URL, which would act as the base URL for your whole class.

driver = new HtmlUnitDriver();

This is when you are mentioning the name of the browser you want to work with. And other functionality will work as is.

Advantage

The main advantage is you need not install any driver for this in your OS. It works faster than the usual browsers like Chrome, IE, FF, etc.

1 view0 comments
bottom of page