Wednesday, 18 December 2013

Detailed steps to implement ghost driver using phantomjs

1.To implement ghost driver need to create driver instance by passing capabilities to PhantomJsDriver class.
2.Before creating driver instance, in capabilities need to provide the path of the phantomjs driver using PhantomJSDriverService class

3.download PhantomJS

    OR, clone & build PhantomJS
      $> git clone https://github.com/ariya/phantomjs.git && cd phantomjs      
$> ./build.sh
$> ... ... go make some coffee ...
$> cd bin && ./phantomjs
$> export PATH=`pwd`:$PATH ... this is optional ...
(optional) clone GhostDriver (instead of embedded version)
$> git clone https://github.com/detro/ghostdriver.git

4.Bindings

<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>LATEST_VERSION_HERE</version>
</dependency>
... or ...
dependencies {
testCompile "com.github.detro.ghostdriver.phantomjsdriver.LATEST_VERSION_HERE"
}
Java
Python
$> [sudo] easy_install selenium or $> [sudo] pip install selenium
Ruby
$> gem install selenium-webdriver
.Net
PM> Install-Package Selenium.WebDriver

5. Capabilities

DesiredCapabilities caps = new DesiredCapabilities();
// If executable not added to $PATH
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/path/to/phantomjs");
// If don't want to use embedded GhostDriver
caps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY,
"/path/to/ghostdriver/src/main.js");
WebDriver driver = new PhantomJSDriver(caps);
Java
for Python / Ruby / .Net check documentation

6.Capabilities: Page Settings

DesiredCapabilities caps = new DesiredCapabilities();
// Change setting X to Y on every Web Page created during this WebDriver session
// NOTE: Can use PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX
caps.setCapability("phantomjs.page.settings.X", "Y");
...
Possible Settings (might change in the future)
page.settings => {
"XSSAuditingEnabled": false,
"javascriptCanCloseWindows": true,
"javascriptCanOpenWindows": true,
"javascriptEnabled": true,
"loadImages": true,
"localToRemoteUrlAccessEnabled": false,
"userAgent": "... AppleWebKit/534.34 ... PhantomJS/1.9.0 (development) ...",
"webSecurityEnabled": true
}

7.Then do the script as like normal driver

see the example code below:

public class GoogleGhostDriverEx {
     WebDriver driver;
        DesiredCapabilities caps;
     @Before
 public void beforeClass() {
          caps = new DesiredCapabilities();
         caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"D:\\Downloads-1\\phantomjs\\phantomjs-1.9.2-windows\\phantomjs.exe");                 
         driver = new PhantomJSDriver(caps);
     }
 
 @Test
 public void search() {
   
     try{
         driver.get("http://www.google.co.in");
       
         File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
       
         FileUtils.copyFile(srcFile, new File("test.png"));
         driver.findElement(By.name("q")).sendKeys("ghostdriver");
          srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
         FileUtils.copyFile(srcFile, new File("test2.png"));
       
       
            }catch(Exception e){
                e.printStackTrace();
            }
  }

 @After
 public void afterClass(){
     driver.quit();
  }
  }

No comments:

Post a Comment