javaseleniumphantomjsghostdriver

Screenshot not working with phantomJS and ghostdriver


I am trying to take screenshot of a page with phantomJS and ghostdriver.

Here is my code:

import java.io.File;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import net.anthavio.phanbedder.Phanbedder;

public class TakeScreenshot1 {
    public static File phantomjs = Phanbedder.unpack(); //Phanbedder to the rescue!

        public static void main(String args[]) throws Exception {
            DesiredCapabilities dcaps = new DesiredCapabilities();
            dcaps.setCapability("takesScreenshot", true);
            dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());

            PhantomJSDriver driver = new PhantomJSDriver(dcaps);
            driver.get("https://www.google.co.in");
            Thread.sleep(20000);

            WebElement element = driver.findElement(By.xpath("//*[@id=\"hplogo\"]"));
            System.out.println("Element is: "+ element.getText());

            System.out.println("Taking screenshot now");
            driver = (PhantomJSDriver) new Augmenter().augment(driver);
            File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            System.out.println("File:" + srcFile);
            FileUtils.copyFile(srcFile, new File("D:\\Java\\screenshot_.png"));
            System.out.println("Done");
        }
    }

I am getting following exceptions in logs:

PhantomJS is launching GhostDriver...
[INFO  - 2015-11-03T17:46:30.119Z] GhostDriver - Main - running on port 14164
[INFO  - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - _decorateNewWindow - page.settings: {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.2 Safari/534.34","webSecurityEnabled":true}
[INFO  - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - page.customHeaders:  - {}
[INFO  - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - CONSTRUCTOR - Desired Capabilities: {"phantomjs.binary.path":"C:\\Users\\AMANDE~1.SIN\\AppData\\Local\\Temp\\phantomjs-1.9.2\\phantomjs.exe","takesScreenshot":true}
[INFO  - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.2","driverName":"ghostdriver","driverVersion":"1.0.4","platform":"windows-7-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO  - 2015-11-03T17:46:31.285Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: d1291d90-8252-11e5-9fb4-dbd19cd6862b
Element is: India
Taking screenshot now
Exception in thread "main" net.sf.cglib.core.CodeGenerationException: java.lang.IllegalStateException-->The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:220)
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:216)
    at net.sf.cglib.proxy.Enhancer.createUsingReflection(Enhancer.java:640)
    at net.sf.cglib.proxy.Enhancer.firstInstance(Enhancer.java:538)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:225)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    at org.openqa.selenium.remote.Augmenter.performAugmentation(Augmenter.java:140)
    at org.openqa.selenium.remote.Augmenter.create(Augmenter.java:53)
    at org.openqa.selenium.remote.BaseAugmenter.augment(BaseAugmenter.java:114)
    at com.snapdeal.hadoop.monitoring.Hadoop_Monitoring_1.TakeScreenshot1.main(TakeScreenshot1.java:36)
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:237)
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:182)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:99)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:89)
    at org.openqa.selenium.phantomjs.PhantomJSDriver$$EnhancerByCGLIB$$c0768800.<init>(<generated>)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:228)
    ... 11 more

I am able to get the element data but while taking screenshot, error is coming.

Please help


Solution

  • I debugged and solved the problem. After removing the Augmenter from the code. It worked fine