Launch HTMLUnitDriver & PhantomJS for Headless Browser Testing in Selenium:
What is a headless browser in Selenium?
- The headless browser is a web browser, which has no graphical user interface.
- Headless browsers offer automatic control over a web page in an environment like a popular web browser, but are executed through command line interfaces or network communications.
Types of Headless Driver’s:
- HtmlUnit
- PhantomJS
- Ghost
- ZombieJS
1. HTMLUnitDriver:
- HTML UnitDriver is the lightest weight and fastest implementation browser for WebDriver, based on HtmlUnit, it is known as a headless browser driver, it is similar to a Chrome, IE, or FireFox driver.
- Does not have a GUI, so the screen can not see test execution on screen.
Features of HTML Unit Driver:
- Speed and performance improvements.
- Allows to test browserless setup.
- Support for HTTPS and HTTP protocols.
- Helps you in multitask.
- Help for cookies.
- Excellent JavaScript Support.
Initialize HtmlUnitDriver:
1 | WebDriver driver = new HtmlUnitDriver(); |
Example of Launching HtmlUnitDriver:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class InternetExplorerDriver{ public static void main(String[] args) { WebDriver driver=new HtmlUnitDriver(); driver.get("https://www.stqatools.com"); System.out.println("Title is :-"+driver.getTitle()); driver.close(); } } |
Output:
1 | Title is :- Software Testing Quality Automation Tutorials |
2. PhantomJS:
- PhantomJS is a headless browser with Javascript APIs Headless Website is an optimal solution for manipulation, access and webpages and comes with standard DOM API.
- PhantomJS gives you the advantage of capturing screenshots so that you can override the user-agent string header from the related server and copy the next browser.
- The latest release of PhantomJS has integrated GhostDriver and there is no need to install it separately.
- GhostDriver is a pure JavaScript implementation, which is the WebDrive Wire Protocol for PhantomJS.
Features of PhantomJS:
- Able to Headless Testing.
- Screen capture Capability.
- Able to Network monitoring.
- To run a unit test on the command line.
- To prepare an employee manual for PDF from HTML.
- Development is very easy with PhantomJS, when you develop UI, you can write code by selecting the HTML element.
- Testers need to do manual testing, which seems easy with the PhantomJS.
Download PhantomJS:
- To Download click on this link to download Windows, Mac OS X, Linux 64-bit, Linux 32-bit OS drivers.
Example of Launching PhantomJS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities; public class While_Java { public static void main(String[] args) { DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://phantomjs.exe"); caps.setCapability("takesScreenshot", true); WebDriver driver = new PhantomJSDriver(caps); driver.get("https://www.stqatools.com"); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); String title = driver.getTitle(); System.out.println(title); } } |