/
Get-SpeedTest.psm1
143 lines (143 loc) · 5.74 KB
/
Get-SpeedTest.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
function Get-SpeedTest {
<#
.SYNOPSIS
Module for measuring internet speed
.DESCRIPTION
Examples:
Get-SpeedTest -Provider Libre
Get-SpeedTest -Provider Open
Get-SpeedTest -Provider Ookla
.LINK
https://github.com/Lifailon/Selenium-Modules
https://www.speedtest.net
https://openspeedtest.com
https://librespeed.org
#>
param (
[ValidateSet("Ookla","Open","Libre")][string]$Provider = "Ookla"
)
$path = "$home\Documents\Selenium\"
$log = "$path\ChromeDriver.log"
$ChromeDriver = "$path\ChromeDriver.exe"
$WebDriver = "$path\WebDriver.dll"
$SupportDriver = "$path\WebDriver.Support.dll"
$Chromium = (Get-ChildItem $path -Recurse | Where-Object Name -like chrome.exe).FullName
Add-Type -Path $WebDriver
Add-Type -Path $SupportDriver
try {
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.BinaryLocation = $Chromium
$ChromeOptions.AddArgument("start-maximized")
$ChromeOptions.AcceptInsecureCertificates = $True
$ChromeOptions.AddArgument("headless")
$ChromeDriverService = [OpenQA.Selenium.Chrome.ChromeDriverService]::CreateDefaultService($ChromeDriver)
$ChromeDriverService.HideCommandPromptWindow = $True
$ChromeDriverService.LogPath = $log
$ChromeDriverService.EnableAppendLog = $False
$ChromeDriverService.EnableVerboseLogging = $False
$Selenium = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeDriverService, $ChromeOptions)
if ($Provider -eq "Ookla") {
$Url = "https://www.speedtest.net"
$Selenium.Navigate().GoToUrl($Url)
Start-Sleep 1
$UrlTemp = $Selenium.Url
$span = $Selenium.FindElements([OpenQA.Selenium.By]::TagName("span"))
$Button = $span | Where-Object Text -Match "GO"
$Button.Click()
while ($True) {
if ($Selenium.Url -ne $UrlTemp) {
$UrlResult = $Selenium.Url
break
}
}
### Parsing Web Content (JSON)
$Cont = Invoke-RestMethod $UrlResult
$Data = ($Cont -split "window.OOKLA.")[3] -replace "(.+ = )|(;)" | ConvertFrom-Json
### Convert Unix Time
$EpochTime = [DateTime]"1/1/1970"
$TimeZone = Get-TimeZone
$UTCTime = $EpochTime.AddSeconds($Data.result.date)
$Data.result.date = $UTCTime.AddMinutes($TimeZone.BaseUtcOffset.TotalMinutes)
return $Data.result
}
if ($Provider -eq "Open") {
$Url = "https://openspeedtest.com"
$Selenium.Navigate().GoToUrl($Url)
Start-Sleep 1
$UrlTemp = $Selenium.Url
$Button = $Selenium.FindElements([OpenQA.Selenium.By]::CssSelector("#OpenSpeedtest"))
$Button.Click()
while ($True) {
if ($Selenium.Url -ne $UrlTemp) {
$UrlResult = $Selenium.Url
break
}
}
### Parsing Web Content (HTML)
$Cont = Invoke-RestMethod $UrlResult
$Cont = $Cont -replace '.+<text id="downResult"','<text id="downResult"'
$Cont = $Cont -replace '</text> </symbol><symbol.+','</text>'
$cont = $cont -split "</text>"
$down = $cont[0] -replace '.+class="rtextnum">'
$downType = $cont[1] -replace '.+class="rtextmbms">'
$up = $cont[2] -replace '.+class="rtextnum">'
$upType = $cont[3] -replace '.+class="rtextmbms">'
$ping = $cont[4] -replace '.+class="rtextnum">'
$pingType = $cont[5] -replace '.+class="rtextmbms">'
$jitter = $cont[6] -replace '.+class="rtextnum">'
$jitterType = $cont[7] -replace '.+class="rtextmbms">'
$carrier = $cont[8] -replace '.+<tspan x=0>|</tspan>'
$server = $cont[9] -replace '.+<tspan x=0>|</tspan>.+'
$ResultLink = $cont[9] -replace '.+class="rtextnum">'
$date = $cont[10] -replace '.+class="rtextnum">'
$Collections = New-Object System.Collections.Generic.List[System.Object]
$Collections.Add([PSCustomObject]@{
Download = "$down $downType";
Upload = "$up $upType";
Ping = "$ping $pingType";
Jitter = "$jitter $jitterType";
Carrier = $carrier;
Server = $server;
Result = $ResultLink;
Date = $date
})
return $Collections
}
if ($Provider -eq "Libre") {
$Url = "https://librespeed.org"
$Selenium.Navigate().GoToUrl($Url)
Start-Sleep 1
while ($True) {
$Button = $Selenium.FindElements([OpenQA.Selenium.By]::CssSelector("#startStopBtn"))
if ($Button.ComputedAccessibleRole -eq "generic") {
break
}
}
$Button.Click()
while ($true) {
if ($Selenium.FindElements([OpenQA.Selenium.By]::CssSelector("#shareArea")).Text.Length -gt 1) {
$Temp = "$path\temp.txt"
$Selenium.PageSource > $Temp
$cont = Get-Content $Temp
break
}
}
$ping = ($cont | Select-String 'id="pingText"' -Raw) -replace "</div>" -replace ".+>"
$jitter = ($cont | Select-String 'id="jitText"' -Raw) -replace "</div>" -replace ".+>"
$down = ($cont | Select-String 'id="dlText"' -Raw) -replace "</div>" -replace ".+>"
$up = ($cont | Select-String 'id="ulText"' -Raw) -replace "</div>" -replace ".+>"
$Collections = New-Object System.Collections.Generic.List[System.Object]
$Collections.Add([PSCustomObject]@{
Download = "$down Mbps";
Upload = "$up Mbps";
Ping = "$ping ms";
Jitter = "$jitter ms";
})
return $Collections
}
}
finally {
$Selenium.Close()
$Selenium.Quit()
}
}