-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDriver.java
More file actions
195 lines (170 loc) · 5.53 KB
/
Driver.java
File metadata and controls
195 lines (170 loc) · 5.53 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.io.BufferedReader;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;
public class Driver {
private static String script;
private static WebDriver dr;
private static JavascriptExecutor js;
private static File scriptFile = new File("script.js");
private static String startUrl;
public static void main(String[] args) throws Exception {
setScript();
launchBrowser();
//dr.get("http://toolsqa.com/automation-practice-form/");
List<String> events;
String allEvents = "";
String total = allEvents;
String keep = "";
String current = dr.getCurrentUrl();
try {
while(true) {
js.executeScript(script);
// js.executeScript("document.addEventListener('DOMSubtreeModified', function(){"
// + "applyEvents();"
// + "});");
try {
while(dr.getCurrentUrl().equals(current)) {
total = (String)js.executeScript("return localStorage[0];");
}
}catch(Exception e) {
}
current = dr.getCurrentUrl();
allEvents+= total;
total = "";
}
}catch(Exception e) {
System.out.println("Test Ended");
if(allEvents.equals("")) {
events = separate(total);
}else {
events = separate(allEvents);
}
}
ExcelRW scriptSheet = getExcelSheet(stringToEvents(events));
scriptSheet.write();
}
public static List<EventLines> stringToEvents(List<String> events){
List<EventLines> newEvents = new ArrayList<EventLines>();
for(int i = 0; i < events.size(); i++) {
newEvents.add(new EventLines(events.get(i)));
}
return newEvents;
}
public static ExcelRW getExcelSheet(List<EventLines> events) {
String filename = "";
int decision = -1;
boolean loop = true;
boolean readFromFile = true;
while(loop) {
try {
decision = Integer.parseInt(JOptionPane.showInputDialog("Would you like to:\n1) Create new Script Sheet\n2) Append to existing Script Sheet"));
if((decision == 1)|| (decision ==2)){
loop = false;
}else {
throw new Exception("");
}
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "ERROR: Please enter a 1 or 2");
}
}
switch(decision) {
case 1:
filename = JOptionPane.showInputDialog("Enter filename");
readFromFile = false;
break;
case 2:
filename = JOptionPane.showInputDialog("Enter filename");
while(!inFolder(filename)) {
filename = JOptionPane.showInputDialog("File not found in folder\nEnter filename");
}
break;
default:
decision = Integer.parseInt(JOptionPane.showInputDialog("ERROR: Please enter a 1 or 2\n1) Create new Script Sheet\n2) Append to existing Script Sheet"));
}
if(filename.contains(".xlsx"))
filename.replace(".xlsx", " ");
return new ExcelRW(filename, events, readFromFile, startUrl);
}
public static boolean inFolder(String filename) {
File folder = new File("./");
File[] files = folder.listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isFile()) {
if(files[i].getName().contains(filename)) {
return true;
}
}
}
return false;
}
public static List<String> separate(String keep){
Stack<String> temp = new Stack<String>();
List<String> finalList = new ArrayList<String>();
String[] newKeep = keep.split("\n");
for(int i = newKeep.length -1; i >=0; i--) {
if(i == newKeep.length -1) {
temp.push(newKeep[i]);
}else {
String prev = temp.peek();
if(!prev.contains(newKeep[i])) {
temp.push(newKeep[i]);
}
}
}
for(int i = temp.size() -1; i >=0; i--) {
finalList.add(temp.get(i));
}
return finalList;
}
public static void launchBrowser() {
startUrl = JOptionPane.showInputDialog("Enter the URL of the site you would like to test");
//System.setProperty("webdriver.chrome.driver","chromedriver.exe");
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
//dr = new ChromeDriver(dc);
dr = new InternetExplorerDriver(dc);
js = (JavascriptExecutor)dr;
//dr.get("http://toolsqa.com/automation-practice-form/");
try {
dr.get(startUrl);
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error launching browser\nExiting...");
System.exit(0);
}
}
public static void setScript() {
Scanner file = null;
try {
file = new Scanner(scriptFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
script = readScript(file);
}
public static String readScript(Scanner file) {
String script = "";
while(file.hasNextLine()) {
script += file.nextLine();
script += "\n";
}
return script;
}
}