[Selenium+Java] Selenium with HTMLUnit Driver & PhantomJS

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
  • ExcellentJavaScriptsupport
  • 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.

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-

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 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.

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 browserTestingis 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.

原文地址:https://www.cnblogs.com/alicegu2009/p/9098667.html

时间: 2024-10-04 17:48:33

[Selenium+Java] Selenium with HTMLUnit Driver & PhantomJS的相关文章

selenium +java 多个公用driver问题

问题点:太久没有写selenium代码,居然把driver公用的问题忘记了,即:每写一个测试类,执行过程中都会新建一个窗口,这样应该说是非常不专业的. 大概想了一个方法,虽然看起来也不怎么专业,但感觉能用就很开心了. 解决步骤: 1 创建一个获取获取driver的方法getDriver() 2 创建成员变量,将 getDriver()赋值给成员变量 3 其他业务类在使用driver时,都可以通过父类.driver的方式,获取同一个driver public class Login{ privat

[Selenium+Java] Selenium Grid Tutorial: Command Line and JSON Example

Original URL: https://www.guru99.com/introduction-to-selenium-grid.html What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in para

[Selenium+Java] Selenium Framework: Keyword Driven & Hybrid

Original from: https://www.guru99.com/creating-keyword-hybrid-frameworks-with-selenium.html What is Selenium Framework? Selenium Framework is a code structure that helps to make code maintenance easy. Without frameworks, we will place the “code” as w

java+selenium/java+selenium RC 项目搭建连接整理

手把手__SELENIUM_JAVA搭建_总结汇总篇:http://wenku.baidu.com/link?url=AbZX_o7JoH9Qaz0Q1aSWvTNLzeOwQCaqf_n0wnvbYAXgF9qoYIY6Chhb0hU6m3_sOI8dZ5iwOKThv9R2QQrmxO7yFNDBzpTBZlRFynIDC2q selenium _RC _java环境配置:http://wenku.baidu.com/view/6641fd0f844769eae009ed93.html?re

软件测试之Selenium Java WebDriver

编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系 package selenium2; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import o

Selenium Java WebDriver 使用

一. Firefox安装Selenium插件 在FireFox的菜单中的附加组件中搜索Selenium IDE 然后安装 二. 使用Selenium IDE录制脚本/导出脚本 点击图中标志打开Selenium IDE 红色按钮按下表示正在录制,这时候只用将界面切换到Firefox,网址中输入www.baidu.com,然后再搜索框中输入文字,点击搜索,所有的控件的访问都会被记录下来,然后切换回seleniumIDE就可以看到已经录制完毕 然后在图中红色选中的区域可以调整重新执行的速度,蓝色选中区

java selenium (五) 元素定位大全

页面元素定位是自动化中最重要的事情, selenium Webdriver 提供了很多种元素定位的方法.  测试人员应该熟练掌握各种定位方法. 使用最简单,最稳定的定位方法. 阅读目录 自动化测试步骤 在自动化测试过程中, 测试程序通常的操作页面元素步骤 1. 找到Web的页面元素,并赋予到一个存储对象中 (WebElement) 2. 对存储页面元素的对象进行操作, 例如:点击链接,在输入框中输入字符等 3. 验证页面上的元素是否符合预期 通过这三个步骤, 我们可以完成一个页面元素的操作, 找

Selenium Web 自动化 - Selenium(Java)环境搭建

Selenium Web 自动化 - Selenium(Java)环境搭建 2016-07-29 第1章 Selenium环境搭建 1.1 下载JDK JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.2 安装和配置JDK 安装目录尽量不要有空格  D:\Java\jdk1.8.0_91; D:\Java\jre8 设置环境变量: “我的电脑”->右键->“

java+selenium环境搭建

这篇文章只是为了记录一下搭建环境,方便自己日后查看. 一.在eclipse中搭建maven 1.安装maven 将apache-maven-3.5.0解压到C盘根目录下 配置maven环境变   MAVEN_HOME : C:\apache-maven-3.3.9  MAVEN : %MAVEN_HOME%\bin  MAVEN_OPTS : -Xms256m -Xmx512m  在path最前面加上: %MAVEN%; 验证maven是否安装成功 Cmd->mvn -version 成功则出现