[Selenium+Java] Selenium with HTMLUnit Driver & PhantomJS

摘要:
原始URL:https://www.guru99.com/selenium-with-htmlunit-driver-phantomjs.htmlHTMLUnitDriver&PhantomJSforSeleniumHeadlessTestingSeleniumWebdriverisawebautomationtoolwhichenablesyoutorunthetestsagains

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 you 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 will behave just like a browser but will not show any GUI.

Some of the examples of Headless Drivers include

  • HtmlUnit
  • Ghost
  • PhantomJS
  • ZombieJS
  • Watir-webdriver

In this tutorial we will focus on HtmlUnit and PhatomJS

HTMLUnitDriver

HTML UnitDriver is the most light weight and fastest implementation headless browser for of WebDriver. It is based on HtmlUnit. It is known as Headless Browser Driver. It is same as Chrome, IE, or FireFox driver, but it does not have 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 the 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 the submit button		
                    element.submit();			
                    
            		// This code will print the page title		
                    System.out.println("Page title is: " + driver.getTitle());		
                    
                    driver.quit();			
         }		
}

Step 2) Run the code. You will observer no browser is launched and results are shown in console.

HTMLUnitDriver & PhantomJS for Selenium Headless Testing

Benefits of Html Unit Driver:

  • Since it is not using any GUI to test, your tests will run in background without any visual interruption
  • Compared to all other instances execution is faster
  • To run your tests through HtmlUnit driver you 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 API.

In order to use PhantomJS with Seleniun, one has to use GhostDriver. GhostDriver is a implementation of Webdriver Wire protocol in simple JS for PhantomJS.

The latest release of PhatomJS has integrated GhostDriver and there is no need to separately install it.

Here is how the system works-

HTMLUnitDriver & PhantomJS for Selenium Headless Testing

Steps to run Selenium with PhatomJS

Step 1) You need Eclipse with Selenium installed

Step 2) Download PhantomJS here

HTMLUnitDriver & PhantomJS for Selenium Headless Testing

Step 3) Extract the downloaded folder to Program Files

HTMLUnitDriver & PhantomJS for Selenium Headless Testing

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

HTMLUnitDriver & PhantomJS for Selenium Headless Testing

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 will observe the output is shown in console and no browser is launched.

NOTE: At first run, based on your settings, you may get security warning from Windows to allow to run PhantomJS. Click on Allow Access.

HTMLUnitDriver & PhantomJS for Selenium Headless Testing

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 on 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 its speed, accuracy and easy to access features, HTML unit driver and PhantomJS are 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 the test code.

免责声明:文章转载自《[Selenium+Java] Selenium with HTMLUnit Driver & PhantomJS》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux系统扩容根目录磁盘空间的操作方法win10 oracle11g Navicat 连接记录下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

运行bat时隐藏cmd窗口

这种方法在击球时仍然闪烁。隐藏和运行蝙蝠文件的几种方法Bat文件是在Windows系统中以命令行模式执行一个或多个命令的批处理文件。然而,大多数脚本在运行时都会弹出黑色背景的DOS窗口,这会让许多用户无所适从,甚至会错误关闭正在运行的窗口。...

如何查看Oracle,MySQL,SQLServer三大数据库的事务隔离级别

1:mysql的事务隔离级别查看方法mysql最简单,执行这条语句就行:select@@tx_isolation详情:1.查看当前会话隔离级别select@@tx_isolation;2.查看系统当前隔离级别select@@global.tx_isolation;3.设置当前会话隔离级别setsessiontransactionisolatinlevelre...

凤凰系统(Phoenix OS)PC版安装,电脑上体验功能丰富的安卓系统

Tid=9074&fid=12安装前准备下载最新的PhoenixOSIO安装映像,并将其刻录到USB闪存驱动器;如果使用Windows操作系统,建议使用UltraISO刻录。下载地址:http://www.phoenixos.com/download_x86开始安装。1.重新启动机器,从启动选项中选择USB闪存磁盘启动,然后选择“Installati...

如何更改SQL Server2008默认数据库的存储路径

1.在安装SQlServer时,修改路径:当然,也可以修改共享函数目录和实例根目录。但是,我不知道共享函数目录和实例根目录是什么。...

scan chain的原理和实现——5.UDTP

UDTP(用户定义的测试点)指示DFTC在设计中用户指定的位置插入控制点和观察点。1.为什么使用UDTP?修复不可控的时钟和/或异步输入;增加设计的测试覆盖率;减少模式数量2.UDTP类型① 力0、力1、力01、力z0、力z1、力z01②控制_ 0...

java--枚举

前言:Java中的enum也是一种类类型,它与一般类的区别在于1.世界上只有一个实例2.不能有公共构造函数3。您不能继承和继承枚举事例publicenumHttpCode{SUCCESS(200,“操作成功”)。//定义的每个枚举项都等效于通过构造函数HttpCode(int code,Stringmessage)实例化没有枚举项的通用HttpCo...