Prabhakar
Prabhakar

Reputation: 25

Downloading a pdf file

I am automating a application using selenium webdriver,below is my codes which works fine

enter code here:

import java. util.concurrent. TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.JavascriptExecutor;

import com.thoughtworks.selenium.SeleneseTestCase;

public class MonTaxRep1 extends SeleneseTestCase{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get(" URL ");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement un= driver.findElement(By.name("username"));
un.sendKeys("clientremote");
driver.findElement(By.name("password")).sendKeys("12345678");
driver.findElement(By.name("submit")).click();
//  find the element and click on signin
//  driver.findElement(By.id("loginButton")).click();
driver.findElement(By.xpath("//a/span[contains(text(),'Thailand')]")).click();
driver.findElement(By.linkText("Williams Limited Thailand")).click();
driver.findElement(By.xpath("//map[@id='Map']/area[3]")).click();
driver.findElement(By.cssSelector("a > img")).click();
driver.findElement(By.xpath("//img[@onclick=\"showItem('_self')\"]")).click();
new Select(driver.findElement(By.name("pay_year"))).selectByVisibleText("2010");
new Select(driver.findElement(By.name("pay_month"))).selectByVisibleText("January");

driver.findElement(By.linkText("Monthly Tax Report")).click();
driver.findElement(By.name("g_title")).sendKeys("Test1");
driver.findElement(By.xpath("//input[@value='Download']")).click();

When selenium clicks on download link a pop-up window appears which contains two radio buttons by default the radio button is clicked on for open with option now i need to switch that radio button into save as option and click on OK button . When i click on OK button the the pdf file should be saved in some specific local drives . For this i have used the below code but it is not working.

//Before  opening pop-up get the main window handle 
String mainWindowHandle=driver.getWindowHandle();
//open the pop-up window(i.e click on element which causes open a new window)
driver.findElement(By.xpath("//input[@value='Download']")).click();

//Below code returns all window handles as set
Set s = driver.getWindowHandles();

Iterator ite = s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}

So please help me in providing code for this, I had a doubt whether the downloaded pdf file can be opened and read line by line and can compare some text present in that or not,is this possible ??

Upvotes: 2

Views: 3702

Answers (2)

Aaron Levenstein
Aaron Levenstein

Reputation: 395

In firefox you can fix this by adding the following code to the setUp method of your selenium tests.

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/x-pdf");

If you have other types of documents you want to download other than pdfs you should look up the MIME type of whatever document you are trying to download and adding it to the comma delimited list.

Upvotes: 1

Petr Janeček
Petr Janeček

Reputation: 38444

The short answer: This has been asked many times, please search.

The long and more correct answer: As of now (2012/11), it can't be done via WebDriver. It's one of the most requested features for the Selenium project. You can try one of these things:

  1. Make a request for the specified link using HttpURLConnection or Apache HttpComponents. You can even download the file this way, although the usual practice is just to assert a 200 OK response to make sure that the file can be downloaded (since you usually don't really need the file when you're testing your application).
  2. Snatch the file using any Java approach. Or this tool made by someone to be used with Selenium.
  3. Use the Robot class to simply press Down arrow and Enter or something. But beware, this will only work for your particular browser and OS. It will break on any other configuration.

Upvotes: 2

Related Questions