virtix / cfwebdriver

Webdriver binding for ColdFusion.

This URL has Read+Write access

cfwebdriver / WebDriver.cfc
100644 273 lines (188 sloc) 6.631 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
 
  WebDriver
  @author bill
  @description ColdFusion wrapper for webdriver (http://code.google.com/p/webdriver)
  
  @todo: lots - subclass driver types
 
*/
 
<cfcomponent displayname="WebDriver" output="true">
 
  
  <cfscript>
this.by = createObject("java","org.openqa.selenium.By");
this.driver = "";
this.webelement = "";
this.driverType = "";
 
 
 
//
function newInstance(type){
switch(type){
 
case "ff" : {
return createObject("component","FirefoxDriver").init();
break;
}
 
case "firefox" : {
return createObject("component","FirefoxDriver").init();
break;
}
 
case "ie": {
return createObject("component","InternetExplorerDriver").init();
break;
}
 
case "htmlunit": {
return createObject("component","HtmlUnitDriver").init();
break;
}
 
case "safari" : {
return createObject("component","SafariDriver").init();
}
 
case "chrome" : {
return createObject("component","ChromeDriver").init();
}
 
default :
return createObject("component","HtmlUnitDriver").init();
break;
}
 
}
 
 
function getType(){
return this.driverType;
}
 
function setDriver(driver){
this.driver = driver;
return this;
}
 
 
function get(url) {
this.driver.get(arguments.url);
}
 
 
 
function close() {
this.driver.close();
}
  
         
function executeScript() {
throwNotImplementedException("executeScript");
} //java.lang.String, java.lang.Object[] :: java.lang.Object
 
 
function findElement(by){
/*
var element = createObject("component","WebElement");
this.webelement = this.driver.findElement(this.by.name(by));
element.init(this.webelement);
return element;
*/
 
try{
return findElementById(by);
}catch(org.openqa.selenium.NoSuchElementException ex){}
 
try{
return findElementByName(by);
}catch(org.openqa.selenium.NoSuchElementException ex){}
 
try{
return findElementByXpath(by);
}catch(org.openqa.selenium.NoSuchElementException ex){}
 
try{
return findElementByLinkText(by);
}catch(org.openqa.selenium.NoSuchElementException ex){}
 
return ""; //return empty string, if it's not there - no exceptions for now
}
 
 
function findElementByLinkText(text){
var element = createObject("component","WebElement");
this.webelement = this.driver.findElementByLinkText(text);
element.init(this.webelement);
return element;
}
 
 
 
function getTitle() {
return this.driver.getTitle();
} //java.lang.String
 
 
function getPageSource() {
return this.driver.getPageSource();
} //java.lang.String
 
 
function getCurrentUrl() {
return this.driver.getCurrentUrl();
} //java.lang.String
 
 
function findElementByName(name) {
var element = this.driver.findElement(this.by.name(name));
return createObject("component","WebElement").init(element);
}
 
function findElementById(id) {
var element = this.driver.findElement(this.by.id(id));
return createObject("component","WebElement").init(element);
} //)
 
function findElementsById(id) {
var elements = arrayNew(1);
var element = "";
var i = 1;
var webelements = this.driver.findElementsById(id);
for(i = 1; i lte arrayLen(webelements); i = i +1){
element = createObject("component","WebElement").init(webelements[i]);
elements.add(element);
}
return elements; //array of WebElement objects
} //)(java.lang.String) java.util.List
 
 
function findElements(by){
//to do: returns an array of element objects
// by name, xpath, etc...
}
 
 
function findElementsByName(name) {
var elements = arrayNew(1);
var element = "";
var i = 1;
var webelements = this.driver.findElementsByName(name);
for(i = 1; i lte arrayLen(webelements); i = i +1){
element = createObject("component","WebElement").init(webelements[i]);
elements.add(element);
}
return elements; //array of WebElement objects
}
 
 
 
 
 
function findElementsByXPath(xpath) {
var elements = arrayNew(1);
var element = "";
var i = 1;
//var webelements = this.driver.findElementsByXPath(xpath);
var webelements = this.driver.findElements(this.By.xPath(xpath));
for(i = 1; i lte arrayLen(webelements); i = i +1){
element = createObject("component","WebElement").init(webelements[i]);
elements.add(element);
}
return elements; //array of WebElement objects
}
 
 
function findElementByXPath(xpath) {
var element = this.driver.findElementByXPath(xpath);
return createObject("component","WebElement").init(element);
}
 
 
function getVisible() {
return this.driver.getVisible();
} //boolean
 
 
function setVisible(visible){
this.driver.setVisible( javacast("boolean",visible) );
} //void
 
 
function quit() {
this.driver.quit();
} //void
 
 
function navigate(location){
//throwNotImplementedException("navigate ... not shure best how to implement this");
this.driver.get(location);
} //org.openqa.selenium.WebDriver$Navigation
 
 
//Not tested.
function setProxy(proxyUrl, port){
this.driver.setProxy(proxyUrl, port);
} //void
 
 
//authenticateAs(String username, String password, String host, int port, String clientHost, String domain)
function authenticateAs(username,password, host, port, clientHost, domain){
this.driver.authenticateAs(username,password, host, port, clientHost, domain);
}
 
 
function switchTo(){
return this.driver.switchTo();
} //org.openqa.selenium.WebDriver$TargetLocator
 
 
 
 
 
//To Do's ......................
 
 
function manage(){
throwNotImplementedException("manage");
} //org.openqa.selenium.WebDriver$Options
 
 
  </cfscript>
 
 
 <cffunction name="throwNotImplementedException">
   <cfargument name="methodName" />
   <cfthrow type="org.mxunit.exception.NotImplementedException"
            message="The method you called has not yet been implemented"
            detail="Method name: #arguments.methodName#">
 </cffunction>
 
 <cffunction name="throwException">
   <cfargument name="type" />
   <cfargument name="message" />
   <cfargument name="detail" />
   <cfthrow type="#arguments.type#"
            message="#arguments.message#"
            detail="#arguments.detail#">
 </cffunction>
 
</cfcomponent>