|
| 1 | +#WIP - this Example is imported from 1.5.0 UDF - and is in "WORK IN PROGRESS" state |
| 2 | +#AutoIt3Wrapper_UseX64=y |
| 3 | +#AutoIt3Wrapper_Run_AU3Check=Y |
| 4 | +#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y |
| 5 | +;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 |
| 6 | + |
| 7 | +; 001-BasicDemo.au3 |
| 8 | + |
| 9 | +#include <GUIConstantsEx.au3> |
| 10 | +#include <WindowsConstants.au3> |
| 11 | +#include "..\NetWebView2Lib.au3" |
| 12 | + |
| 13 | +Main() |
| 14 | + |
| 15 | +Func Main() |
| 16 | + ConsoleWrite("! MicrosoftEdgeWebview2 : version check: " & _NetWebView2_IsAlreadyInstalled() & ' ERR=' & @error & ' EXT=' & @extended & @CRLF) |
| 17 | + |
| 18 | + Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) |
| 19 | + #forceref $oMyError |
| 20 | + |
| 21 | + ; Create the UI |
| 22 | + Local $iHeight = 800 |
| 23 | + Local $hGUI = GUICreate("WebView2 .NET Manager - Community Demo", 1100, $iHeight) |
| 24 | + GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI") |
| 25 | + |
| 26 | + WinMove($hGUI, '', Default, Default, 800, 440) |
| 27 | + GUISetState(@SW_SHOW, $hGUI) |
| 28 | + |
| 29 | + ; Initialize WebView2 Manager and register events |
| 30 | + Local $oWebV2M = _NetWebView2_CreateManager("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0", "", "--disable-gpu, --mute-audio") |
| 31 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 32 | + |
| 33 | + ; Initialize JavaScript Bridge |
| 34 | + Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_") |
| 35 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 36 | + |
| 37 | + Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder" |
| 38 | + _NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, 1.2, "0x2B2B2B", False) |
| 39 | + If @error Then Return SetError(@error, @extended, $oWebV2M) |
| 40 | + |
| 41 | + __Example_Log(@ScriptLineNumber, "After: _NetWebView2_Initialize()" & @CRLF) |
| 42 | + |
| 43 | + ; navigate to HTML string - full fill the object with your own offline content - without downloading any content |
| 44 | + ConsoleWrite(@CRLF) |
| 45 | + |
| 46 | + __Example_Log(@ScriptLineNumber, "Before: _NetWebView2_NavigateToString()") |
| 47 | + GUISetState(@SW_SHOW, $hGUI) |
| 48 | + WinMove($hGUI, '', Default, Default, 1100, 800) |
| 49 | + |
| 50 | + #COMMENT This example is based on ==> ; https://github.com/Danp2/au3WebDriver/blob/1834e95206bd4a6ef6952c47a1f1192042f98c0b/wd_demo.au3#L588-L732 |
| 51 | + #Region - Testing how to manage frames |
| 52 | + _NetWebView2_Navigate($oWebV2M, 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe', $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, "", 5000) |
| 53 | + MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 1) |
| 54 | +;~ _Demo_NavigateCheckBanner($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe", '//*[@id="snigel-cmp-framework" and @class="snigel-cmp-framework"]') |
| 55 | + If @error Then Return SetError(@error, @extended) |
| 56 | + |
| 57 | + #Region ; testing NetWebView2Lib new methodes |
| 58 | + Local $iFrameCount = $oWebV2M.GetFrameCount() |
| 59 | + ConsoleWrite(@CRLF) |
| 60 | + ConsoleWrite("! " & @ScriptLineNumber & " : Frames=" & $iFrameCount & @CRLF) |
| 61 | + For $IDX_Frame = 0 To $iFrameCount -1 |
| 62 | + ConsoleWrite("- IDX=" & $IDX_Frame & @CRLF) |
| 63 | + ConsoleWrite("- URL=" & $oWebV2M.GetFrameUrl($IDX_Frame) & @CRLF) |
| 64 | + ConsoleWrite("- NAME=" & $oWebV2M.GetFrameName($IDX_Frame) & @CRLF) |
| 65 | + ConsoleWrite(@CRLF) |
| 66 | + Next |
| 67 | + |
| 68 | + ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameUrls() :" & @CRLF & $oWebV2M.GetFrameUrls() & @CRLF) |
| 69 | + ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameNames() :" & @CRLF & $oWebV2M.GetFrameNames() & @CRLF) |
| 70 | + For $IDX_Frame = 0 To $iFrameCount -1 |
| 71 | + ConsoleWrite(@CRLF & "======================================================" & @CRLF) |
| 72 | + ConsoleWrite("! " & @ScriptLineNumber & " : GetFrameHtmlSource("&$IDX_Frame&") :" & @CRLF & $oWebV2M.GetFrameHtmlSource($IDX_Frame) & @CRLF) |
| 73 | + Next |
| 74 | + ConsoleWrite(@CRLF & "======================================================" & @CRLF) |
| 75 | + ConsoleWrite(@CRLF) |
| 76 | + ConsoleWrite(@CRLF) |
| 77 | + #EndRegion ; testing NetWebView2Lib new methodes |
| 78 | + |
| 79 | +#CS |
| 80 | + ; just after navigate current context should be on top level Window |
| 81 | + ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF) |
| 82 | +
|
| 83 | + $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe[@id='iframeResult']") |
| 84 | + ; changing context to first frame |
| 85 | + _WD_FrameEnter($sSession, $sElement) |
| 86 | + If @error Then Return SetError(@error, @extended) |
| 87 | +
|
| 88 | + $bIsWindowTop = _WD_IsWindowTop($sSession) |
| 89 | + ; after changing context to first frame the current context is not on top level Window |
| 90 | + ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF) |
| 91 | +
|
| 92 | + ; changing context to first sub frame using iframe element specified ByXPath |
| 93 | + $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe") |
| 94 | + _WD_FrameEnter($sSession, $sElement) |
| 95 | + If @error Then Return SetError(@error, @extended) |
| 96 | +
|
| 97 | + ; Leaving sub frame |
| 98 | + _WD_FrameLeave($sSession) |
| 99 | + If @error Then Return SetError(@error, @extended) |
| 100 | +
|
| 101 | + $bIsWindowTop = _WD_IsWindowTop($sSession) |
| 102 | + ; after leaving sub frame, the current context is back to first frame but still is not on top level Window |
| 103 | + ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF) |
| 104 | +
|
| 105 | + ; Leaving first frame |
| 106 | + _WD_FrameLeave($sSession) |
| 107 | + If @error Then Return SetError(@error, @extended) |
| 108 | +#CE |
| 109 | + |
| 110 | + #EndRegion - Testing how to manage frames |
| 111 | + |
| 112 | +#CS |
| 113 | +
|
| 114 | + #Region - Testing _WD_FrameList() usage |
| 115 | +
|
| 116 | + #Region - Example 1 ; from 'https://www.w3schools.com' get frame list as string |
| 117 | + $bIsWindowTop = _WD_IsWindowTop($sSession) |
| 118 | + ; after leaving first frame, the current context should back on top level Window |
| 119 | + ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : TopWindow = " & $bIsWindowTop & @CRLF) |
| 120 | +
|
| 121 | + ; now lets try to check frame list and using locations as path 'null/0' |
| 122 | + ; firstly go to website |
| 123 | + _WD_Navigate($sSession, 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe') |
| 124 | + _WD_LoadWait($sSession) |
| 125 | +
|
| 126 | + Local $sResult = _WD_FrameList($sSession, False) |
| 127 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 1" & @CRLF) |
| 128 | + ConsoleWrite($sResult & @CRLF) |
| 129 | + #EndRegion - Example 1 ; from 'https://www.w3schools.com' get frame list as string |
| 130 | +
|
| 131 | + #Region - Example 2 ; from 'https://www.w3schools.com' get frame list as array |
| 132 | + Local $aFrameList = _WD_FrameList($sSession, True) |
| 133 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 2" & @CRLF) |
| 134 | + _ArrayDisplay($aFrameList, 'Example 2 - w3schools.com - get frame list as array', 0, 0, Default, $sArrayHeader) |
| 135 | +
|
| 136 | + #EndRegion - Example 2 ; from 'https://www.w3schools.com' get frame list as array |
| 137 | +
|
| 138 | + #Region - Example 3 ; from 'https://www.w3schools.com' get frame list as array, while current location is "null/0" |
| 139 | + ; check if document context location is Top Window - should be as we are after navigation |
| 140 | + ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF) |
| 141 | +
|
| 142 | + ; change document context location by path 'null/0' |
| 143 | + _WD_FrameEnter($sSession, 'null/0') |
| 144 | +
|
| 145 | + ; check if document context location is Top Window - should not be as we enter to frame 'null/0' |
| 146 | + ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF) |
| 147 | +
|
| 148 | + $aFrameList = _WD_FrameList($sSession, True) |
| 149 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 3" & @CRLF) |
| 150 | + _ArrayDisplay($aFrameList, 'Example 3 - w3schools.com - relative to "null/0"', 0, 0, Default, $sArrayHeader) |
| 151 | + #EndRegion - Example 3 ; from 'https://www.w3schools.com' get frame list as array, while current location is "null/0" |
| 152 | +
|
| 153 | + #Region - Example 4 ; from 'https://stackoverflow.com' get frame list as string |
| 154 | + ; go to another website |
| 155 | + _WD_Navigate($sSession, 'https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom') |
| 156 | + _WD_LoadWait($sSession) |
| 157 | +
|
| 158 | + $sResult = _WD_FrameList($sSession, False) |
| 159 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 4" & @CRLF) |
| 160 | + ConsoleWrite($sResult & @CRLF) |
| 161 | + #EndRegion - Example 4 ; from 'https://stackoverflow.com' get frame list as string |
| 162 | +
|
| 163 | + #Region - Example 5 ; from 'https://stackoverflow.com' get frame list as array |
| 164 | + $aFrameList = _WD_FrameList($sSession, True) |
| 165 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 5" & @CRLF) |
| 166 | + _ArrayDisplay($aFrameList, 'Example 5 - stackoverflow.com - get frame list as array', 0, 0, Default, $sArrayHeader) |
| 167 | + #EndRegion - Example 5 ; from 'https://stackoverflow.com' get frame list as array |
| 168 | +
|
| 169 | + #Region - Example 6v1 ; from 'https://stackoverflow.com' get frame list as array, while is current location is "null/2" |
| 170 | + ; check if document context location is Top Window - should be as we are after navigation |
| 171 | + ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF) |
| 172 | +
|
| 173 | + ; change document context location by path 'null/2' |
| 174 | + _WD_FrameEnter($sSession, 'null/2') |
| 175 | +
|
| 176 | + ; check if document context location is Top Window - should not be as we enter to frame 'null/2' |
| 177 | + ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF) |
| 178 | +
|
| 179 | + $aFrameList = _WD_FrameList($sSession, True) |
| 180 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 6v1" & @CRLF) |
| 181 | + _ArrayDisplay($aFrameList, 'Example 6v1 - stackoverflow.com - relative to "null/2"', 0, 0, Default, $sArrayHeader) |
| 182 | + #EndRegion - Example 6v1 ; from 'https://stackoverflow.com' get frame list as array, while is current location is "null/2" |
| 183 | +
|
| 184 | + #Region - Example 6v2 ; from 'https://stackoverflow.com' get frame list as array, check if it is still relative to the same location as it was before recent _WD_FrameList() was used - still should be "null/2" |
| 185 | + ; check if document context location is Top Window |
| 186 | + ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF) |
| 187 | +
|
| 188 | + $aFrameList = _WD_FrameList($sSession, True) |
| 189 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example 6v2" & @CRLF) |
| 190 | + _ArrayDisplay($aFrameList, 'Example 6v2 - stackoverflow.com - check if it is still relative to "null/2"', 0, 0, Default, $sArrayHeader) |
| 191 | + #EndRegion - Example 6v2 ; from 'https://stackoverflow.com' get frame list as array, check if it is still relative to the same location as it was before recent _WD_FrameList() was used - still should be "null/2" |
| 192 | +
|
| 193 | + #EndRegion - Testing _WD_FrameList() usage |
| 194 | +
|
| 195 | + #Region - Testing element location in frame set and iframe collecion |
| 196 | + ; go to website |
| 197 | + _WD_Navigate($sSession, 'https://www.tutorialspoint.com/html/html_frames.htm#') |
| 198 | + _WD_LoadWait($sSession) |
| 199 | +
|
| 200 | + ; check if document context location is Top Window |
| 201 | + ConsoleWrite("> " & @ScriptLineNumber & " IsWindowTop = " & _WD_IsWindowTop($sSession) & @CRLF) |
| 202 | +
|
| 203 | + MsgBox($MB_TOPMOST, "", 'Before checking location of multiple elements on multiple frames' & @CRLF & 'Try the same example with and without waiting about 30 seconds in order to see that many frames should be fully loaded, and to check the differences') |
| 204 | +
|
| 205 | + $aFrameList = _WD_FrameList($sSession, True, 5000, Default) |
| 206 | + ConsoleWrite("! ---> @error=" & @error & " @extended=" & @extended & " : Example : Testing element location in frame set - after pre-checking list of frames" & @CRLF) |
| 207 | + _ArrayDisplay($aFrameList, @ScriptLineNumber & ' Before _WD_FrameListFindElement - www.tutorialspoint.com - get frame list as array', 0, 0, Default, $sArrayHeader) |
| 208 | +
|
| 209 | + Local $aLocationOfElement = _WD_FrameListFindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "li.nav-item[data-bs-original-title='Home Page'] a.nav-link[href='https://www.tutorialspoint.com/index.htm']") |
| 210 | + ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : $aLocationOfElement (" & UBound($aLocationOfElement) & ")=" & @CRLF & _ArrayToString($aLocationOfElement) & @CRLF) |
| 211 | + _ArrayDisplay($aLocationOfElement, @ScriptLineNumber & ' $aLocationOfElement', 0, 0, Default, $sArrayHeader) |
| 212 | +
|
| 213 | + #EndRegion - Testing element location in frame set and iframe collecion |
| 214 | +
|
| 215 | +#CE |
| 216 | + |
| 217 | + ; Main Loop |
| 218 | + While 1 |
| 219 | + Switch GUIGetMsg() |
| 220 | + Case $GUI_EVENT_CLOSE |
| 221 | + ExitLoop |
| 222 | + EndSwitch |
| 223 | + WEnd |
| 224 | + |
| 225 | + GUIDelete($hGUI) |
| 226 | + |
| 227 | + |
| 228 | + _NetWebView2_CleanUp($oWebV2M, $oJSBridge) |
| 229 | +EndFunc ;==>Main |
| 230 | + |
| 231 | +; ============================================================================== |
| 232 | +; ; Function to update a text element inside the WebView UI |
| 233 | +; ============================================================================== |
| 234 | +Func UpdateWebUI($oWebV2M, $sElementId, $sNewText) |
| 235 | + If Not IsObj($oWebV2M) Then Return '' |
| 236 | + |
| 237 | + ; Escape backslashes, single quotes and handle new lines for JavaScript safety |
| 238 | + Local $sCleanText = StringReplace($sNewText, "\", "\\") |
| 239 | + $sCleanText = StringReplace($sCleanText, "'", "\'") |
| 240 | + $sCleanText = StringReplace($sCleanText, @CRLF, "\n") |
| 241 | + $sCleanText = StringReplace($sCleanText, @LF, "\n") |
| 242 | + |
| 243 | + Local $sJavaScript = "document.getElementById('" & $sElementId & "').innerText = '" & $sCleanText & "';" |
| 244 | + _NetWebView2_ExecuteScript($oWebV2M, $sJavaScript) |
| 245 | +EndFunc ;==>UpdateWebUI |
| 246 | + |
| 247 | +; ============================================================================== |
| 248 | +; MY EVENT HANDLER: Bridge (JavaScript Messages) |
| 249 | +; ============================================================================== |
| 250 | +Func _BridgeMyEventsHandler_OnMessageReceived($oWebV2M, $hGUI, $sMessage) |
| 251 | + Local Static $iMsgCnt = 0 |
| 252 | + |
| 253 | + If $sMessage = "CLOSE_APP" Then |
| 254 | + If MsgBox(36, "Confirm", "Exit Application?", 0, $hGUI) = 6 Then Exit |
| 255 | + Else |
| 256 | + MsgBox(64, "JS Notification", "Message from Browser: " & $sMessage) |
| 257 | + $iMsgCnt += 1 |
| 258 | + UpdateWebUI($oWebV2M, "mainTitle", $iMsgCnt & " Hello from AutoIt!") |
| 259 | + EndIf |
| 260 | +EndFunc ;==>_BridgeMyEventsHandler_OnMessageReceived |
| 261 | + |
| 262 | +; ============================================================================== |
| 263 | +; HELPER: Demo HTML Content |
| 264 | +; ============================================================================== |
| 265 | +Func __GetDemoHTML() |
| 266 | + Local $sH = _ |
| 267 | + '<html><head><style>' & _ |
| 268 | + 'body { font-family: "Segoe UI", sans-serif; background: #202020; color: white; padding: 40px; text-align: center; }' & _ |
| 269 | + '.card { background: #2d2d2d; padding: 20px; border-radius: 8px; border: 1px solid #444; }' & _ |
| 270 | + 'button { padding: 12px 24px; cursor: pointer; background: #0078d4; color: white; border: none; border-radius: 4px; font-size: 16px; margin: 5px; }' & _ |
| 271 | + 'button:hover { background: #005a9e; }' & _ |
| 272 | + '</style></head><body>' & _ |
| 273 | + '<div class="card">' & _ |
| 274 | + ' <h1 id="mainTitle">WebView2 + AutoIt .NET Manager</h1>' & _ ; Fixed ID attribute |
| 275 | + ' <p id="statusMsg">The communication is now 100% Event-Driven (No Sleep needed).</p>' & _ |
| 276 | + ' <button onclick="window.chrome.webview.postMessage(''Hello from JavaScript!'')">Send Ping</button>' & _ |
| 277 | + ' <button onclick="window.chrome.webview.postMessage(''CLOSE_APP'')">Exit App</button>' & _ |
| 278 | + '</div>' & _ |
| 279 | + '</body></html>' |
| 280 | + Return $sH |
| 281 | +EndFunc ;==>__GetDemoHTML |
| 282 | + |
| 283 | +Func __Example_Log($s_ScriptLineNumber, $sString, $iError = @error, $iExtended = @extended) |
| 284 | + ConsoleWrite(@ScriptName & ' SLN=' & $s_ScriptLineNumber & ' [' & $iError & '/' & $iExtended & '] ::: ' & $sString & @CRLF) |
| 285 | + Return SetError($iError, $iExtended, '') |
| 286 | +EndFunc ;==>__Example_Log |
0 commit comments