﻿;-- TIME_STAMP   2018-12-26 09:46:15   v 0.1

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n ; y|n
#AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#Region    ;************ Includes ************
#include-once
#include <InetConstants.au3>
;~ #include <wd_helper.au3>
;~ #Include <_BuildInZIP.au3>
;~ #Include <_GetFileVersionInfo.au3>
#Include "Includes\wd_helper.au3"
#Include "Includes\_BuildInZIP.au3"
#Include "Includes\_GetFileVersionInfo.au3"
#include <WinAPIRes.au3>
#include <WinAPIShPath.au3>
#EndRegion ;************ Includes ************

#include <WinAPISys.au3>


Global Enum $eFireFox, $eChrome

; Global $aDemoSuite = [["DemoTimeouts", False], ["DemoNavigation", True], ["DemoElements", False], ["DemoScript", False], ["DemoCookies", False], ["DemoAlerts", False], ["DemoFrames", False], ["DemoActions", True]]

Local $aDemoSuite[][2] = [["DemoTimeouts", False], _
                          ["DemoNavigation", True], _
                          ["DemoElements", False], _
                          ["DemoScript", False], _
                          ["DemoCookies", False], _
                          ["DemoAlerts", False], _
                          ["DemoFrames", False], _
                          ["DemoActions", True], _
                          ["DemoWindows", False]]

Global Const $_TestType = $eFireFox ; Hier kannst du auch $eChrome eintragen, wenn du den Chrome-Browser nutzen willst (und den Treiber hast..)

Global $sDesiredCapabilities, $iIndex, $sSession

$_WD_DEBUG = True

; ConsoleWrite("Switch" & @CRLF)
Switch $_TestType
	Case $eFireFox
		SetupGecko()
	Case $eChrome
		SetupChrome()
EndSwitch
; ConsoleWrite("EndSwitch" & @CRLF)



#Region ### Hier ist der Programmablauf
; ConsoleWrite("_WD_Startup()" & @CRLF)

_WD_Startup()

$sSession = _WD_CreateSession($sDesiredCapabilities)

For $iIndex = 0 To UBound($aDemoSuite, $UBOUND_ROWS) - 1
;    ConsoleWrite("$iIndex: " & $iIndex & @CRLF)
	If $aDemoSuite[$iIndex][1] Then
		ConsoleWrite("Running: " & $aDemoSuite[$iIndex][0] & @CRLF)
		Call($aDemoSuite[$iIndex][0])
	Else
		ConsoleWrite("Bypass: " & $aDemoSuite[$iIndex][0] & @CRLF)
	EndIf
Next

	Sleep (1000 * 30)

_WD_DeleteSession($sSession)
_WD_Shutdown()
; ConsoleWrite("_WD_Shutdown()" & @CRLF)

#EndRegion ### Hier ist der Programmablauf



Func DemoTimeouts()
	_WD_Timeouts($sSession)
	_WD_Timeouts($sSession, '{"pageLoad":2000}')
	_WD_Timeouts($sSession)
EndFunc   ;==>DemoTimeouts

Func DemoNavigation()
    $S = 15
;	_WD_Navigate($sSession, "https://autoit.com")
	_WD_Navigate($sSession, "https://autoitscript.com")
	ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
	Sleep (1000 * $S)
	_WD_Navigate($sSession, "https://google.com")
	ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
	Sleep (1000 * $S)
	_WD_Action($sSession, "back")
	ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
	Sleep (1000 * $S)
	_WD_Action($sSession, "forward")
	ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
	ConsoleWrite("Title=" & _WD_Action($sSession, 'title') & @CRLF)
	Sleep (1000 * $S)
EndFunc   ;==>DemoNavigation

Func DemoElements()
	Local $sElement, $aElements, $sValue

	_WD_Navigate($sSession, "http://google.com")
	$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib1']")

	If @error = $_WD_ERROR_NoMatch Then
		$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
	EndIf

	$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/input", '', True)

	_ArrayDisplay($aElements)

	_WD_ElementAction($sSession, $sElement, 'value', "testing 123")
	Sleep(500)

	_WD_ElementAction($sSession, $sElement, 'text')
	_WD_ElementAction($sSession, $sElement, 'clear')
	Sleep(500)
	_WD_ElementAction($sSession, $sElement, 'value', "abc xyz")
	Sleep(500)
	_WD_ElementAction($sSession, $sElement, 'text')
	_WD_ElementAction($sSession, $sElement, 'clear')
	Sleep(500)
	_WD_ElementAction($sSession, $sElement, 'value', "fujimo")
	Sleep(500)
	_WD_ElementAction($sSession, $sElement, 'text')
	_WD_ElementAction($sSession, $sElement, 'click')

	_WD_ElementAction($sSession, $sElement, 'Attribute', 'text')

	$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
	$sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')

	ConsoleWrite('value = ' & $sValue & @CRLF)

