Tuesday, 9 September 2014

How to download PDFfile using Selenium

package Spicejet;

import java.io.BufferedInputStream;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.util.PDFTextStripper;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;
import org.testng.Reporter;

public class FirstApplicationthree {

private WebDriver driver;

private String baseUrl;

private FirefoxProfile profile;

PDFParser parser;

URL url;
Alert alert;

Actions action;
WebElement privacy;
BufferedInputStream fileToParse;

String output;
@BeforeMethod

public void setUp() throws Exception {

//profile = new FirefoxProfile();

// profile.setEnableNativeEvents(true);

driver = new FirefoxDriver();

baseUrl = "http://www.spicejet.com/";

driver.manage().window().maximize();



driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


}

@Test

public void footer_links() throws Exception {

driver.get(baseUrl + "/");

try{

driver.findElement(By.linkText("Tariffs")).click();



driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Thread.sleep(1000);

driver.findElement(By.id("download")).click();

Reporter.log("I am  missed here");
        //get the current url

url = new URL(driver.getCurrentUrl());

//file to parse buffered IO
fileToParse=new BufferedInputStream(url.openStream());
         ///getting the PDF parser
parser = new PDFParser(fileToParse);
parser.parse();

output=new PDFTextStripper().getText(parser.getPDDocument());

System.out.println(output);

//alert = driver.switchTo().alert();

// System.out.println(alert.getText());

//alert.accept();

parser.getPDDocument().close();

}catch(Exception e){
System.out.println("parser exception"+e);
}

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Test

public void right_click() throws Exception {

driver.get(baseUrl + "/");

privacy = driver.findElement(By.linkText("Privacy Policy"));
action = new Actions(driver);

action.moveToElement(privacy);

action.contextClick(privacy).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build();


action.perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


}

@AfterMethod

public void tearUp() throws Exception {

driver.quit();
}


}

To know FAQ's in Interviews on Selenium  ClickHere
To know more on SQL Queries Required For Testers  ClickHere

No comments:

Post a Comment