July 23, 2018

Sample java program for PhantomJS

PhantomJs Driver Executable

Download PhantomJs driver here

Sample Java Program:

  • 
    package com.test.phantomjs;
    
    import org.openqa.selenium.By;
    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.DesiredCapabilities;
    
    public class Test {
    
    public static void main(String[] args) throws InterruptedException {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setJavascriptEnabled(true);
    caps.setCapability("takesScreenshot", true);
    caps.setCapability(
    PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
    "/Users/yosuvaarulanthu/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs"
    );
    WebDriver driver = new PhantomJSDriver(caps);
    
    String baseUrl = "http://demo.guru99.com/test/upload/";
    
    driver.get(baseUrl);
    WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
    
    // enter the file path onto the file-selection input field
    uploadElement.sendKeys("/Users/yosuvaarulanthu/Downloads/sample.txt");
    
    // check the "I accept the terms of service" check box
    driver.findElement(By.id("terms")).click();
    
    // click the "UploadFile" button
    driver.findElement(By.name("send")).click();
    System.out.println("done"+driver.getTitle());
    Thread.sleep(3000);
    System.out.println("Succes Msg:"+driver.findElement(By.xpath("//h3/center")).getText());
    driver.quit();
    }
    
    }
    
    

POM.xml
  • <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>PhantomJS</groupId>
      <artifactId>PhantomJS</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <dependencies>
      	<dependency>
        <groupId>com.github.detro.ghostdriver</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.1.0</version>
    	</dependency>
      </dependencies>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
              <release>10</release>
            </configuration>
          </plugin>
        </plugins>
      </build>
      
    </project>
    

0 Comments

Leave A Comment

Leave a Reply