Menu

[r5]: / trunk / WebDriver.vbs  Maximize  Restore  History

Download this file

236 lines (185 with data), 7.7 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
Sub Include(ByVal strFile)
Set objFs = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strFile = WshShell.ExpandEnvironmentStrings(strFile)
file = objFs.GetAbsolutePathName(strFile)
Set objFile = objFs.OpenTextFile(strFile)
strCode = objFile.ReadAll
objFile.Close
ExecuteGlobal(strCode)
End Sub
Include "JSON_2.0.4.vbs"
Include "WebElement.vbs"
Class WebDriver
Public sBaseURL
Public sSessionID
Private objHTTP
Private sBrowser
'Returns an element whose class name contains the search value; compound class names are not permitted.
Public className
'Returns an element matching a CSS selector.
Public cssSelector
'Returns an element whose ID attribute matches the search value.
Public id
'Returns an element whose NAME attribute matches the search value.
Public name
'Returns an anchor element whose visible text matches the search value.
Public linkText
'Returns an anchor element whose visible text partially matches the search value.
Public partialLinkText
'Returns an element whose tag name matches the search value.
Public tagName
'Returns an element matching an XPath expression.
Public xpath
Private Sub Class_Initialize
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
initializeLocators()
End Sub
Private Sub initializeLocators()
className = "class name"
cssSelector = "css selector"
id = "id"
name ="name"
linkText = "link text"
partialLinkText = "partial link text"
tagName = "tag name"
tagName = "xpath"
End Sub
Public Function connect(ByVal sHost, ByVal sPort,ByVal sBrowserName, ByVal sVersion, byVal aCap)
Dim sRequest
Dim objAllCaps : Set objAllCaps = jsObject()
sBaseURL = "http://" & sHost & ":" & sPort & "/wd/hub/session"
Set objAllCaps("desiredCapabilities") = jsObject()
objAllCaps("desiredCapabilities")("javascriptEnabled") = "true"
objAllCaps("desiredCapabilities")("nativeEvents") = "false"
objAllCaps("desiredCapabilities")("browserName") = sBrowserName
objAllCaps("desiredCapabilities")("version") = sVersion
sSessionID = getSession(executePost(sBaseURL,objAllCaps.jsString))
Set objAllCaps = Nothing
End Function
Public Function executePost(ByVal sRequest,ByVal sArgs)
Dim sResponse
'MsgBox "URL: " & sRequest & vbCrLf &"Args: " & sArgs
With objHTTP
.open "POST", sRequest, False
.setRequestHeader "Content-Type","application/json;charset=UTF-8"
.send sArgs
sResponse = .responseText
End With
ExecutePost = sResponse
End Function
Public Function executeGet(ByVal sRequest)
Dim sResponse
With objHTTP
.open "GET", sRequest, False
.setRequestHeader "Content-Type","application/json;charset=UTF-8"
.send
sResponse = .responseText
End With
ExecuteGet= sResponse
End Function
Private Function getSession(ByVal sResponseText)
Dim objRegExpr, colMatches
Set objRegExpr = New RegExp
objRegExpr.Pattern = "RequestURI=/wd/hub/session/[0-9]{13}"
objRegExpr.Global = True
objRegExpr.IgnoreCase = True
Set colMatches = objRegExpr.Execute(sResponseText)
If colMatches.Count <> 0 Then
GetSession = Mid(colMatches.Item(0),28,13)
End If
Set colMatches = Nothing
Set objRegExpr = Nothing
End Function
Public Sub closeWindow()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/window"
Call executeGet(sRequest)
End Sub
Public Function navigateTo(sURL)
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/url"
Set objArgs = jsObject()
objArgs("url")= sURL
Call executePost(sRequest,objArgs.jsString)
Set objArgs = Nothing
End Function
Public Function getCurrentUrl()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/url"
Dim parser : Set parser = jsonParser()
Dim sURL : sURL = executeGet(sRequest)
getCurrentUr = parser.getProperty(sURL, "value",False)
Set parser = Nothing
End Function
Public Function getTitle()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/title"
Dim parser : Set parser = jsonParser()
Dim sTitle : sTitle = executeGet(sRequest)
getTitle = parser.getProperty(sTitle,"value",False)
Set parser = Nothing
End Function
Public Function getPageSource()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/source"
Dim parser : Set parser = jsonParser()
Dim sSource : sSource = executeGet(sRequest)
getPageSource = parser.getProperty(sSource,"value",False)
Set parser = Nothing
End Function
Public function getSpeed()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/speed"
Dim parser : Set parser = jsonParser()
Dim sSpeed : sSpeed = executeGet(sRequest)
getSpeed = parser.getProperty(sSpeed,"value",False)
Set parser = Nothing
End Function
Public Function getAllCookies()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/cookie"
Dim parser : Set parser = jsonParser()
Dim sCookies : sCookies = executeGet(sRequest)
getAllCookies = parser.getProperty(sCookies,"value",False)
Set parser = Nothing
End Function
Public Function getAlertText()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/alert_text"
Dim parser : Set parser = jsonParser()
Dim sAlertText : sAlertText = executeGet(sRequest)
getAlertText = parser.getProperty(sAlertText,"value",False)
Set parser = Nothing
End Function
Public Sub acceptAlert()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/accept_alert"
Call executeGet(sRequest)
End Sub
Public Sub dismissAlert()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/dismiss_alert"
Call executeGet(sRequest)
End Sub
Public Function getScreenshot()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/screenshot"
Dim parser : Set parser = jsonParser()
Dim sScreenshot : sScreenshot = executeGet(sRequest)
getScreenshot = parser.getProperty(sScreenshot,"value",False)
End Function
Public Sub refresh()
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/refresh"
Call ExecutePost(sRequest,Null)
End Sub
Public Function findElementBy(ByVal sLocatorStrategy, ByVal sValue)
Dim sRequest : sRequest = sBaseURL & "/" & sSessionID & "/element"
Dim objAllCaps : Set objAllCaps = jsObject()
Dim parser : Set parser = jsonParser()
Dim oElement : Set oElement = New WebElement
Dim sResponse
objAllCaps("using") = sLocatorStrategy
objAllCaps("value") = sValue
sResponse = executePost(sRequest,objAllCaps.jsString)
oElement.Init me,parser.getProperty(sResponse,"value","ELEMENT")
Set objAllCaps = Nothing
Set parser = Nothing
Set findElementBy = oElement
End Function
End Class
Set Driver = New WebDriver
Driver.connect "127.0.0.1","4444","internet explorer", "", ""
Driver.navigateTo "http://www.google.com"
Driver.getCurrentUrl
Set Element = Driver.findElementBy(Driver.name,"q")
Element.sendKeys "VBScript"
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.