2013-06-27 32 views
3

我有一個Selenium(Java)測試應用程序,它可以在瀏覽器中打開pdf文檔並將其保存到硬盤中,以便稍後在測試中使用。由於測試將在多臺機器上使用,因此手動設置配置文件不是一種選擇;它必須以編程方式完成。使用Selenium在Chrome瀏覽器中保存文檔

在Firefox中我設置的配置偏好:

FirefoxProfile profile = new FirefoxProfile(); 
    profile.setPreference("browser.download.manager.showWhenStarting", false); 
    profile.setPreference("browser.download.dir", appSet.getDocumentSavePath()); 
    profile.setPreference("browser.download.folderList", 2); 
    profile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf"); 
    profile.setPreference("pref.downloads.disable_button.edit_actions", true); 
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf"); 
    profile.setPreference("pdfjs.disabled", true); 

是否有Chrome的相同呢?

我知道在Chrome中可以設置開關的功能,但我沒有看到任何可以幫助我的功能。

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); 
capabilities.setCapability("chrome.binary", appSet.getChromeBinaryLocation()); 
capabilities.setCapability("chrome.switches", Arrays.asList("--allow-running-insecure- content=true")); 

回答

4

我希望這對某人有用。該解決方案可跨瀏覽器運行,並且在啓動瀏覽器時不依賴於瀏覽器中的配置文件或首選項。

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Set; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.CookieStore; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.params.ClientPNames; 
import org.apache.http.client.params.CookiePolicy; 
import org.apache.http.client.protocol.ClientContext; 
import org.apache.http.cookie.ClientCookie; 
import org.apache.http.impl.client.BasicCookieStore; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.impl.cookie.BasicClientCookie2; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext; 
import org.openqa.selenium.WebDriver; 

public void downloadDocument(String documentName) { 
    HttpClient httpClient = new DefaultHttpClient(); 
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); 

    CookieStore cookieStore = new BasicCookieStore(); 
    Set<org.openqa.selenium.Cookie> cookies = driver.manage().getCookies(); 

    for (org.openqa.selenium.Cookie cookie: cookies) { 
     BasicClientCookie2 cookie2 = new BasicClientCookie2(cookie.getName(), cookie.getValue()); 
     cookie2.setAttribute(ClientCookie.VERSION_ATTR, "1"); 
     cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain()); 
     cookie2.setDomain(cookie.getDomain()); 
     cookie2.setPath(cookie.getPath()); 
     cookie2.setExpiryDate(cookie.getExpiry()); 
     cookieStore.addCookie(cookie2); 
    } 

    HttpContext httpContext = new BasicHttpContext(); 
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

    try { 
     HttpResponse httpResponse = httpClient.execute(new HttpGet(driver.getCurrentUrl()), httpContext); 
     InputStream inputStream = httpResponse.getEntity().getContent(); 

     FileOutputStream fos = new FileOutputStream (getDocumentSavePath() + documentName.replaceAll("\\s+", "") + ".pdf"); 

     byte[] buffer = new byte[2048]; 
     int bytesRead = 0; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      fos.write(buffer, 0, bytesRead); 
     } 

     fos.close(); 
    } catch (ClientProtocolException e) { logger.error("message and trace") 
    } catch (IOException e) { logger.error("message and trace") 
    } 

    httpClient.getConnectionManager().shutdown(); 

} 
+0

謝謝,這對我的用例非常有用! –

+0

這可以擴展到其他類型的文件,比如Excel嗎?此外,將不勝感激代碼中的評論,以更好地理解它。 – Ziska

+0

這是我能找到的唯一例子,它實際上用於從屏幕獲取PDF並將其寫入文件。即使在Selenium之外的直接Java代碼中,我們也無法使它適用於嵌入式pdf.stream。當然,2017年以及許多進口產品已被棄用,但仍然有效。 – Miek

相關問題