Software Testing Material

A site for software testers. We provide free online tutorials on Manual Testing, Automation Testing - Selenium, QTP, LoadRunner, Testing Tools and many more.

  • Blog
  • Tutorials
    • Manual Testing
    • Java
    • Selenium
      • TestNG
      • Maven
      • Jenkins
    • Framework
    • Katalon
    • Agile
    • SQL
    • VBScript
    • API Testing
  • Tools
    • TestLodge
    • FrogLogic GUI Tool
    • CrossBrowserTesting
    • BrowserStack
    • TestCaseLab
    • Kobiton
    • Sikuli
    • Postman
  • Interview Q & A
    • Selenium
    • TestNG
    • Test Framework
    • Explain Framework
    • Manual Testing
    • Software QA
    • Agile
    • Testing As A Career
    • General Interview Questions
    • API Testing
    • SOAP
  • Free Resources
  • Guest Post
    • Guest Post Guidelines
  • Training

Headless Browser Testing Using PhantomJSDriver in Selenium WebDriver

Last Updated on September 1, 2017 by Rajkumar

Headless Browser Testing Using PhantomJSDriver In Selenium WebDriver

In the earlier post, we have seen some brief explanation on what is headless browser and in this post, we learn how to perform headless browser testing using PhantomJSDriver in Selenium WebDriver. PhantomJSDriver is one of the drivers of Selenium WebDriver. HtmlUnitDriver and PhantomJSDriver are most popular headless browsers.

PhantomJS is used for Headless Browser Testing of Web Applications that comes with in-built GhostDriver. This means that the rendered web pages are never actually displayed.

Advantages:

  1. Lightweight
  2. Proxy server support
  3. Fastest implementation of WebDriver
  4. Platform Independent
  5. Allows us to simulate different browsers having different versions.

You could download PhantomJSDriver executable file from here – PhantomJS Driver

Download PhantomJSDriver jar file from here – PhantomJS Jars

Usually, to run Selenium Scripts using Firefox, we initialize the Firefox driver.

1
WebDriver driver = new FirefoxDriver();

Whereas while running Selenium Scripts using PhantomJSDriver you need to write the below statement

1
WebDriver driver = new PhantomJSDriver ();

Remaining statements in your script will be as usual.

Let’s see a working example using PhantomJSDriver in Selenium WebDriver:

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
32
33
34
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.testng.annotations.Test;
 
public class HeadlessBrowser {
@Test
public void phantomJSDriver() throws Exception{
//Set the path of the phantomjs.exe file in the properties
System.setProperty("phantomjs.binary.path", "D:\\Selenium\\Drivers\\phantomjs.exe");
// To declare and initialize PhantomJSDriver
WebDriver driver = new PhantomJSDriver();
// Download link is http://phantomjs.org/download.html
// Set implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Open "Google.com and search SoftwareTestingMaterial.com"
driver.get("https://www.google.com");
// To locate the searchbox using its name
WebElement element = driver.findElement(By.name("q"));
// To enter text "softwaretestingmaterial.com"
element.sendKeys("softwaretestingmaterial.com");
// To submit
element.submit();
//Click on Software Testing Material link
driver.findElement(By.linkText("Software Testing Material")).click();
// Get the title of the site and store it in the variable Title
String Title = driver.getTitle();
// Print the title
System.out.println("I am at " +Title);
}
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
I am at Software Testing Material - A Site for Software Testers
PASSED: phantomJSDriver
 
===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================
 
 
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Different Browser Versions using PhanthomJSDriver:

We could test our scripts on different browser versions using PhanthomJSDriver. Yes, it allows us to choose our preferred browser version. See the below screenshot.

1
WebDriver driver = new PhanthomJSDriver(BrowserVersion.FIREFOX_3);

I am concluding this post about headless browser testing using PhanthomJSDriver. Check out our other post on how to perform headless browser testing using HtmlUnitDriver in Selenium WebDriver.

As always, feel free to share this post with your friends and colleagues on Facebook, Twitter, and Google Plus!

SUBSCRIBE TO GET FREE EBOOK AND REGULAR UPDATES ON SOFTWARE TESTING

Filed Under: Selenium

data-matched-content-rows-num="2" data-matched-content-columns-num="3"
Previous Article:
Headless Browser Testing Using HtmlUnitDriver in Selenium WebDriver
Next Article:
TestLodge Tutorial – TestLodge Test Management Tool Tutorial

About the Author

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Comments

  1. Prajwal says

    September 1, 2017 at 5:18 pm

    What is the difference between HTML unit driver and phantomjsdriver

    • Rajkumar says

      September 1, 2017 at 5:41 pm

      Hi Prajwal, Both HtmlUnitDriver and PhanthomJSDrivr are headless browsers. HtmlUnit Driver uses Rhino JavaScript engine and PhanthomJS uses Webkit javascript engine. We could use any of these two for headless browser testing. There are some other headless browsers but these two are popular ones. HtmlUnitDriver is much faster than PhanthomJSDriver.

ADVERTISEMENT

Froglogic-Squish-GUI-Tester
CrossBrowserTesting

TUTORIALS

  • Manual Testing Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • VBScript Tutorial
  • Agile Methodology
  • SQL Tutorial for Testers
  • INTERVIEW QUESTIONS

  • Framework Interview Questions
  • Real Time Manual Testing
  • 100 Top Selenium Interview Questions
  • 30 Top TestNG Interview Questions
  • Agile Interview Questions
  • 80 SQL Interview Questions
  • TESTING TOOLS

  • Test Lodge
  • FrogLogic Squish
  • Cross Browser Testing
  • BrowserStack
  • Test Case Lab
  • Sikuli
  • © 2019 www.softwaretestingmaterial.com | About us | Privacy Policy | Contact us | Guest Post | Disclaimer | Terms of use | Sitemap