-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAWS.SAML.ChromeDriver.psm1
168 lines (140 loc) · 4.57 KB
/
AWS.SAML.ChromeDriver.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using module .\AWS.SAML.Settings.psm1
function Get-ChromeDriverNeeded{
[CmdletBinding()]
[OutputType([String])]
param(
)
$version = Get-ChromeVersion
return Get-ChromeDriverPathByVersion -ChromeVersion $version
}
function Get-ChromeVersion {
[CmdletBinding()]
[OutputType([String])]
param(
)
switch ($true) {
$IsMacOS {
# Example response: Google Chrome 80.0.3987.163
$response = & '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' --version
return $response.trim(' ').split(' ')[-1]
}
$IsLinux {
throw 'Chrome Driver Management Not Implemented for Linux Yet!'
}
$IsWindows {
# Get File Path
$ChromeFile = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)'
return (Get-Item $ChromeFile).VersionInfo.ProductVersion
}
Default {
Throw 'Not able to detect the platform being used. Make sure you are using an updated version of PowerShell.'
}
}
}
function Get-ChromeDriverPathByVersion {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory=$true)]
[String]$ChromeVersion
)
$neededDriverVersion = Get-ChromeDriverVersionByChromeVersion -ChromeVersion $ChromeVersion
$versionsAvailable = Get-ChromeDriverVersions
$DriverFolder = Get-ChromeDriverFolder
# Check for the driver and download if needed
if($neededDriverVersion -notin $versionsAvailable){
Install-ChromeDriver -ChromeVersion $ChromeVersion
}
# Get Driver Details
$file = (Get-ChildItem "$DriverFolder\$neededDriverVersion\").Name
return "$DriverFolder\$neededDriverVersion\$file"
}
function Get-ChromeDriverVersions {
[CmdletBinding()]
param(
)
# Get Driver Details
$DriverFolder = Get-ChromeDriverFolder
Return (Get-ChildItem $DriverFolder).Name
}
function Install-ChromeDriver{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String]$ChromeVersion
)
# Get Driver Details
$DriverFolder = Get-ChromeDriverFolder
$DriverVersion = Get-ChromeDriverVersionByChromeVersion -ChromeVersion $ChromeVersion
# Download File
$DownloadFile = Invoke-ChromeDriverDownload -DriverFolder $DriverFolder -DriverVersion $DriverVersion
Expand-Archive -LiteralPath $DownloadFile -DestinationPath "$DriverFolder\$DriverVersion"
# Additional File Prep
if($IsMacOS){
# Mark as Executable
Push-Location "$DriverFolder\$DriverVersion"
& 'chmod' 755 chromedriver
Pop-Location
}
Remove-Item $DownloadFile
}
function Invoke-ChromeDriverDownload {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory=$true)]
[String]$DriverFolder,
[Parameter(Mandatory=$true)]
[String]$DriverVersion
)
$DriverName = Get-ChromeDriverName
$DownloadURI = "https://chromedriver.storage.googleapis.com/$DriverVersion/$DriverName"
$DownloadFile = "$($DriverFolder)\$DriverVersion`_$DriverName"
Invoke-WebRequest -Uri $DownloadURI -OutFile $DownloadFile
return $DownloadFile
}
function Get-ChromeDriverVersionByChromeVersion {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory=$true)]
[String]$ChromeVersion
)
$version = Select-ChromeVersionForDriver -ChromeVersion $ChromeVersion
$URL = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$version"
return Invoke-RestMethod -Method Get -Uri $URL
}
function Get-ChromeDriverFolder {
[CmdletBinding()]
[OutputType([String])]
param(
)
$directory = "$(Get-SaveDir)\ChromeDrivers"
# Create the folder if it doesn't exist
if(!(Test-Path $directory)){
New-Item -ItemType Directory $directory
}
return $directory
}
function Select-ChromeVersionForDriver {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory=$true)]
[String]$ChromeVersion
)
$versions = $ChromeVersion.Split('.')
return "$($versions[0]).$($versions[1]).$($versions[2])"
}
function Get-ChromeDriverName {
[CmdletBinding()]
[OutputType([String])]
param(
)
switch ($true) {
$IsMacOS { return 'chromedriver_mac64.zip' }
$IsLinux { return 'chromedriver_linux64.zip' }
$IsWindows { return 'chromedriver_win32.zip' }
Default { Throw 'Not able to detect the platform being used. Make sure you are using an updated version of PowerShell.' }
}
}