2012-11-17 203 views
2

我使用硒的webdriver自动化一个应用,下面是我的代码工作正常下载PDF文件

输入代码在这里:

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(); 

当下载链接硒点击会出现一个弹出窗口其中包含两个单选按钮默认情况下,单选按钮被点击打开与选项现在我需要将该单选按钮切换到另存为选项,然后单击确定按钮。 当我点击确定按钮时,PDF文件应保存在一些特定的本地驱动器。 为此,我使用了下面的代码,但它不工作。

//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); 
} 

所以,请帮我在此提供代码, 我有一个疑问,下载的PDF文件是否可以打开和读取线线可以比较目前存在的一些文本或没有,这可能吗? ?

+0

为什么不使用这个servlet? –

回答

1

在firefox中,您可以通过将以下代码添加到您的硒测试的setUp方法来解决此问题。

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

如果您有其他类型的要下载其他的PDF文件相比,你应该查找MIME类型的任何文档您要下载并将其添加到逗号分隔的列表文件。

2

简短的回答:This has been asked many times, please search.

长期和更正确的答案:截至目前(2012/11),它不能被通过的webdriver完成。 It's one of the most requested features for the Selenium project.你可以试试这些东西:

  1. 请使用HttpURLConnectionApache HttpComponents指定的链接的请求。你甚至可以用这种方式下载文件,虽然通常的做法是断言200 OK响应来确保文件可以被下载(因为当你测试你的应用程序时你通常不需要这个文件)。
  2. Snatch the file using any Java approach.this tool由某人与硒一起使用。
  3. 使用Robot类简单地按向下箭头输入什么的。但要小心,这只适用于您特定的浏览器和操作系统。它会在任何其他配置上崩溃。