Skip to content

Instantly share code, notes, and snippets.

@daominhdam
Forked from rcorreia/drag_and_drop_helper.js
Last active September 3, 2022 09:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save daominhdam/de0629d1048e9c9848ebb93d717769fd to your computer and use it in GitHub Desktop.
Save daominhdam/de0629d1048e9c9848ebb93d717769fd to your computer and use it in GitHub Desktop.
drag_and_drop_helper.js
(function( $ ) {
$.fn.simulateDragDrop = function(options) {
return this.each(function() {
new $.simulateDragDrop(this, options);
});
};
$.simulateDragDrop = function(elem, options) {
this.options = options;
this.simulateEvent(elem, options);
};
$.extend($.simulateDragDrop.prototype, {
simulateEvent: function(elem, options) {
/*Simulating drag start*/
var type = 'dragstart';
var event = this.createEvent(type);
this.dispatchEvent(elem, type, event);
/*Simulating drop*/
type = 'drop';
var dropEvent = this.createEvent(type, {});
dropEvent.dataTransfer = event.dataTransfer;
this.dispatchEvent($(options.dropTarget)[0], type, dropEvent);
/*Simulating drag end*/
type = 'dragend';
var dragEndEvent = this.createEvent(type, {});
dragEndEvent.dataTransfer = event.dataTransfer;
this.dispatchEvent(elem, type, dragEndEvent);
},
createEvent: function(type) {
var event = document.createEvent("CustomEvent");
event.initCustomEvent(type, true, true, null);
event.dataTransfer = {
data: {
},
setData: function(type, val){
this.data[type] = val;
},
getData: function(type){
return this.data[type];
}
};
return event;
},
dispatchEvent: function(elem, type, event) {
if(elem.dispatchEvent) {
elem.dispatchEvent(event);
}else if( elem.fireEvent ) {
elem.fireEvent("on"+type, event);
}
}
});
})(jQuery);
public String getContentFile(String filePath) throws IOException {
Charset cs = Charset.forName("UTF-8");
FileInputStream stream = new FileInputStream(filePath);
try {
Reader reader = new BufferedReader(new InputStreamReader(stream, cs));
StringBuilder builder = new StringBuilder();
char[] buffer = new char[8192];
int read;
while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
builder.append(buffer, 0, read);
}
return builder.toString();
} finally {
stream.close();
}
}
(function (jqueryUrl, callback) {
if (typeof jqueryUrl != 'string') {
jqueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
}
if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
var head = document.getElementsByTagName('head')[0];
var done = false;
script.onload = script.onreadystatechange = (function () {
if (!done && (!this.readyState || this.readyState == 'loaded'
|| this.readyState == 'complete')) {
done = true;
script.onload = script.onreadystatechange = null;
head.removeChild(script);
callback();
}
});
script.src = jqueryUrl;
head.appendChild(script);
}
else {
callback();
}
})(arguments[0], arguments[arguments.length - 1]);
@Test
public void TC_07_Drag_And_Drop_HTML5() throws InterruptedException, IOException {
driver.get("http://the-internet.herokuapp.com/drag_and_drop");
String sourceCss = "#column-a";
String targetCss = "#column-b";
String java_script = readFile(javascriptPath);
// Inject Jquery lib to site
// String jqueryscript = readFile(jqueryPath);
// javascriptExecutor.executeScript(jqueryscript);
// A to B
java_script = java_script + "$(\"" + sourceCss + "\").simulateDragDrop({ dropTarget: \"" + targetCss + "\"});";
javascriptExecutor.executeScript(java_script);
Thread.sleep(3000);
Assert.assertTrue(isElementDisplayed("//div[@id='column-a']/header[text()='B']"));
// B to A
javascriptExecutor.executeScript(java_script);
Thread.sleep(3000);
Assert.assertTrue(isElementDisplayed("//div[@id='column-b']/header[text()='B']"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment