Skip to content

Commit

Permalink
version update
Browse files Browse the repository at this point in the history
Added ExecuteCDP method to WebDriver class to expose Chrome DevTools Protocol methods for browser automation
Added ExecuteCmd method to WebDriver class to facilitate testing of new/unwrapped Selenium WebDriver commands
Added SaveBase64StringToFile, and ResolvePath helper methods for above to WebDriver class
Added GetElementId helper method for above to WebDriver and WebElement classes
Added SendAlert method to WebDriver class
Refactored execute procedure of WebDriver class for improved clarity and error handling:
	- Added optional receiveTimeout input argument
	- Moved OpenBrowser-specific error handling to OpenBrowser method
	- Added code to handle non-standard Selenium and server communication errors
	- Added clientStatus error code to response package for caller error handling
Added implicit wait, page load timeout, and script timeout default parameters to SeleniumVBA ini file
Added globals to WebDriver class to track timeout settings for avoiding unnecessary calls to Selenium WebDriver
Extended OpenBrowser receiveTimeout from 3 secs to 10 secs to accomodate slower computers
Added optional arguments to NavigateToFile method of WebDriver class to align with same in NavigateTo method
Added file-exits check to NavigateToFile method of WebDriver class
Modified DefaultIOfolder get property of WebDriver class to return the default path
Added full screenshot option to SaveScreenshot method of WebDriver class
Added SetGeolocationAware method to WebCapabilities class to turn on/off browser geolocation
Added CtrlShiftKey method to WebActionChain class
Added test_ExecuteCmd and test_ExcuteCDP standard modules
Removed test_UserAgent standard module
Added more test procedures to test_ExecuteScript, test_Wait, test_IsPresent and others
Minor code mods and cleanup
Designed DLL work-around for twinBASIC Err.Raise issue twinbasic/twinbasic#464
Updated Help and Object model pages in Wiki, and SeleniumVBA Readme file
  • Loading branch information
