Headless Browser Testing using PhantomJS GhostDriver WebDriver
Headless Browser Testing using PhantomJS GhostDriver WebDriver
PhantomJS is a Headless Webkit with JavaScript API. It has fast & native support for various Web Standards: DOM handling, CSS selector, JSON, canvas and SVG. GhostDriver is a Webdriver wire protocol in simple JS for PhantomJS. PhantomJS is used for Headless Testing of Web Applications that comes with in-built GhostDriver.
Involves,
1| General command-line based testing.
2| As a part of a Continuous Integration System.
PhantomJS is not a Test framework, it is used only to LAUNCH the tests via a suitable Test Runner.
Framework used: WebDriver
Test Runner: GhostDriver
CI systems: Make sure PhantomJS is installed properly on the slave/build agent and it is ready to go. Headless Browser is a Web Browser without a GUI (Graphical User Interface). It access Web Pages but doesnt show them to any human being. Headless Browser should be able to parse JavaScript.
Configure PhantomJS
1. Download phantomjs.exe
2. Extract the phantomjs-1.8.x-windows.zip folder and locate phantomjs.exe file to C:/ folder
3. Add the following imports to your code:
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;import org.openqa.selenium.remote.DesiredCapabilities;
4. Replace the object, "driver" specifying "FirefoxDriver" with "PhantomJSDriver".
Replace the code,
WebDriver driver = new FirefoxDriver
with
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
5. Run Test.
Note|
PhantomJSDriver-1.0.x.jar can also be downloaded and configured in Eclipse manually.
PhantomJS | Screen Capture
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://phantomjs.exe");
caps.setCapability("takesScreenshot", true);
driver = new PhantomJSDriver(caps);
baseUrl = "http://www.xyz.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test01() throws Exception {
driver.get(baseUrl + "/");
long iStart = System.currentTimeMillis(); // start timing
<your script>
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:sample.jpeg"),true);
System.out.println("Single Page Time:" + (System.currentTimeMillis() - iStart)); // end timing
}
alternative link download