からリンクを取得するためにセレンを使用していますが、それがダウンロードリンクであるかどうかを確認したいと思いました。
そのために、私はURLとURLConnectionで作成したこのコードを使用しました:
final WebElement element = driver.findElement(By.xpath(pathToFile));
URL url = null;
final String urlFileToDownload = element.getAttribute("href");
URLConnection myCon = null;
String contentDisposition = "";
try {
url = new URL(urlFileToDownload);
myCon = url.openConnection();
contentDisposition = myCon.getHeaderField("Content-Disposition");
if (!contentDisposition.contains("attachment;filename=")) {
assertTrue(false, "The link isn't a download link.");
}
} catch (final MalformedURLException e) {
throw new TestIntegrationException("Error while creating URL : " + e.getMessage());
} catch (final IOException e) {
throw new TestIntegrationException("Error while connecting to the URL : " + e.getMessage());
}
assertTrue(true, "Link is a download link.");
問題は、この写真でわかるように、私のリンクがダウンロードリンクであるということです:Image-link-download。(写真はコンソールのプリントスクリーンです)そして私が接続を開くとurl.openConnection();
myCon.getHeaderField("Content-Disposition")
無効である。
これを行う方法を検索しましたが、ヘッダーフィールドが空で、コンソールで確認したときにヘッダーフィールドが空ではないため、問題を見つけることができません...
編集:DockerサーバーでSeleniumテストを開始しています。これは、知っておくべき重要なポイントだと思います。
私の問題は、とは異なる私のセッションが原因でしたurl.openConnection()
。
この問題を修正するために、次のようなセレンを使用してCookieJSESSIONを収集しました。
String cookieTarget = null;
for (final Cookie cookie : this.kSupTestCase.getDriver().manage().getCookies()) {
if (StringUtils.equalsIgnoreCase(cookie.getName(), "JSESSIONID")) {
cookieTarget = cookie.getName() + "=" + cookie.getValue();
break;
}
}
次に、開いた接続にCookieを配置しました:
try {
url = new URL(urlFichierATelecharger);
myCon = url.openConnection();
myCon.setRequestProperty("Cookie", cookieCible);
contentDisposition = myCon.getHeaderField("Content-Disposition");
if (!contentDisposition.contains("attachment;filename=")) {
assertTrue(false, "The link isn't a download link.");
}
} catch [...]
そのように私は良いセッションを持っており、私のURLはダウンロードリンクとして認識されました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加