GCuser99 committed Jun 20, 2023
1 parent 980e26a commit cfc0b10
Show file tree
Hide file tree
Showing 36 changed files with 1,162 additions and 229 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -7,17 +7,19 @@ A comprehensive Selenium wrapper for browser automation developed for MS Office
- Edge, Chrome, Firefox, and IE Mode browser automation support
- MS Excel Add-in, MS Access DB, and [twinBASIC](https://twinbasic.com/preview.html) ActiveX DLL solutions available
- A superset of Selenium's [WC3 WebDriver](https://w3c.github.io/webdriver/) commands - [over 350 public methods and properties](https://github.com/GCuser99/SeleniumVBA/wiki/Object-Model-Overview)
- Support for HTML DOM, Action Chains, SendKeys, Shadow Roots, Cookies, ExecuteScript, and Capabilities
- Support for HTML DOM, Action Chains, SendKeys, Shadow Roots, Cookies, ExecuteScript, CDP, and Capabilities
- Automated Browser/WebDriver version alignment - works out-of-the-box with no manual downloads necessary!
- Help documentation is available in the [SeleniumVBA Wiki](https://github.com/GCuser99/SeleniumVBA/wiki)

$\textcolor{green}{\textsf{What's New? ExecuteCDP method exposing Chrome DevTools Protocol - a low-level interface for browser interaction.}}$

## Setup

**SeleniumVBA will function right out-of-the-box**. Just download/install any one of the provided [SeleniumVBA solutions](https://github.com/GCuser99/SeleniumVBA/tree/main/dist) and then run one of the subs in the "test" Standard modules. If the Selenium WebDriver does not exist, or is out-of-date, SeleniumVBA will detect this automatically and download the appropriate driver to a [configurable location](https://github.com/GCuser99/SeleniumVBA/wiki#advanced-customization) on your system.

Driver updates can also be programmatically invoked via the [WebDriverManager class](https://github.com/GCuser99/SeleniumVBA/wiki/Object-Model-Overview#webdrivermanager).

To try the ActiveX DLL, download and run the installer in the [dist folder](https://github.com/GCuser99/SeleniumVBA/tree/main/dist).
The ActiveX DLL solution requires no dependencies (such as .Net Framework). To try it, download and run the installer in the [dist folder](https://github.com/GCuser99/SeleniumVBA/tree/main/dist).

## SendKeys Example

Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified dev/twinBasic/no_registration/SeleniumVBA_win64.dll
Binary file not shown.
Binary file modified dist/SeleniumVBA.accdb
Binary file not shown.
Binary file modified dist/SeleniumVBA.ini
Binary file not shown.
Binary file modified dist/SeleniumVBA.xlam
Binary file not shown.
Binary file modified dist/SeleniumVBADLLSetup.exe
Binary file not shown.
4 changes: 3 additions & 1 deletion dist/readme.md
Expand Up @@ -31,7 +31,9 @@ Instructions for referencing add-in versions of SeleniumVBA from another MS Exce

### ActiveX DLL Installation:

For the ActiveX DLL, more detailed instructions on how to install and use the DLL will be presented during installation. The setup program, which was compiled using Inno Setup, will install and register the DLL, and copy test Excel, Access, and VBScript documents to the installation folder. After installation, be aware that when it is first called during a VBA session, SeleniumVBA will display a "twinBASIC" banner for 5 seconds. Subsequent calls during the session will not show the banner.
For the ActiveX DLL, more detailed instructions on how to install and use the DLL will be presented during installation. The setup program, which was compiled using Inno Setup, will install and register the DLL, and copy test Excel, Access, and VBScript documents to the installation folder. After installation, be aware that when it is first called during a VBA session, SeleniumVBA will display a "twinBASIC" banner for 5 seconds. Subsequent calls during the session will not show the banner.

The ActiveX DLL solution requires no dependencies (such as .Net Framework).

### Advanced Customization - SeleniumVBA.ini File:

Expand Down
2 changes: 1 addition & 1 deletion src/ClassFactory.bas
Expand Up @@ -3,7 +3,7 @@ Attribute VB_Description = "This class is used for object instantiation when ref
'@ModuleDescription "This class is used for object instantiation when referencing SeleniumVBA externally from another code project"
'@folder("SeleniumVBA.Source")
' ==========================================================================
' SeleniumVBA v4.1
' SeleniumVBA v4.2
'
' A Selenium wrapper for browser automation developed for MS Office VBA
'
Expand Down
40 changes: 39 additions & 1 deletion src/WebActionChain.cls
Expand Up @@ -12,7 +12,7 @@ Attribute VB_Description = "This class is used to emulate a human interaction se
'@Exposed
'@folder("SeleniumVBA.Source")
' ==========================================================================
' SeleniumVBA v4.1
' SeleniumVBA v4.2
'
' A Selenium wrapper for browser automation developed for MS Office VBA
'
Expand Down Expand Up @@ -79,6 +79,7 @@ Attribute VB_Description = "This class is used to emulate a human interaction se
'ClickAndHold Holds down the left mouse button on an element
'ClickContext Performs a context-click (right click) on an element
'CtrlKey Sends a Control-[Key] sequence to focused element
'CtrlShiftKey Sends a Control-Shift-[Key] sequence to focused element
'DoubleClick Double-clicks an element
'DragAndDrop Holds down the left mouse button on the source element,
' then moves to the target element and releases the mouse button
Expand Down Expand Up @@ -819,6 +820,43 @@ Attribute ShiftKey.VB_Description = "Sends a Shift-[Key] sequence to focused ele
Set ShiftKey.DataDictionary = data_
End Function

'@Description("Sends a Control-Shift-[Key] sequence to focused element")
Public Function CtrlShiftKey(ByVal key As String, Optional element As WebElement) As WebActionChain
Attribute CtrlShiftKey.VB_Description = "Sends a Control-Shift-[Key] sequence to focused element"
Dim inputChan As New Dictionary
Dim inputChans() As Variant
Dim actions() As Dictionary

If Not element Is Nothing Then
Me.Click element 'get focus
End If

ReDim actions(0 To 6)
Set actions(0) = actionKeyDown("\ue009") 'control key
Set actions(1) = actionKeyDown("\ue008") 'shift key
Set actions(2) = actionKeyDown(key)
Set actions(3) = actionKeyUp(key)
Set actions(4) = actionKeyUp("\ue008")
Set actions(5) = actionKeyUp("\ue009")
Set actions(6) = actionPause

inputChan.Add "id", "keyboardDevice" 'for a given device, this needs to be same for entire chain
inputChan.Add "type", "key"
inputChan.Add "actions", actions

If data_.Exists("actions") Then
inputChans = data_.Item("actions")
data_.Item("actions") = appendInputChannel(inputChans, inputChan)
Else
data_.Add "actions", Array(inputChan)
End If

'now set things up for next link in chain
Set CtrlShiftKey = New WebActionChain
Set CtrlShiftKey.WebDriver = driver_
Set CtrlShiftKey.DataDictionary = data_
End Function

'@Description("Scrolls to element position with optional x and y offsets")
Public Function ScrollToElement(element As WebElement, Optional ByVal xOffset As Long = 0, Optional ByVal yOffset As Long = 0) As WebActionChain
Attribute ScrollToElement.VB_Description = "Scrolls to element position with optional x and y offsets"
Expand Down
59 changes: 46 additions & 13 deletions src/WebCapabilities.cls
Expand Up @@ -12,7 +12,7 @@ Attribute VB_Description = "This class is used to manage/set Selenium optional C
'@Exposed
'@folder("SeleniumVBA.Source")
' ==========================================================================
' SeleniumVBA v4.1
' SeleniumVBA v4.2
'
' A Selenium wrapper for browser automation developed for MS Office VBA
'
Expand Down Expand Up @@ -189,8 +189,10 @@ Attribute RemoveControlNotification.VB_Description = "Removes control notificati
Me.SetOption "useAutomationExtension", False
'for msedge, you may be greeted with an unwanted "Sync your profile" dialog
'to get rid of that, run in incognito mode
Case svbaBrowser.Firefox, svbaBrowser.IE
'no need to do anything
Case svbaBrowser.Firefox
Err.Raise 1, "WebCapabilities", "The RemoveControlNotification method is not supported by the Firefox browser."
Case svbaBrowser.IE
Err.Raise 1, "WebCapabilities", "The RemoveControlNotification method is not supported by the IE browser."
End Select
End Sub

Expand All @@ -201,19 +203,23 @@ Attribute SetDetachBrowser.VB_Description = "Sets the detach option for Edge/Chr
Select Case browser_
Case svbaBrowser.Chrome, svbaBrowser.Edge
Me.SetOption "detach", val
Case svbaBrowser.Firefox, svbaBrowser.IE
'no need to do anything
Case svbaBrowser.Firefox
Err.Raise 1, "WebCapabilities", "The SetDetachBrowser method is not supported by the Firefox browser."
Case svbaBrowser.IE
Err.Raise 1, "WebCapabilities", "The SetDetachBrowser method is not supported by the IE browser."
End Select
End Sub

'@Description("Sets the debuggerAddress option for Edge/Chrome")
'@Description("Sets the Debugger Address option for Edge/Chrome needed to connect with an already existing browser")
Public Sub SetDebuggerAddress(Optional ByVal val As String = "localhost:9222")
Attribute SetDebuggerAddress.VB_Description = "Sets the debuggerAddress option for Edge/Chrome"
Attribute SetDebuggerAddress.VB_Description = "Sets the Debugger Address option for Edge/Chrome needed to connect with an already existing browser"
Select Case browser_
Case svbaBrowser.Chrome, svbaBrowser.Edge
Me.SetOption "debuggerAddress", val
Case svbaBrowser.Firefox, svbaBrowser.IE
'no need to do anything
Case svbaBrowser.Firefox
Err.Raise 1, "WebCapabilities", "The SetDebuggerAddress method is not supported by the Firefox browser."
Case svbaBrowser.IE
Err.Raise 1, "WebCapabilities", "The SetDebuggerAddress method is not supported by the IE browser."
End Select
End Sub

Expand All @@ -238,7 +244,7 @@ Attribute RunInvisible.VB_Description = "Runs headless argument mode"
Case svbaBrowser.Firefox
Me.AddArguments "--headless"
Case svbaBrowser.IE
'not supported
Err.Raise 1, "WebCapabilities", "The RunInvisible method is not supported by the IE browser."
End Select
End Sub

Expand All @@ -253,7 +259,16 @@ Attribute RunIncognito.VB_Description = "Runs incognito/private argument mode"
Case svbaBrowser.Firefox
Me.AddArguments "-private"
Case svbaBrowser.IE
'not supported
'see https://www.selenium.dev/documentation/ie_driver_server/
'for this to work, the following register key value must be set:
'Key: "Computer\HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main"
'Value "TabProcGrowth" set to REG_DWORD = 0
'WARNING - setting the above key value to 0 will slow sendkeys (similar to 64-bit driver problem)
'Me.SetOption "ie.forceCreateProcessApi", True
'Me.SetOption "ie.browserCommandLineSwitches", "-private"
'Me.SetOption "ie.ensureCleanSession", True
'the above did not work for GCUser99
Err.Raise 1, "WebCapabilities", "The RunIncognito method is not supported by the IE browser."
End Select
End Sub

Expand Down Expand Up @@ -351,7 +366,25 @@ Attribute SetDownloadPrefs.VB_Description = "Sets auto-download preferences and
'Me.SetPreference "browser.download.useDownloadDir", True
'Me.SetPreference "browser.download.viewableInternally.enabledTypes", ""
Case svbaBrowser.IE
'no known way to do this
Err.Raise 1, "WebCapabilities", "The SetDownloadPrefs method is not supported by the IE browser."
End Select
End Sub

'@Description("Sets whether or not to allow geolocation awareness")
Public Sub SetGeolocationAware(Optional ByVal aware As Boolean = True)
Attribute SetGeolocationAware.VB_Description = "Sets whether or not to allow geolocation awareness"
'this may be required, for example, if using incognito mode and
'wanting navigated url to be aware of geolocation
Select Case browser_
Case svbaBrowser.Chrome, svbaBrowser.Edge
Me.SetPreference "profile.managed_default_content_settings.geolocation", IIf(aware, 1, 2)
'Me.SetPreference "googlegeolocationaccess.enabled", aware
'Me.SetPreference "profile.default_content_setting_values.notifications", IIf(aware, 1, 2)
'Me.SetPreference "profile.default_content_setting_values.geolocation", IIf(aware, 1, 2)
Case svbaBrowser.Firefox
Err.Raise 1, "WebCapabilities", "The SetGeolocationAware method is not supported by the Firefox browser."
Case svbaBrowser.IE
Err.Raise 1, "WebCapabilities", "The SetGeolocationAware method is not supported by the IE browser."
End Select
End Sub

Expand Down Expand Up @@ -394,7 +427,7 @@ Attribute LoadFromFile.VB_Description = "Loads the capabilities from file path"

filePath = WebShared.getFullLocalPath(filePath, driver_.DefaultIOFolder)

If Not fso.FileExists(filePath) Then Err.Raise 1, "WebCapabilities", "Capabilities Json file does not exist:" & vbNewLine & vbNewLine & filePath
If Not fso.FileExists(filePath) Then Err.Raise 53, "WebCapabilities", "Capabilities Json file does not exist:" & vbNewLine & vbNewLine & filePath
'note that this will fail if input file format is utf-8 with non-ascii chars - see https://github.com/GCuser99/SeleniumVBA/discussions/32
Set ts = fso.OpenTextFile(filePath, , , TristateUseDefault)
Set data_ = jc.ParseJson(ts.ReadAll)
Expand Down
2 changes: 1 addition & 1 deletion src/WebCookie.cls
Expand Up @@ -12,7 +12,7 @@ Attribute VB_Description = "This class is used to manage/modify a cookie object"
'@Exposed
'@folder("SeleniumVBA.Source")
' ==========================================================================
' SeleniumVBA v4.1
' SeleniumVBA v4.2
'
' A Selenium wrapper for browser automation developed for MS Office VBA
'
Expand Down
2 changes: 1 addition & 1 deletion src/WebCookies.cls
Expand Up @@ -12,7 +12,7 @@ Attribute VB_Description = "This class is used to manage a collection of cookie
'@Exposed
'@folder("SeleniumVBA.Source")
' ==========================================================================
' SeleniumVBA v4.1
' SeleniumVBA v4.2
'
' A Selenium wrapper for browser automation developed for MS Office VBA
'
Expand Down

0 comments on commit cfc0b10

Please sign in to comment.