Excelシートをダウンロードする必要があるテストを自動化する必要があります。ファイルダイアログがOKとキャンセルボタンで画面に表示され、 excel sheet ..私はオートメーション言語としてJavaを使用しています。私のオペレーティングシステムはLinuxです。このケースを自動化する方法を提案してください。別のフォーラムでも検索し、AutoItをWindowsベースコンポーネントのスクリプト言語として使用しています。しかし、ここで私はLinuxを使っているので、AutoItは私の場合には動かないでしょう。Linux上でSelenium IDEを使用してファイルダイアログをダウンロードするには
答えて
セレンとのダイアログはセレンがダイアログとやりとりすることができないため、本当に痛いです。一言で言えば、特定のMIMEタイプのファイルをダウンロードするときにユーザーにプロンプトが表示されないカスタムFirefoxプロファイルを作成すると、指定したフォルダにファイルが自動的にダウンロードされます。セレンにどのプロファイルを使用すべきかを伝える必要があります。あなたはセレンが匿名のプロファイルでfirefoxを起動しない場合。残念ながら、正確な手順は、firefoxとseleniumのバージョンによって異なります。私はこれらのリンクが役立つことを願って:すべての
Link to my blog where I discuss this in more detail.
まず、なぜあなたがファイルをダウンロードしたいのですか?あなたはそれで何かをするつもりですか?
ファイルをダウンロードしたい人の大半は、オートメーションフレームワークを表示することができるので、ファイルをダウンロードすることができます。
ヘッダーの応答を確認して、200 OK(またはリダイレクト、予想される結果に応じて)を確認し、ファイルが存在することを通知できます。
ファイルをダウンロードするのは、テスト時間、ネットワーク帯域幅、ディスク容量を浪費しているため、実際にファイルをダウンロードする場合のみです。
上記にもかかわらずファイルをダウンロードしたい場合は、Selenium IDEを使用せず、代わりにWebDriver APIを使用することをお勧めします。これは、ページ上のリンクを見つけ、にリンクされたURLを抽出
:
は、ここでJavaを使用して、私の実装です。次に、apacheコモンを使用して、セレンによって使用されるブラウザセッションを複製し、ファイルをダウンロードします。 (ページ上のリンクがダウンロードファイルに実際にはリンクしていないが、自動ファイルダウンロードを防ぐためのレイヤーにリンクしている)場合があります。
一般的にはうまく動作し、クロスプラットフォーム/クロスブラウザの互換性があります。
コードは次のとおりです。
/*
* Copyright (c) 2010-2011 Ardesco Solutions - http://www.ardescosolutions.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lazerycode.ebselen.customhandlers;
import com.google.common.annotations.Beta;
import com.lazerycode.ebselen.EbselenCore;
import com.lazerycode.ebselen.handlers.FileHandler;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.*;
import java.net.URL;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Beta
public class FileDownloader {
private static final Logger LOGGER = LoggerFactory.getLogger(EbselenCore.class);
private WebDriver driver;
private String downloadPath = System.getProperty("java.io.tmpdir");
public FileDownloader(WebDriver driverObject) {
this.driver = driverObject;
}
/**
* Get the current location that files will be downloaded to.
*
* @return The filepath that the file will be downloaded to.
*/
public String getDownloadPath() {
return this.downloadPath;
}
/**
* Set the path that files will be downloaded to.
*
* @param filePath The filepath that the file will be downloaded to.
*/
public void setDownloadPath(String filePath) {
this.downloadPath = filePath;
}
/**
* Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie state
*
* @param seleniumCookieSet
* @return
*/
private HttpState mimicCookieState(Set<org.openqa.selenium.Cookie> seleniumCookieSet) {
HttpState mimicWebDriverCookieState = new HttpState();
for (org.openqa.selenium.Cookie seleniumCookie : seleniumCookieSet) {
Cookie httpClientCookie = new Cookie(seleniumCookie.getDomain(), seleniumCookie.getName(), seleniumCookie.getValue(), seleniumCookie.getPath(), seleniumCookie.getExpiry(), seleniumCookie.isSecure());
mimicWebDriverCookieState.addCookie(httpClientCookie);
}
return mimicWebDriverCookieState;
}
/**
* Mimic the WebDriver host configuration
*
* @param hostURL
* @return
*/
private HostConfiguration mimicHostConfiguration(String hostURL, int hostPort) {
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(hostURL, hostPort);
return hostConfig;
}
public String fileDownloader(WebElement element) throws Exception {
return downloader(element, "href");
}
public String imageDownloader(WebElement element) throws Exception {
return downloader(element, "src");
}
public String downloader(WebElement element, String attribute) throws Exception {
//Assuming that getAttribute does some magic to return a fully qualified URL
String downloadLocation = element.getAttribute(attribute);
if (downloadLocation.trim().equals("")) {
throw new Exception("The element you have specified does not link to anything!");
}
URL downloadURL = new URL(downloadLocation);
HttpClient client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort()));
client.setState(mimicCookieState(driver.manage().getCookies()));
HttpMethod getRequest = new GetMethod(downloadURL.getPath());
FileHandler downloadedFile = new FileHandler(downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true);
try {
int status = client.executeMethod(getRequest);
LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm());
BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream());
int offset = 0;
int len = 4096;
int bytes = 0;
byte[] block = new byte[len];
while ((bytes = in.read(block, offset, len)) > -1) {
downloadedFile.getWritableFileOutputStream().write(block, 0, bytes);
}
downloadedFile.close();
in.close();
LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile());
} catch (Exception Ex) {
LOGGER.error("Download failed: {}", Ex);
throw new Exception("Download failed!");
} finally {
getRequest.releaseConnection();
}
return downloadedFile.getAbsoluteFile();
}
}
私はむしろ制限の設定でこの問題を解決しなければならなかった:タスクが生成されたPDFの内容を試験することであった、それがあった。いずれかを介して
- 利用できません直接URLまたはFTPですが、jQueryリクエストでブラウザに直接ストリームします。
- オープン/保存ダイアログを強制するHTTPヘッダー。 (私はシステムの対話、粗いのを意味します)
- お尻の最後の痛みは、ターゲットブラウザIE8です。 したがって、上記の解決策のどれも私には当てはまりませんでした。
私が見つけた解決策はほとんどありません。ダイアログをナビゲートし、キーボードを使用してウィンドウを開く可能性があります。私は
- をヒットする必要があるキーのシーケンスを指摘
- ダウンロードしたファイル= Alt + F4キー
- スイッチフォーカス= Alt + Tab
を閉じて私もシーケンスCTRL + A + CTRL + Cを経由して、文書の文字列の内容をコピーしてを介してシステムクリップボードからそれを読むことができますjava.awt.Toolkit.getDefaultToolkit()。getSystemClipboard();
確かにこのシーケンスは環境とブラウザによって異なりますが、「インテリジェント」にする必要があります。
私は2つの事実を中継します: 1. Seleniumは常にlocalhost上で動作しますので、FileInputStream経由で保存したファイルを開き、私が望むものを何でも実行できます。 MD5を生成し、バイナリを比較し、専用のライブラリで開くか、手動でテストするためにそのまま使用します。 2.どのウィンドウにフォーカスを当てるか予測できます。 (ダウンロードダイアログが開くと、filePathFieldにフォーカスが入るので、パスを入力することができます)
- 1. PHPUnitを使用してSelenium RCでSelenium IDEスクリプトを実行するには?
- 2. selenium ideでif/elseを使用する
- 3. ファイルダイアログをダウンロードしていますか?
- 4. selenium python webdriverを使用してPDFファイルをダウンロードするには?
- 5. Selenium IDEでTestNGを使用するには?
- 6. xpathとselenium ideを使用してリンクをクリックする方法
- 7. Selenium IDEを使用したオートメーション
- 8. Firefox 10でSelenium IDEを使用していますか?
- 9. VB 6で開いているファイルダイアログを使用するには?
- 10. Seleniumを使用してIEでファイルをダウンロードします。
- 11. ファイルダイアログを使用してパスを取得
- 12. Java(Eclipse IDE)を使用しているSelenium Webdriver
- 13. ネットワーク上でcloud9-ideを使用する
- 14. Selenium IDEを使用してコーディングなしでドロップダウンを選択する方法
- 15. Linuxでmod_xsendfileを使用して大容量ファイルをダウンロードする
- 16. Python Seleniumを使用してzipファイルをダウンロードする
- 17. Selenium IDE 2.9.1を使用してファイルをアップロード
- 18. Selenium IDEを使用して無効なラジオボタンを識別する方法は?
- 19. Selenium IDEでのワイルドカードの使用
- 20. Selenium IDE testscript to Selenium IDE
- 21. Selenium IDEを使用してテキストを整数に変換する方法
- 22. Selenium IDEを使用してダイナミックページロードを必要とするものをテストする
- 23. Selenium IDEで後で使用するための値を保存
- 24. PHPを使用してSelenium WebDriver上でファイルをアップロードする
- 25. Selenium IDE:ワイルドカードを使用してテキストを検索するか、エスケープする方法 '
- 26. オンラインIDE上でPythonでGUIを使用していますか?
- 27. Selenium IDEのテキストをクリックするには?
- 28. EclipseのIDEを使用してLinuxでCアプリケーションをデバッグする方法
- 29. Eclipse IDEを使用してSelenium Javaでテストケースを実行するMTMを使用できます
- 30. Linux上のSelenium Chrome Driver