From 621127eaf039d2fb42d13c4ec0dc783a40ea3580 Mon Sep 17 00:00:00 2001 From: yuriy Date: Fri, 28 Jul 2023 10:15:46 -0400 Subject: [PATCH] code refactoring, file name cleanups, added ability to use portable version of chrome to Selenium functions --- ...-Shared-Functions.ahk => API-Functions.ahk | 0 Chrome-Functions.ahk | 87 +++++++++++++ ...red-Functions.ahk => General-Functions.ahk | 9 -- ...hared-Functions.ahk => Gitea-Functions.ahk | 0 ...ed-Functions.ahk => Selenium-Functions.ahk | 123 +++++------------- 5 files changed, 116 insertions(+), 103 deletions(-) rename APIs-Shared-Functions.ahk => API-Functions.ahk (100%) create mode 100644 Chrome-Functions.ahk rename General-Shared-Functions.ahk => General-Functions.ahk (98%) rename Gitea-Shared-Functions.ahk => Gitea-Functions.ahk (100%) rename Selenium-Shared-Functions.ahk => Selenium-Functions.ahk (88%) diff --git a/APIs-Shared-Functions.ahk b/API-Functions.ahk similarity index 100% rename from APIs-Shared-Functions.ahk rename to API-Functions.ahk diff --git a/Chrome-Functions.ahk b/Chrome-Functions.ahk new file mode 100644 index 0000000..8d5b224 --- /dev/null +++ b/Chrome-Functions.ahk @@ -0,0 +1,87 @@ +; Notes/Extra Info/#Includes +;------------------------------------------------ +; Chrome Related Functions + + + +GetChromeFilepath(){ + ; ChromeFilepath is global variable + ; Establish Variable with Filepath to be used throughout the script + + ChromePortableFilepath = %A_ScriptDir%\Lib\chrome-win64\chrome.exe + + if(FileExist(ChromePortableFilepath)){ + ChromeFilepath := ChromePortableFilepath + } + else { + if(FileExist("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")) + ChromeFilepath = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe + + if(FileExist("C:\Program Files\Google\Chrome\Application\chrome.exe")) + ChromeFilepath = C:\Program Files\Google\Chrome\Application\chrome.exe + } +} + +GetChromeVersion(){ + ; https://stackoverflow.com/questions/52457766/how-can-i-get-google-chromes-version-number + ; Command = powershell `$ChromePath = 'C:\Program Files\Google\Chrome\Application\chrome.exe' `; [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion + + ; command = powershell $(Get-Package -Name 'Google Chrome').Version + ; ChromeVersion := RunCMD(command) + ; ChromeVersion := StrSplit(ChromeVersion, "`r")[1] ; remove newline character if it exists + + if(ChromeFilepath = ""){ + GetChromeFilepath() + } + + GetChromeVersionCommand = powershell (Get-Item '%ChromeFilepath%').VersionInfo.ProductVersion + Chromeversion := RunCMD(GetChromeVersionCommand) + ChromeVersion := StrReplace(ChromeVersion, "`r")[1] ; replace any newline characters that powershell returns + return ChromeVersion +} + + +GetURLofLatestChromedriver(ChromeVersion){ + + ; https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints + json_str := urldownloadtovar("https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json") + + ; requires #include of json.ahk in parent script + parsed := JSON.Load(json_str) + + ChromelabsJsonEntriesCount := parsed.versions.count() + + if(ChromelabsJsonEntriesCount = ""){ + return "Failed to parse chromedriver json. " + } + + loop % ChromelabsJsonEntriesCount { + + if(A_index = ChromelabsJsonEntriesCount){ + Message = "Failed to find %ChromeVersion% in the ChromeLabs Json" + return Message + } + + if(ChromeVersion = parsed.versions[A_Index].version){ + + VersionIndex := A_Index + + ; loop through the platforms to get download URL + loop % parsed.versions[VersionIndex].downloads.chromedriver.count() { + + ; 64 bit version + if(parsed.versions[VersionIndex].downloads.chromedriver[A_Index].platform = "win64") + chromedriverDLURL64 := parsed.versions[VersionIndex].downloads.chromedriver[A_Index].url + + ; 32 bit version + if(parsed.versions[VersionIndex].downloads.chromedriver[A_Index].platform = "win32") + chromedriverDLURL32 := parsed.versions[VersionIndex].downloads.chromedriver[A_Index].url + } + break + } + } + + ; Msgbox % "chromedriverDLURL32: " chromedriverDLURL32 + ; Msgbox % "chromedriverDLURL64: " chromedriverDLURL64 + return chromedriverDLURL32 +} \ No newline at end of file diff --git a/General-Shared-Functions.ahk b/General-Functions.ahk similarity index 98% rename from General-Shared-Functions.ahk rename to General-Functions.ahk index 0cba934..d10aa88 100644 --- a/General-Shared-Functions.ahk +++ b/General-Functions.ahk @@ -4,15 +4,6 @@ ; -------------------------------Variables------------------------------- ; Declare global variables here so they don't have to be declared in each script -global ChromeTabsURLArray -global DriverTitleArray -global LastWebsitePostURL -global ChromeFilepath -global CurrentTabURL -global URLOfLastErrorPage - -; #Include %A_ScriptDir%\ChromeAutomationFunctions.ahk -; #Include %A_ScriptDir%\RunCMD.ahk DevModeMsgBox(Message){ diff --git a/Gitea-Shared-Functions.ahk b/Gitea-Functions.ahk similarity index 100% rename from Gitea-Shared-Functions.ahk rename to Gitea-Functions.ahk diff --git a/Selenium-Shared-Functions.ahk b/Selenium-Functions.ahk similarity index 88% rename from Selenium-Shared-Functions.ahk rename to Selenium-Functions.ahk index 49196ac..7b9d012 100644 --- a/Selenium-Shared-Functions.ahk +++ b/Selenium-Functions.ahk @@ -1,68 +1,18 @@ -; Functions +; Notes/Extra Info/#Includes ;------------------------------------------------ +; Selenium Related Functions -GetChromeVersion(){ - ; https://stackoverflow.com/questions/52457766/how-can-i-get-google-chromes-version-number - ; Command = powershell `$ChromePath = 'C:\Program Files\Google\Chrome\Application\chrome.exe' `; [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion - - command = powershell $(Get-Package -Name 'Google Chrome').Version - ChromeVersion := RunCMD(command) - ChromeVersion := StrSplit(ChromeVersion, "`r")[1] ; remove newline character if it exists - return ChromeVersion -} - -GetURLofLatestChromedriver(ChromeVersion){ - - ; https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints - json_str := urldownloadtovar("https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json") - - ; requires #include of json.ahk in parent script - parsed := JSON.Load(json_str) - - ChromelabsJsonEntriesCount := parsed.versions.count() - - if(ChromelabsJsonEntriesCount = ""){ - return "Failed to parse chromedriver json. " - } - - loop % ChromelabsJsonEntriesCount { - - if(A_index = ChromelabsJsonEntriesCount){ - Message = "Failed to find %ChromeVersion% in the ChromeLabs Json" - return Message - } - - if(ChromeVersion = parsed.versions[A_Index].version){ - - VersionIndex := A_Index - - ; loop through the platforms to get download URL - loop % parsed.versions[VersionIndex].downloads.chromedriver.count() { - - ; 64 bit version - if(parsed.versions[VersionIndex].downloads.chromedriver[A_Index].platform = "win64") - chromedriverDLURL64 := parsed.versions[VersionIndex].downloads.chromedriver[A_Index].url - - ; 32 bit version - if(parsed.versions[VersionIndex].downloads.chromedriver[A_Index].platform = "win32") - chromedriverDLURL32 := parsed.versions[VersionIndex].downloads.chromedriver[A_Index].url - } - break - } - } - - ; Msgbox % "chromedriverDLURL32: " chromedriverDLURL32 - ; Msgbox % "chromedriverDLURL64: " chromedriverDLURL64 - return chromedriverDLURL32 -} - - +; Global Variables +;------------------------------------------------ +; Declare global variables here so they don't have to be declared in each script +global ChromeTabsURLArray +global DriverTitleArray +global LastWebsitePostURL +global CurrentTabURL +global URLOfLastErrorPage ShowSeleniumErrorMsgbox(){ - GetChromeVersionCommand = powershell (Get-Item '%ChromeFilepath%').VersionInfo.ProductVersion - Chromeversion := RunCMD(GetChromeVersionCommand) - ChromeVersion := StrReplace(ChromeVersion, "`n", "") ; Clipboard := ChromeVersion ; Msgbox % "Chromeversion: " Chromeversion @@ -544,6 +494,9 @@ SaveDriverURL(){ ; save the url of the result page. That way if a tab is not fou ; -------------------------------SChrome_Get------------------------------- SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){ + Message = Trying to Connect to Chrome + SaveOrPostProgress(Message:=Message,PostType:="Tooltip") + IP_Port_Nr := RegExReplace(IP_Port, ".*:(\d*)", "$1") if WinExist("ahk_exe Chrome.exe"){ WinGet, pid, PID, ahk_exe chrome.exe @@ -573,44 +526,26 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){ } } - ; ; yuriy's settings - ; IniRead, ChromeFilepath, C:\Users\%A_username%\Documents\Autohotkey\Lib\ScriptSettings.ini, Selenium, %A_Computername%, %A_Space% - - ; ; Establish Variable with Filepath to be used throughout the script - ; if(ChromeFilepath = ""){ - if(FileExist("C:\Program Files\Google\Chrome\Application\chrome.exe")){ - ChromeFilepath = C:\Program Files\Google\Chrome\Application\chrome.exe - } - else if (FileExist("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")){ - ChromeFilepath = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe - } - - - if(ChromeFilepath = ""){ - Message = Failed to find chrome.exe in the usual locations. - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - MsgBox 0x30, Error!, Unable to find Chrome.exe in the usual locations. `nProgram Exiting. - ExitApp - } - - if(!winExist("ahk_exe chrome.exe")){ - run, %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL% - } - - Driver := ComObjCreate("Selenium.ChromeDriver") - Driver.SetCapability("debuggerAddress", IP_Port) - try Driver.Start() - catch e { + if(ChromeFilepath = ""){ + GetChromeFilepath() + } - ShowSeleniumErrorMsgbox() + if(!winExist("ahk_exe chrome.exe")){ + run, %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL% + } - } ; end of catch + Driver := ComObjCreate("Selenium.ChromeDriver") + Driver.SetCapability("debuggerAddress", IP_Port) + try Driver.Start() + catch e { + ShowSeleniumErrorMsgbox() + } ; end of catch - ; Save current chrome version to ini file - return Driver - } + ; Save current chrome version to ini file + return Driver + } - ; -------------------------------/SChrome_Get------------------------------- + ; -------------------------------/SChrome_Get------------------------------- /* */