c# - Selenium:从文件系统拖放到 WebDriver?

标签 c# selenium ui-automation

我必须测试一个包含拖放区域的网络应用程序,用于从本地文件系统上传文件。我的测试环境基于 C#。

对于自动化测试,我使用了 Selenium,但无法从文件系统中拖动文件。上传区域是一个div 标签(没有input 标签)。那么最好的方法是什么? AutoIt(是否可以放入网络浏览器)?西库里?

最佳答案

单独使用 Selenium 是可能的,但这并不简单。它需要在页面中注入(inject)一个新的INPUT 元素以通过SendKeys 接收文件。然后,脚本需要通过向目标区域发送 dragenterdragoverdrop 事件来模拟掉落。

static void Main(string[] args)
{
    var driver = new ChromeDriver();
    driver.Url = "https://react-dropzone.js.org/";

    IWebElement droparea = driver.FindElementByCssSelector("[data-preview='Basic example'] [style]");
    DropFile(droparea, @"C:\Users\florent\Desktop\capture.png");

    driver.Quit();
}

const string JS_DROP_FILE = "for(var b=arguments[0],k=arguments[1],l=arguments[2],c=b.ownerDocument,m=0;;){var e=b.getBoundingClientRect(),g=e.left+(k||e.width/2),h=e.top+(l||e.height/2),f=c.elementFromPoint(g,h);if(f&&b.contains(f))break;if(1<++m)throw b=Error('Element not interractable'),b.code=15,b;b.scrollIntoView({behavior:'instant',block:'center',inline:'center'})}var a=c.createElement('INPUT');a.setAttribute('type','file');a.setAttribute('style','position:fixed;z-index:2147483647;left:0;top:0;');a.onchange=function(){var b={effectAllowed:'all',dropEffect:'none',types:['Files'],files:this.files,setData:function(){},getData:function(){},clearData:function(){},setDragImage:function(){}};window.DataTransferItemList&&(b.items=Object.setPrototypeOf([Object.setPrototypeOf({kind:'file',type:this.files[0].type,file:this.files[0],getAsFile:function(){return this.file},getAsString:function(b){var a=new FileReader;a.onload=function(a){b(a.target.result)};a.readAsText(this.file)}},DataTransferItem.prototype)],DataTransferItemList.prototype));Object.setPrototypeOf(b,DataTransfer.prototype);['dragenter','dragover','drop'].forEach(function(a){var d=c.createEvent('DragEvent');d.initMouseEvent(a,!0,!0,c.defaultView,0,0,0,g,h,!1,!1,!1,!1,0,null);Object.setPrototypeOf(d,null);d.dataTransfer=b;Object.setPrototypeOf(d,DragEvent.prototype);f.dispatchEvent(d)});a.parentElement.removeChild(a)};c.documentElement.appendChild(a);a.getBoundingClientRect();return a;";

static void DropFile(IWebElement target, string filePath, double offsetX = 0, double offsetY = 0)
{
    if (!File.Exists(filePath))
        throw new FileNotFoundException(filePath);

    IWebDriver driver = ((RemoteWebElement)target).WrappedDriver;
    IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;

    IWebElement input = (IWebElement)jse.ExecuteScript(JS_DROP_FILE, target, offsetX, offsetY);
    input.SendKeys(filePath);
}

来源:https://gist.github.com/florentbr/349b1ab024ca9f3de56e6bf8af2ac69e

关于c# - Selenium:从文件系统拖放到 WebDriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829153/

相关文章:

python-3.x - python : How can I press arrow keys randomly selenium

c# - 有没有办法在 Unity 中进行自动化 UI 测试?

automation - Microsoft UI 自动化框架是否适用于 Chrome、Python 和 Java 应用程序?

c# - 为什么重写文本框的 Text 属性会使 Visual Studio 崩溃?

c# - 使用 Selenium Webdriver 测试元素是否聚焦

java - 尝试与 Bamboo 一起运行时,Selenium 测试挂起

比较两个值并在 Cypress 中做出决定

c# - 为什么输入 "System.Byte[]"却出现 "byte[]"?

c# - 如何捕获 EF 异常

c# - ADO.NET CommandBuilder、InsertCommand 和默认约束