c# - Selenium: Drag and Drop from file system to webdriver? -


i have test web-application contains drag , drop area uploading files local file system. test environment based on c#.

for automation testing have used selenium, not possible drag files file system. upload area div tag (no input tag). what's best way it? autoit (is possible drop in web browser)? sikuli?

it's possible selenium alone, it's not simple. requires inject new input element in page receive file through sendkeys. then, script needs simulate drop sending dragenter, dragover, drop events targeted area.

iwebelement droparea = driver.findelement(by.id("droparea")); dropfile(droparea, @"c:\...\image.png"); 
static void dropfile(iwebelement target, string filepath, int offsetx = 0, int offsety = 0) {     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)); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -