Private/TidalSeleniumLoginCheck.ps1
function TidalSeleniumLoginCheck { [CmdletBinding()] param( [parameter(Mandatory=$false)] [ValidatePattern('[0-9]')] [int]$ChromeProfileNumber = '0', [parameter(Mandatory=$true)] [ValidateSet("UserNamePwd","Apple","Facebook","Twitter")] [string]$LoginType ) $ServiceName = "Tidal" $SiteUrl = "https://listen.tidal.com/" $ChromeUserData = "$HOME\AppData\Local\Google\Chrome\User Data" $AvailableProfiles = $(Get-ChildItem -Path $ChromeUserData -Directory -Filter "Profile *").Name $ProfileDirName = 'Profile ' + $ChromeProfileNumber $ChromeProfile = @($AvailableProfiles) -match $ProfileDirName if (!$ChromeProfile) { Write-Error "Unable to find Chrome Profile '$ProfileDirName'. Halting!" return } switch ($LoginType) { 'UserNamePwd' {$Message = "Login to $ServiceName with a dedicated $ServiceName UserName and Password"} 'Apple' {$Message = "Login to $ServiceName using your $LoginType account UserName and Password"} 'Facebook' {$Message = "Login to $ServiceName using your$LoginType account UserName and Password"} 'Twitter' {$Message = "Login to $ServiceName using your $LoginType account UserName and Password"} } # Make sure we can connect to the Url try { $null = CheckUrlStatus -SiteUrl $SiteUrl -ErrorAction Stop } catch { Write-Error $_ return } try { $null = ChromeDriverAndEventGhostCheck -ErrorAction Stop } catch { Write-Error $_ return } try { $Driver = Start-SeChrome -Arguments @("window-size=1200x600", "user-data-dir=$ChromeUserData", "profile-directory=$ChromeProfile") # The below Tab + Enter will clear either the "Chrome was not shutdown properly" message or the "Chrome is being controlled by automated software" message #[OpenQA.Selenium.Interactions.Actions]::new($Driver).SendKeys([OpenQA.Selenium.Keys]::Tab).Perform() #[OpenQA.Selenium.Interactions.Actions]::new($Driver).SendKeys([OpenQA.Selenium.Keys]::Enter).Perform() #& "C:\Program Files (x86)\EventGhost\EventGhost.exe" -event ClearChromeRestoreMsg $EventGhostProcess = Get-Process eventghost -ErrorAction SilentlyContinue if ($EventGhostProcess) {$null = $EventGhostProcess | Stop-Process -ErrorAction SilentlyContinue} $EventGhostConfigFilePath = $(Get-Module AutoWebCreds).ModuleBase + '\' + 'EventGhost' + '\' + 'ConfigurationFiles' + '\' + 'eventghosttreett.xml' $null = Start-Process -FilePath "C:\Program Files (x86)\EventGhost\EventGhost.exe" -ArgumentList "-file `"$EventGhostConfigFilePath`"" Start-Sleep -Seconds 1 $null = Start-Process -FilePath "C:\Program Files (x86)\EventGhost\EventGhost.exe" -ArgumentList "-event `"MinimizeEventGhost`"" Start-Sleep -Seconds 1 $null = Start-Process -FilePath "C:\Program Files (x86)\EventGhost\EventGhost.exe" -ArgumentList "-event `"ClearChromeRestoreMsg`"" Enter-SeUrl $SiteUrl -Driver $Driver # Determine if we see a "Sign In" button. If we do, then we need to login $LoginButton = Get-SeElement -By XPath -Selection '//*[@id="sidebar"]/section[1]/button[2]' -Target $Driver if (!$LoginButton) { throw "Unable to find the SignIn button! Halting!" } } catch { Write-Error $_ return } if ($LoginButton) { if ([System.Environment]::OSVersion.Version.Build -lt 10240) { try { # Have the user provide Credentials [pscredential]$PSCreds = GetAnyBoxPSCreds -ServiceName $ServiceName -ErrorAction Stop } catch { Write-Error $_ return } } else { try { [pscredential]$PSCreds = UWPCredPrompt -ServiceName $ServiceName -SiteUrl $SiteUrl -Message $Message -ErrorAction Stop } catch { Write-Error $_ return } } # We need to actually Login try { Send-SeClick -Element $SignInButton -Driver $Driver -ErrorAction Stop } catch { Write-Error $_ return } ### Basic UserName and Password Login #### if ($LoginType -eq "UserNamePwd") { try { $null = TidalUserNamePwdLogin -SeleniumDriver $Driver -PSCreds $PSCreds -ErrorAction Stop } catch { Write-Error $_ return } } ### Login With Twitter ### if ($LoginType -eq "Twitter") { try { # Get "Twitter" Button $TwitterButton = Get-SeElement -By XPath -Selection "//button[contains(@class, 'btn-client-twitter')]" -Target $Driver if (!$TwitterButton) { throw "Cannot find 'Twitter' button! Halting!" } Send-SeClick -Element $TwitterButton -Driver $Driver -ErrorAction Stop } catch { Write-Error $_ return } # Even if the below fails, we might be okay if the Chrome Browser is already signed into a Google Account try { $null = TwitterAccountLogin -SeleniumDriver $Driver -PSCreds $PSCreds -ErrorAction Stop } catch { Write-Warning $_.Exception.Message } } ### Login With Facebook ### if ($LoginType -eq "Facebook") { try { # Get "Continue With Facebook" Link $ContinueWithFacebookLink = Get-SeElement -By XPath -Selection '//button[contains(@class, 'btn-client-facebook')]' -Target $SeleniumDriver if (!$ContinueWithFacebookLink) { throw "Cannot find 'Continue With Facebook' link! Halting!" } Send-SeClick -Element $ContinueWithFacebookLink -Driver $SeleniumDriver -ErrorAction Stop } catch { Write-Error $_ return } try { $null = FacebookAccountLogin -SeleniumDriver $Driver -PSCreds $PSCreds -ErrorAction Stop } catch { Write-Warning $_.Exception.Message } } ### Login with Apple ### if ($LoginType -eq "Apple") { try { # Get "Continue With Apple" Link $ContinueWithAppleLink = Get-SeElement -By XPath -Selection '//*[@id="appleid-signin"]' -Target $SeleniumDriver if (!$ContinueWithAppleLink) { throw "Cannot find 'Continue With Apple' link! Halting!" } Send-SeClick -Element $ContinueWithAppleLink -Driver $SeleniumDriver -ErrorAction Stop } catch { Write-Error $_ return } try { $null = AppleAccountLogin -SeleniumDriver $Driver -PSCreds $PSCreds -ErrorAction Stop } catch { Write-Warning $_.Exception.Message } } # So we need to check the webpage for an indication that we are actually logged in now try { $SuccessfulLoginIndicator = Get-SeElement -By XPath -Selection "//span[contains(text(),'My Mix')]" -Target $Driver if (!$SuccessfulLoginIndicator) { throw 'Unable to determine login was successful!' } else { $global:SuccessfulLogin = $True Write-Host "Successfully signed in!" -ForegroundColor Green } } catch { Write-Warning $_.Exception.Message } } $Driver <# $Driver.Close() $Driver.Dispose() #> } |