我正在測試Web應用程序,其中一種情況是將文件從文件系統中拖出並將其放到組件上的能力。使用Selenium webdriver從文件系統拖放文件
與此類似:
http://s3u.github.com/har-view/
http://html5demos.com/dnd-upload
有沒有一種方法來模擬下降從文件系統中的文件到 使用的網絡驅動器的元素?
我正在測試Web應用程序,其中一種情況是將文件從文件系統中拖出並將其放到組件上的能力。使用Selenium webdriver從文件系統拖放文件
與此類似:
http://s3u.github.com/har-view/
http://html5demos.com/dnd-upload
有沒有一種方法來模擬下降從文件系統中的文件到 使用的網絡驅動器的元素?
不,不幸的是,WebDriver
只能驅動瀏覽器,並且無法觸摸任何東西。如果您想拖放某些東西,則必須使用Robot
或任何其他工具(there might be a library for this)。
,幫助我在運行JS代碼,我在Selenium: Drag and Drop from file system to webdriver?
只要複製粘貼整個事情中找到你的項目的唯一的事。
參見: 靜態無效DropFile(IWebElement目標,串文件路徑,INT OFFSETX = 0,INT OFFSETY = 0){
if (String.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentNullException("filePath");
}
if (!File.Exists(filePath))
{
throw new FileNotFoundException(filePath);
}
IWebDriver driver = ((RemoteWebElement)target).WrappedDriver;
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
string JS_DROP_FILE = @"
var target = arguments[0],
offsetX = arguments[1],
offsetY = arguments[2],
document = target.ownerDocument || document,
window = document.defaultView || window;
var input = document.createElement('INPUT');
input.type = 'file';
input.style.display = 'none';
input.onchange = function() {
target.scrollIntoView(true);
var rect = target.getBoundingClientRect(),
x = rect.left + (offsetX || (rect.width >> 1)),
y = rect.top + (offsetY || (rect.height >> 1)),
dataTransfer = { files: this.files };
['dragenter', 'dragover', 'drop'].forEach(function (name) {
var evt = document.createEvent('MouseEvent');
evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);
evt.dataTransfer = dataTransfer;
target.dispatchEvent(evt);
});
setTimeout(function() { document.body.removeChild(input); }, 25);
};
document.body.appendChild(input);
return input;
";
IWebElement input = (IWebElement)jse.ExecuteScript(JS_DROP_FILE, target, offsetX, offsetY);
input.SendKeys(filePath);
wait.Until(ExpectedConditions.StalenessOf(input));
您可以添加更多的內容到您的文章?如果您提供的鏈接發生變化,您的回答將不再有用。 – dckuehn