Run Selenium With Headless Mode

Posted By :Hemant Chauhan |31st July 2020

Run selenium with headless mode

 

Selenium Web driver is a web automation tool which is used to run the test in various browsers such as Google Chrome, Firefox and Internet Explorer. Every browser needs a corresponding driver with selenium. By the help of these drivers at the time of test case is running, selenium automatically open the browser and executes test steps. You can easily see the browser and executed test actions on the screen.

 

Introduction to headless browser and headless testing

 

Headless browser is a web browser without GUI(graphical user interface). When we run the selenium test it behaves like a browser but not shown any GUI. In other words, when we run the selenium test case the browser is not shown on the screen, but it can run on background process and this process is called headless testing.

Today we mainly use Google chrome and Firefox for headless testing.

Before Google Chrome, headless execution testing was done by third-party browsers like PhantomJS, SlimerJS, Ghost and HTMLUnit.

 

In this article we do headless execution testing by Google Chrome using ChromeOptions and Xvfb.

 

1. Using ChromeOptions

For every chrome version there is a corresponding driver available. So firstly download the driver for respective operating system.

Google recently introduced a headless option for Chrome. It is available from version 59. If you want headless execution, so you need google chrome version 59 or above. It is very simple method if you want to done headless testing.The main advantage of this method of headless testing is it can run on all three OS platform(linux, windows and mac). It can saves so much time and increase peformance and speed in testing. Also you dont need to install browsers on servers.

ChromeOptions is class in selenium from which we set the arguements for headless execution.

For example:

 public class ChromeHeadlessTest {   
 @Test  
   public void testExecution() throws IOException {  
     System.setProperty("webdriver.chrome.driver", "<chromedriver_path of your local system>");  
     ChromeOptions options = new ChromeOptions();  
     options.addArguments("headless");  
     options.addArguments("window-size=1200x600");  
     WebDriver driver = new ChromeDriver(options);  
     driver.get("http://seleniumhq.org");  
     assertTrue(driver.findElement(By.id("q")).isDisplayed());  
     driver.quit();  
   }  
 }  


 

2. Using Xvfb( short for X virtual framebuffer)

 

We can also do headless execution testing using Xvfb in linux based systems(ubuntu, redhat and centos).

Xvfb or X virtual frame buffer is X11 server which does graphical operation in memory or in other words we say it is an in-memory display server specially for Unix distributed systems like linux. Using xvfb you can run graphical applications without display. Also it can have capability to take screenshots where htmlunit driver can’t take it.

Steps to setup Xvfb in your local system:


1.First we need to install xvfb using this command

sudo apt-get install Xvfb firefox

 

2. Then Start xvfb server by using this command
Xvfb :1 -screen 0 1024x768x24 &

The server will listen for connections as server number 1, and screen 0 will be depth 24 1024x768.

 

3. Then run the selenium test case, when you run this code it saves the screenshot image into directory and prints the title name of given URL.


 

 public class ChromeHeadlessTest1{  
 public static void main(String[] args) throws IOException {  
 ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()  
     .usingDriverExecutable(new File("/home/tarun/Downloads/chromedriver"))  
     .usingAnyFreePort().withEnvironment(ImmutableMap.of("DISPLAY", ":1")).build();  
 chromeDriverService.start();  
 WebDriver driver = new ChromeDriver(chromeDriverService);  
 driver.get("https://demoqa.com/buttons");  
 driver.manage().window().maximize();  
 JavascriptExecutor js = (JavascriptExecutor) driver;  
 TakesScreenshot scrShot = ((TakesScreenshot) driver);  
 File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);  
 String fileWithPath = "./src/main/resources/s.png";  
 File DestFile = new File(fileWithPath);  
 Files.copy(SrcFile.toPath(), DestFile.toPath(), StandardCopyOption.REPLACE_EXISTING);  
 System.out.println("Screenshot taken");  
 System.out.println("Title is " + driver.getTitle());  
 }}  


 


About Author

Hemant Chauhan

Hemant is an accomplished backend developer with extensive experience in software development. He possesses an in-depth understanding of various technologies and has a strong command over Java, Spring Boot, MySQL, Elasticsearch, Selenium with Java, GitHub/GitLab, HTML/CSS, and MongoDB. Hemant has worked on several related projects, including Tessaract OCR, Sikuli with Selenium Automation, Transleqo, and currently, SecureNow. He excels at managing trading bots, developing centralized exchanges, and has a creative mindset with exceptional analytical skills.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us