{"id":4598,"date":"2020-08-28T16:58:42","date_gmt":"2020-08-28T11:28:42","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4598"},"modified":"2025-04-02T06:53:17","modified_gmt":"2025-04-02T10:53:17","slug":"selenium-headless-browser-testing","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/selenium-headless-browser-testing\/","title":{"rendered":"Selenium Headless Browser Testing"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Headless Browser?<\/strong><\/h2>\n\n\n\n<p>A headless browser testing is a browser simulation program that does not have a graphic user interface (UI less). The programs of the Headless browser will operate like any other browser but will not display any UI. Selenium executes its tests in the background.<\/p>\n\n\n\n<p>There are several headless browsers available in the current market the following are the most popular ones.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ghost<\/li>\n\n\n\n<li>HtmlUnit<\/li>\n\n\n\n<li>Phantom JS<\/li>\n\n\n\n<li>Watir-webdriver<\/li>\n\n\n\n<li>ZombieJS<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is headless testing?<\/strong><\/h2>\n\n\n\n<p>Executing the web applications UI tests without opening a browsers user interface is called headless browser testing. A Headless browser will similarly act like a normal web browser. Testers will have full control over the web pages loaded into the headless browsers the only difference is you will not see a graphical user interface.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>When to use headless browser testing?<\/strong><\/h2>\n\n\n\n<p>We use headless testing once the cross-browser testing is completed and want to run regression test cases in subsequent releases and with continuous integration. It is recommended to use a <a href=\"https:\/\/www.h2kinfosys.com\/blog\/selenium-headless-browser-testing\/\">headless browser<\/a> when tests are executed in parallel as User Interface based browsers and consumes a lot of memory or resources.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>HTMLUnitDriver<\/strong><\/h2>\n\n\n\n<p>By importing the <a href=\"https:\/\/github.com\/SeleniumHQ\/htmlunit-driver\" rel=\"nofollow noopener\" target=\"_blank\">HtmlUnitDriver <\/a>class in selenium the Headless browser can be achieved. A Headless browser is used to perform a functional test, load test, and regression test as it is the fastest and lightweight of WebDriver API. These programs just behave like a browser but do not show any GUI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Advantages of HtmlUnitDriver<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It is Lightweight and fastest to implement<\/li>\n\n\n\n<li>It is used to perform tests such as functional tests, load tests, regression tests, and sanity tests in the server without installing the browsers.<\/li>\n\n\n\n<li>It can easily run test cases on multiple browser versions.<\/li>\n\n\n\n<li>It can access the content of the web pages quickly without having to load them.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>&nbsp;Disadvantages of HtmlUnitDriver<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Even though they support browser features like (cookies, HTML parsing) however they do not provide DOM elements of JavaScript. It uses the Rhino JavaScript engine.<\/li>\n\n\n\n<li>Since the Headless browser uses JavaScript engine, which Java Script slightly configured is different than the W3C standard. However, we can run the test cases with JavaScript or without JavaScript with an option.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps to Use HTMLUnit Driver with Selenium<\/strong><\/h2>\n\n\n\n<p><strong>Step 1:<\/strong>\u00a0Launch the Eclipse and copy the below code and add the standard <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium testing <\/a>library files to the project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> htmlunitdriver;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n\/\/import org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.htmlunit.HtmlUnitDriver;\n\n\n<strong>public<\/strong> <strong>class<\/strong> htmlUnitTest {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {\n&nbsp; &nbsp; HtmlUnitDriver driver = <strong>new<\/strong> HtmlUnitDriver();\n&nbsp; &nbsp; driver.setJavascriptEnabled(<strong>true<\/strong>);\n&nbsp; &nbsp; driver.get(\"http:\/\/www.google.com\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Title of the page is\" + driver.getTitle());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp; &nbsp; WebElement element = driver.findElement(By.<em>name<\/em>(\"q\"));\n&nbsp; &nbsp; element.sendKeys(\"Selenium Framework\");\n&nbsp; &nbsp; element.submit();\n&nbsp;\n&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Title of the web page&nbsp; is \" + driver.getTitle());\n}\n\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 2: <\/strong>Run the above code.<strong>&nbsp;<\/strong>You will observe that no browser is launched and results are shown in the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;Enabling JavaScript<\/h3>\n\n\n\n<p>Java Script can be enabled in two ways:<\/p>\n\n\n\n<p>1. By passing JavaScript enable flag to&nbsp;HtmlUnitDriver&nbsp;class as a constructor<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HtmlUnitDriver driver = new HtmlUnitDriver(true);<\/code><\/pre>\n\n\n\n<p>2. By using&nbsp;setJavaScriptEnabled&nbsp;method<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HtmlUnitDriver driver = new HtmlUnitDriver(); Driver.setJavaScriptEnabled(true);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> htmlunitdriver;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n\/\/import org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.htmlunit.HtmlUnitDriver;\n\n\n<strong>public<\/strong> <strong>class<\/strong> htmlUnitTest {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {\n&nbsp; &nbsp; HtmlUnitDriver driver = <strong>new<\/strong> HtmlUnitDriver();\n&nbsp; &nbsp; driver.setJavascriptEnabled(<strong>true<\/strong>);\n&nbsp; &nbsp; driver.get(\"http:\/\/www.google.com\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"The Title of the web page is\" + driver.getTitle());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp; &nbsp; WebElement java = driver.findElement(By.<em>name<\/em>(\"q\"));\n&nbsp; &nbsp; java.sendKeys(\"Selenium Automation\");\n&nbsp; &nbsp; java.submit();\n&nbsp;\n&nbsp; &nbsp; System.<strong><em>out<\/em><\/strong>.println(\"The Title of the web page is \" + driver.getTitle());\n}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Enabling different types of Browsers and versions.<\/strong><\/h3>\n\n\n\n<p>Types of Browsers can be added by passing&nbsp;BrowserVersion&nbsp;as constructor HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> htmlunitdriver;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n\/\/import org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.htmlunit.HtmlUnitDriver;\n<strong>import<\/strong> com.gargoylesoftware.htmlunit.BrowserVersion;\n\n\n\n<strong>public<\/strong> <strong>class<\/strong> htmlUnitTest {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HtmlUnitDriver driver = <strong>new<\/strong> HtmlUnitDriver(BrowserVersion.FIREFOX_45);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.get(\"http:\/\/www.google.com\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"The Title of the page is\" + driver.getTitle());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebElement java = driver.findElement(By.<em>name<\/em>(\"q\"));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java.sendKeys(\"Selenium Automation Framework\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java.submit();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"The Title of the web page is \" + driver.getTitle());\n&nbsp;\n}\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Headless Chrome in Selenium<\/strong><\/h2>\n\n\n\n<p><strong>Step1: <\/strong>Create a Package (headless)<\/p>\n\n\n\n<p><br><strong>Step2:<\/strong> Create a Class(HeadLessChrome)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package headless;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\nimport org.openqa.selenium.chrome.ChromeOptions;\n\npublic class HeadLessChrome {\npublic static void main(String&#91;] args) {\n\nSystem.setProperty(\u201cwebdriver.chrome.driver\u201d,\u201dC:\/Drivers\/chromedriver_win32\/\nchromedriver.exe\u201d);\nChromeOptions options = new ChromeOptions();\noptions.setHeadless(true);\n\/\/options.addArguments(\u201c\u2014headless\u201d);\nWebDriver driver = new ChromeDriver(options);\ndriver.get(\u201c<a href=\"https:\/\/demo.nopcommerce.com\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/demo.nopcommerce.com\/<\/a>\u201d);\nSystem.out.println(\u201cTitle of the page:\u201d+driver.getTitle());\n\n}\n}<\/code><\/pre>\n\n\n\n<p>When you run the above program, it will print the Title of the webpage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Headless Firefox in Selenium<\/strong><\/h2>\n\n\n\n<p><strong>Step1:<\/strong> Create a Package (headless)<\/p>\n\n\n\n<p><strong>Step2: <\/strong>Create a Class(HeadLessFirefox)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package headless;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.FirefoxDriver;\nimport org.openqa.selenium.chrome.FirefoxOptions;\n\npublic class HeadLessFirefox {\npublic static void main(String&#91;] args) {\n\nSystem.setProperty(\u201cwebdriver.gecko.driver\u201d,\u201dC:\/Drivers\/geckodriver-v0.23.0-win64\/\ngeckodriver.exe\u201d);\nFirefoxOptions options = new FirefoxOptions();\noptions.setHeadless(true);\nWebDriver driver = new FirefoxDriver(options);\ndriver.get(\u201c<a href=\"https:\/\/demo.nopcommerce.com\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/demo.nopcommerce.com\/<\/a>\u201d);\nSystem.out.println(\u201cTitle of the page:\u201d+driver.getTitle());\n\n}\n}<\/code><\/pre>\n\n\n\n<p>When you run the above program, it will print the Title of the webpage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion:<\/strong><\/h2>\n\n\n\n<p>To rapidly test the application in various browsers and without any interruption, headless browser&nbsp;testing is used. HTML unit driver, Chrome, and Firefox are popular for headless <a href=\"https:\/\/www.h2kinfosys.com\/blog\/cross-browser-testing-using-selenium-webdriver\/\">browser testing<\/a> due to its speed, accuracy, y and easy to access features.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Headless Browser? A headless browser testing is a browser simulation program that does not have a graphic user interface (UI less). The programs of the Headless browser will operate like any other browser but will not display any UI. Selenium executes its tests in the background. There are several headless browsers available in [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":6879,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[156,1899,1298,1300,1301,1900,1901,45,448,1299],"class_list":["post-4598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-automation-testing","tag-headless-browser","tag-headless-browser-testing","tag-headless-chrome","tag-headless-firefox","tag-headless-testing","tag-htmlunitdriver","tag-selenium","tag-selenium-webdriver","tag-what-is-headless-testing"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4598"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4598\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/6879"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}