EndFunc   ;==>DemoElements

Func DemoScript()
	_WD_ExecuteScript($sSession, "return arguments[0].second;", '{"first": "1st", "second": "2nd", "third": "3rd"}')
	_WD_Alert($sSession, 'Dismiss')
EndFunc   ;==>DemoScript

Func DemoCookies()
	_WD_Navigate($sSession, "http://google.com")
	_WD_Cookies($sSession, 'Get', 'NID')
EndFunc   ;==>DemoCookies

Func DemoAlerts()
	ConsoleWrite('Alert Detected => ' & _WD_Alert($sSession, 'status') & @CRLF)
	_WD_ExecuteScript($sSession, "alert('testing 123')")
	ConsoleWrite('Alert Detected => ' & _WD_Alert($sSession, 'status') & @CRLF)
	ConsoleWrite('Text Detected => ' & _WD_Alert($sSession, 'gettext') & @CRLF)
	_WD_Alert($sSession, 'sendtext', 'new text')
	ConsoleWrite('Text Detected => ' & _WD_Alert($sSession, 'gettext') & @CRLF)
	Sleep(5000)
	_WD_Alert($sSession, 'Dismiss')

EndFunc   ;==>DemoAlerts

Func DemoFrames()
	_WD_Navigate($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols")
	ConsoleWrite("Frames=" & _WD_GetFrameCount($sSession) & @CRLF)
	ConsoleWrite("TopWindow=" & _WD_IsWindowTop($sSession) & @CRLF)
	Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe[@id='iframeResult']")
	;	_WD_Window($sSession, "frame", '{"id":{"' & $_WD_ELEMENT_ID & '":"' & $sElement & '"}}')
	_WD_FrameEnter($sSession, $sElement)
	ConsoleWrite("TopWindow=" & _WD_IsWindowTop($sSession) & @CRLF)
	_WD_FrameLeave($sSession)
	ConsoleWrite("TopWindow=" & _WD_IsWindowTop($sSession) & @CRLF)

EndFunc   ;==>DemoFrames

Func DemoActions()
;~ 	Local $sElement, $aElements, $sValue, $sAction ; $aElements/$sValue: declared, but not used in func.
	Local $sElement, $sAction

	_WD_Navigate($sSession, "http://google.com")
	$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")

	ConsoleWrite("$sElement = " & $sElement & @CRLF)

	$sAction = '{"actions":[{"id":"default mouse","type":"pointer","parameters":{"pointerType":"mouse"},"actions":[{"duration":100,"x":0,"y":0,"type":"pointerMove","origin":{"ELEMENT":"'
	$sAction &= $sElement & '","' & $_WD_ELEMENT_ID & '":"' & $sElement & '"}},{"button":2,"type":"pointerDown"},{"button":2,"type":"pointerUp"}]}]}'

	ConsoleWrite("$sAction = " & $sAction & @CRLF)

	_WD_Action($sSession, "actions", $sAction)
	Sleep(5000)
	_WD_Action($sSession, "actions")
	Sleep(5000)
EndFunc   ;==>DemoActions

; geckodriver	Selenium				Supported versions of Firefox
;										min	max
; 0.23.0		≥ 3.11 (3.14 Python)	57	n/a
; 0.22.0		≥ 3.11 (3.14 Python)	57	n/a
; 0.21.0		≥ 3.11 (3.14 Python)	57	n/a
; 0.20.1		≥ 3.5					55	62
; 0.20.0		≥ 3.5					55	62
; 0.19.1		≥ 3.5					55	62
; 0.19.0		≥ 3.5					55	62
; 0.18.0		≥ 3.4					53	62
; 0.17.0		≥ 3.4					52	62
Func SetupGecko()
;	Local $sFirefox = @ProgramFilesDir & '\Mozilla Firefox\firefox.exe'
	Local $sFirefox = 'D:\WWW\Firefox\firefox.exe'
	If Not FileExists($sFirefox) And @AutoItX64 Then $sFirefox = @ProgramFilesDir & ' (x86)\Mozilla Firefox\firefox.exe'
	If Not FileExists($sFirefox) Then Exit ConsoleWrite('! Error: File not found: ' & $sFirefox & @CRLF)
	ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sFirefox --> " & $sFirefox & @CRLF)

	Local $aFirefox_Version = _GetFileVersionInfo($sFirefox)
	If Not UBound($aFirefox_Version) Then Exit ConsoleWrite('! Error: No version found in file: ' & $sFirefox & @CRLF)
;~ 	_ArrayDisplay($aFirefox_Version, '$aFirefox_Version')

;~ 	_WD_Option('Driver', 'geckodriver.exe')
	Local $sX3264 = StringInStr($sFirefox, '(x86)') ? 'win32' : 'win64', $sGeckoVersion, $sGeckoDriver
	Switch Int($aFirefox_Version[1][1])
		Case 52
			$sGeckoVersion = 'v0.17.0'
		Case 53
			$sGeckoVersion = 'v0.18.0'
		Case 55 To 62
			$sGeckoVersion = 'v0.20.1'
		Case Else ; 57+
			$sGeckoVersion = 'v0.23.0'
	EndSwitch
	$sGeckoDriver = StringFormat('.\geckodriver\geckodriver-%s-%s\geckodriver.exe', $sGeckoVersion, $sX3264)
	ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sGeckoDriver --> " & $sGeckoDriver & @CRLF)

	If Not FileExists($sGeckoDriver) Then
;~ 		eg: https://github.com/mozilla/geckodriver/releases/download/v0.17.0/geckodriver-v0.17.0-win32.zip
		Local $sGeckoDriverZip = StringFormat('.\geckodriver\geckodriver-%s-%s.zip', $sGeckoVersion, $sX3264)
		ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sGeckoDriverZip --> " & $sGeckoDriverZip & @CRLF)
		If Not FileExists($sGeckoDriverZip) Then
			Local $sURL = StringFormat('https://github.com/mozilla/geckodriver/releases/download/%s/geckodriver-%s-%s.zip', $sGeckoVersion, $sGeckoVersion, $sX3264)
			ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sURL --> " & $sURL & @CRLF)
			Local $iBytesSize = InetGet($sURL, $sGeckoDriverZip, $INET_FORCERELOAD)
			ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $iBytesSize --> " & $iBytesSize & @CRLF)
		EndIf
;~ 		_BuildInZIP($sPath, $s_a_ToZip, $fUnZip = False, $iIndexBase = 1, $fOverwrite = True)
		Local $sGeckoDriverDir = StringFormat('.\geckodriver\geckodriver-%s-%s', 'v0.99.0', $sX3264)
		Local $iUnZip = _BuildInZIP($sGeckoDriverZip, $sGeckoDriverDir, True, 0, False)
		ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $iUnZip --> " & $iUnZip & @CRLF & "!@ " & @TAB & "#Error: " & @error & @TAB & "#Extended: " & @extended & @CRLF)
	EndIf
;	Exit
	_WD_Option('Driver', $sGeckoDriver)
	_WD_Option('DriverParams', '--log trace')
	_WD_Option('Port', 4444)

	$sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupGecko

; Ich will Chrome bei mir nicht installieren, deshalb wird hier nur der aktuelle chromedriver gedownloadet, falls er noch nicht vorhanden ist!
Func SetupChrome()
	Local $sChromeDriver = '.\chromedriver\chromedriver_win32\chromedriver.exe'
	ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sChromeDriver --> " & $sChromeDriver & @CRLF)
	If Not FileExists($sChromeDriver) Then
		Local $sChromeDriverZip = '.\chromedriver\chromedriver_win32.zip'
		ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sChromeDriverZip --> " & $sChromeDriverZip & @CRLF)
		If Not FileExists($sChromeDriverZip) Then
			Local $sURL = 'https://chromedriver.storage.googleapis.com/2.45/chromedriver_win32.zip' ; 2018-12-11 - Supports Chrome v70-72
			ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $sURL --> " & $sURL & @CRLF)
			Local $iBytesSize = InetGet($sURL, $sChromeDriverZip, $INET_FORCERELOAD)
			ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $iBytesSize --> " & $iBytesSize & @CRLF)
		EndIf
;~ 		_BuildInZIP($sPath, $s_a_ToZip, $fUnZip = False, $iIndexBase = 1, $fOverwrite = True)
		Local $sChromeDriverDir = '.\chromedriver\chromedriver_win32'
		Local $iUnZip = _BuildInZIP($sChromeDriverZip, $sChromeDriverDir, True, 0, False)
		ConsoleWrite("@@ Debug line" & @TAB & @ScriptLineNumber & "   var: $iUnZip --> " & $iUnZip & @CRLF & "!@ " & @TAB & "#Error: " & @error & @TAB & "#Extended: " & @extended & @CRLF)
	EndIf
;~ 	Exit

;~ 	_WD_Option('Driver', 'chromedriver.exe')
	_WD_Option('Driver', $sChromeDriver)
	_WD_Option('Port', 9515)
	_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

	$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
;~ 	$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true, "args": ["--user-data-dir=E:/ScriptDir/MyProfil"] }}}}'
EndFunc   ;==>SetupChrome
