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"