[Selenium+java] Selenium with Htmlunit Driver & PHANTOMJS

Source: Internet
Author: User
Tags guru99

Original url:https://www.guru99.com/selenium-with-htmlunit-driver-phantomjs.html

Htmlunitdriver & PHANTOMJS for Selenium Headless testing

Selenium Web Driver is a Web automation tool which enables your to run the tests against different browsers. These browsers can be Internet Explorer, Firefox or Chrome. To use a particular browser with Selenium you need corresponding driver.

At Test run, Selenium launches the corresponding browser called in script and executes test steps. You can see the browser and the test execution in action.

What is Headless Browser?

A headless browser is a web-browser without a graphical user interface. This program would behave just like a browser but would not show any GUI.

Some of the examples of Headless Drivers include

    • Htmlunit
    • Ghost
    • Phantomjs
    • Zombiejs
    • Watir-webdriver

In this tutorial we'll focus on Htmlunit and PHATOMJS

Htmlunitdriver

HTML Unitdriver is the most light weight and fastest implementation headless browser for of Webdriver. It's based on Htmlunit. It is known as Headless Browser Driver. It's same as Chrome, IE, or FireFox driver, but it does not has GUI so one cannot see the test execution on screen.

Features of HTML Unit driver

    • Support for the HTTPS and HTTP protocols
    • Support for HTML responses (clicking Links, submitting forms, walking the DOM model of the HTML document etc.)
    • Support for Cookies
    • Proxy Server Support
    • Support for basic and NTLM authentication
    • Excellent JavaScript support
    • Support for submit methods GET and POST
    • Ability to customize the request headers being sent to the server
    • Ability to determine whether failing responses from the server should throw exceptions or should be returned as pages of T He appropriate type
Steps to use Htmlunit Driver with Selenium

Step 1) In Eclipse, copy the following code. ADD the standard Selenium library files to the project. No additional jar files are required.

Package Htmldriver;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.htmlunit.htmlunitdriver;public class HtmlUnitYest {                                           public static void Main (string[] args) {//Creating a new instance of the HTML unit driver                                      Webdriver Driver = new Htmlunitdriver ();           Navigate to Google driver.get ("http://www.google.com");                                         Locate the searchbox using its name webelement element = Driver.findelement (By.name ("Q"));                               Enter a search query Element.sendkeys ("Guru99"); Submit the query. Webdriver searches for the form using the text INPUT element automatically//No need to locate/find th                          E submit button Element.submit ();      This code would print the page title System.  out. println ("page title is:" + driver.gettitle ());         Driver.quit (); }}

Step 2) Run the code. You'll observer no browser is launched and results be shown in console.

Benefits of Html Unit Driver:
    • Since It is a using any GUI to test, your tests would run in background without any visual interruption
    • Compared to all and instances execution is faster
    • To run your tests through Htmlunit driver can also select other browser versions
    • It is platform independent and easier to run several tests concurrently. Ideal for Load testing.

Limitations:

    • It cannot emulate other browsers JavaScript behavior
Phantomjs

PHANTOMJS is a headless browser with JavaScript API. It is an optimal solution for Headless Website testing, Access and manipulate webpages & comes with the standard DOM A Pi.

In order to use the PHANTOMJS with Seleniun, one have to use Ghostdriver. Ghostdriver is a implementation of the Webdriver wire protocol in simple JS for PHANTOMJS.

The latest release of PHATOMJS has integrated ghostdriver and There are no need to separately install it.

Here's how the system works-

Steps to run Selenium with PHATOMJS

Step 1) You need Eclipse with Selenium installed

Step 2) Download PHANTOMJS Here

Step 3) Extract the downloaded folder to program Files

Step 4) Download the Phantomjs Driver from here. Add the jar to your project

Step 5) Paste the following code in eclipse

 Package Htmldriver;import java.io.file;import org.openqa.selenium.by;import org.openqa.selenium.WebDriver; Import Org.openqa.selenium.webelement;import Org.openqa.selenium.phantomjs.phantomjsdriver;public class Phantom { public static void Main (string[] args) {File file = new file ("C:/Program Files/phantomjs-2.0.0-windows/bin                    /phantomjs.exe ");                    System.setproperty ("Phantomjs.binary.path", File.getabsolutepath ());                    Webdriver Driver = new Phantomjsdriver ();                             Driver.get ("http://www.google.com");                    webelement element = Driver.findelement (By.name ("Q"));                    Element.sendkeys ("Guru99");                             Element.submit ();                    System.out.println ("page title is:" + driver.gettitle ());           Driver.quit (); }}

Step 6) Run the code. You'll observe the output is shown on console and no browser is launched.

Note: At the first run, based on your settings, the Get security warning from Windows to allow to run PHANTOMJS. Click on allow Access.

Many organization uses Phantom.js for various purpose, for example,

    • Headless Testing
    • Screen Capture
    • Page Automation
    • Network Monitoring
    • To-render dashboard screenshots for their users
    • To run Unit tests in command line
    • To generate employee handbooks from HTML to PDF
    • Combined with Qunit for the test suite

Summary

To test application rapidly in various browsers and without any visual interruption, headless browsertesting is used. Due to it speed, accuracy and easy to access features, HTML unit driver and PHANTOMJS is gaining popularity for headless Browser testing. By following some simple steps you get to know how easily these tools can be integrated with other tools and can execute T He test code.

[Selenium+java] Selenium with Htmlunit Driver & PHANTOMJS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.