forked from vikramvi/Selenium-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragAndDropHTML5.java
More file actions
121 lines (92 loc) · 4.54 KB
/
DragAndDropHTML5.java
File metadata and controls
121 lines (92 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DragAndDropHTML5 {
private WebDriver webDriver;
@Before
public void beforeMethod() throws Exception {
//webDriver = new FirefoxDriver(); //Works
webDriver = new ChromeDriver(); //Works
}
@After
public void afterMethod() throws Exception {
webDriver.quit();
}
//http://stackoverflow.com/questions/33849040/drag-and-drop-testing-in-selenium-on-html5-using-java?lq=1
//http://stackoverflow.com/questions/29381233/how-to-simulate-html5-drag-and-drop-in-selenium-webdriver
@Test
public void testDragAndDrop() throws InterruptedException, IOException{
try{
String basePath = new File("").getAbsolutePath();
//System.setProperty("webdriver.Firefox.driver", "Path_executable");
//WebDriver driver= new FirefoxDriver();
webDriver.get("http://html5demos.com/drag#");
//http://stackoverflow.com/questions/29381233/how-to-simulate-html5-drag-and-drop-in-selenium-webdriver
//https://gist.github.com/rcorreia/2362544
final String JQUERY_LOAD_SCRIPT = (basePath + "/src/test/resources/jquery_load_helper.js");
String jQueryLoader = readFile(JQUERY_LOAD_SCRIPT);
webDriver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeAsyncScript(
jQueryLoader /* , http://localhost:8080/jquery-1.7.2.js */);
// ready to rock
js.executeScript("jQuery(function($) { " + " $('input[name=\"q\"]').val('bada-bing').closest('form').submit(); "
+ " }); ");
//http://stackoverflow.com/questions/29381233/how-to-simulate-html5-drag-and-drop-in-selenium-webdriver
//"where jquery_load_helper.js contains:"
String filePath = basePath + "/src/test/resources/drag_and_drop_helper.js";
//JQuery can ONLY work with id and css , xpath does NOT work with it.
//String source = "//section[@id='wrapper']/article/ul/li[4]/a";
String source = "section#wrapper article ul li:nth-child(4) a";
String target = "section#wrapper article div"; //#bin";
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader br = new BufferedReader(new FileReader(filePath));
while((line = br.readLine())!=null)
buffer.append(line);
String javaScript = buffer.toString();
javaScript = javaScript + "window.jQuery('" + source + "').simulateDragDrop({ dropTarget: '" + target + "'});";
((JavascriptExecutor)webDriver).executeScript(javaScript);
Thread.sleep(1000);
source = "section#wrapper article ul li:nth-child(2) a";
javaScript = javaScript + "$('" + source + "').simulateDragDrop({ dropTarget: '" + target + "'});";
((JavascriptExecutor)webDriver).executeScript(javaScript);
Thread.sleep(1000);
source = "section#wrapper article ul li:nth-child(1) a";
javaScript = javaScript + "$('" + source + "').simulateDragDrop({ dropTarget: '" + target + "'});";
((JavascriptExecutor)webDriver).executeScript(javaScript);
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
private static String readFile(String file) throws IOException {
Charset cs = Charset.forName("UTF-8");
FileInputStream stream = new FileInputStream(file);
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();
}
}
}