Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Last active March 20, 2020 03:48
Show Gist options
  • Save steviecoaster/851158a7dc8238aae5ddbcf2aa428d64 to your computer and use it in GitHub Desktop.
Save steviecoaster/851158a7dc8238aae5ddbcf2aa428d64 to your computer and use it in GitHub Desktop.
Configuring Jenkins using Selenium.....because I'm a f*@#%# idiot like that
choco install googlechrome -y --no-progress
choco install jenkins -y -s https://chocolatey.org/api/v2
choco install selenium-chrome-driver -y -s https://chocolatey.org/api/v2
$ModuleBase = (Get-Module -ListAvailable Selenium).ModuleBase
Unblock-File -Path 'C:\tools\selenium\*'
Get-Process -Name *chrome*, *firefox*, *gecko* | Stop-Process -Force
Get-ChildItem -Path 'C:\tools\selenium\*' | Copy-Item -Destination "$ModuleBase\assemblies\" -Force -ErrorAction SilentlyContinue
Import-Module Selenium
# Start Jenkins in Selenium
$driver = Start-SeChrome -Maximized
Enter-SeUrl -Url http://localhost:8080 -Driver $driver
# Login to UI
gc 'C:\Program Files (x86)\Jenkins\secrets\initialAdminPassword' | Set-Clipboard
$input = Find-SeElement -Driver $driver -Id 'security-token'
Send-SeKeys -Element $input -Keys "$(Get-Clipboard)"
$Continue = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/form/div[1]/div/div/div/div[3]/input'
Invoke-SeClick -Driver $driver -Element $Continue
Start-Sleep -Seconds 3
# Install Suggested Plugins
$SuggestedPlugins = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div/div/div/div/div/div[2]/div/p[2]/a[1]'
Invoke-SeClick -Driver $driver -Element $SuggestedPlugins
#continue as admin
$ContinueAsAdmin = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div/div/div/div/div/div[3]/button[1]'
Invoke-SeClick -Element $ContinueAsAdmin -Driver $driver
Start-Sleep -Seconds 3
#Click Save and Finish Button
$Save = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div/div/div/div/div/div[3]/button[2]'
Invoke-SeClick -Element $Save -Driver $driver
Start-Sleep -Seconds 3
#Start using Jenkins
$Start = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div/div/div/div/div/div[2]/div/button'
Invoke-SeClick -Element $Start -Driver $driver
Start-Sleep -Seconds 3
#Manage Jenkins
$ManageJenkins = Find-SeElement -Driver $driver -XPath '//*[@id="tasks"]/div[4]/a[2]'
Invoke-SeClick -Element $ManageJenkins -Driver $driver
Start-Sleep -Seconds 3
# Manage Plugins
$ManagePlugins = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div[7]/a'
Invoke-SeClick -Element $ManagePlugins -Driver $driver
Start-Sleep -Seconds 3
# Click Available tab
$Available = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/form/div[1]/div[1]/div[2]/a'
Invoke-SeClick -Element $Available -Driver $driver
Start-Sleep -Seconds 3
# Filter for PowerShell
$filterBox = Find-SeElement -Driver $driver -Id 'filter-box'
Send-SeKeys -Element $filterBox -Keys 'Powershell'
#Tick check box
$TickBox = Find-SeElement -Driver $driver -XPath '//*[@id="plugins"]/tbody/tr[11]/td[1]/input'
Invoke-SeClick -Element $TickBox -Driver $driver
# Install w/o Restart
$InstallButton = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen1-button"]'
Invoke-SeClick -Element $InstallButton -Driver $driver
Start-Sleep -Seconds 15
# Go Back to Top
$BackToTop = Find-SeElement -Driver $driver -XPath '//*[@id="scheduleRestart"]/p[1]/a'
Invoke-SeClick -Element $BackToTop -Driver $driver
Start-Sleep -Seconds 5
#region Script deployment
If(!(Test-Path C:\scripts)){
$null = New-Item C:\scripts -ItemType Directory -Force
}
$string = @'
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]
$LocalRepo,
[Parameter(Mandatory)]
[string]
$LocalRepoApiKey,
[Parameter(Mandatory)]
[string]
$RemoteRepo
)
. .\ConvertTo-ChocoObject.ps1
Write-Verbose "Getting list of local packages from '$LocalRepo'."
$localPkgs = choco list --source $LocalRepo | Select-Object -Skip 1 | Select-Object -SkipLast 1 | ConvertTo-ChocoObject
Write-Verbose "Retrieved list of $(($localPkgs).count) packages from '$Localrepo'."
$localPkgs | ForEach-Object {
Write-Verbose "Getting remote package information for '$($_.name)'."
$remotePkg = choco list $_.name --source $RemoteRepo --exact | Select-Object -Skip 1 | Select-Object -SkipLast 1 | ConvertTo-ChocoObject
if ([version]($remotePkg.version) -gt ([version]$_.version)) {
Write-Verbose "Package '$($_.name)' has a remote version of '$($remotePkg.version)' which is later than the local version '$($_.version)'."
Write-Verbose "Internalizing package '$($_.name)' with version '$($remotePkg.version)'."
$tempPath = Join-Path -Path $env:TEMP -ChildPath ([GUID]::NewGuid()).GUID
choco download $_.name --no-progress --internalize --force --internalize-all-urls --append-use-original-location --output-directory=$tempPath --source=$RemoteRepo
if ($LASTEXITCODE -eq 0) {
Write-Verbose "Pushing package '$($_.name)' to local repository '$LocalRepo'."
(Get-Item -Path (Join-Path -Path $tempPath -ChildPath "*.nupkg")).fullname | ForEach-Object {
choco push $_ --source $LocalRepo --api-key $LocalRepoApiKey --force
if ($LASTEXITCODE -eq 0) {
Write-Verbose "Package '$_' pushed to '$LocalRepo'."
}
else {
Write-Verbose "Package '$_' could not be pushed to '$LocalRepo'.`nThis could be because it already exists in the repository at a higher version and can be mostly ignored. Check error logs."
}
}
}
else {
Write-Verbose "Failed to download package '$($_.name)'"
}
}
else {
Write-Verbose "Package '$($_.name)' has a remote version of '$($remotePkg.version)' which is not later than the local version '$($_.version)'."
}
}
'@
$string | Set-Content 'C:\scripts\Get-UpdatedPackage.ps1'
$string = @'
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]
$ProdRepo,
[Parameter(Mandatory)]
[string]
$ProdRepoApiKey,
[Parameter(Mandatory)]
[string]
$TestRepo
)
. .\ConvertTo-ChocoObject.ps1
# get all of the packages from the test repo
$testPkgs = choco list --source $TestRepo | Select-Object -Skip 1 | Select-Object -SkipLast 1 | ConvertTo-ChocoObject
$prodPkgs = choco list --source $ProdRepo | Select-Object -Skip 1 | Select-Object -SkipLast 1 | ConvertTo-ChocoObject
$tempPath = Join-Path -Path $env:TEMP -ChildPath ([GUID]::NewGuid()).GUID
if ($null -eq $testPkgs) {
Write-Verbose "Test repository appears to be empty. Nothing to push to production."
}
elseif ($null -eq $prodPkgs) {
$pkgs = $testPkgs
}
else {
$pkgs = Compare-Object -ReferenceObject $testpkgs -DifferenceObject $prodpkgs -Property name, version | Where-object SideIndicator -eq '<='
}
$pkgs | ForEach-Object {
Write-Verbose "Downloading package '$($_.name)' to '$tempPath'."
choco download $_.name --no-progress --output-directory=$tempPath --source=$TestRepo
if ($LASTEXITCODE -eq 0) {
$pkgPath = (Get-Item -Path (Join-Path -Path $tempPath -ChildPath '*.nupkg')).FullName
# #######################
# INSERT CODE HERE TO TEST YOUR PACKAGE
# #######################
# If package testing is successful ...
if ($LASTEXITCODE -eq 0) {
Write-Verbose "Pushing downloaded package '$(Split-Path -Path $pkgPath -Leaf)' to production repository '$ProdRepo'."
choco push $pkgPath --source=$ProdRepo --api-key=$ProdRepoApiKey --force
if ($LASTEXITCODE -eq 0) {
Write-Verbose "Pushed package successfully."
}
else {
Write-Verbose "Could not push package."
}
}
else {
Write-Verbose "Package testing failed."
}
Remove-Item -Path $pkgPath -Force
}
else {
Write-Verbose "Could not download package."
}
}
'@
$string | Set-Content 'C:\scripts\Update-ProdRepoFromTest.ps1'
$string = @'
function ConvertTo-ChocoObject {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline)]
[string]$InputObject
)
Process {
# format of the 'choco list' output is:
# <PACKAGE NAME> <VERSION> (ie. adobereader 2015.6.7)
if (-not [string]::IsNullOrEmpty($InputObject)) {
$props = $_.split(' ')
New-Object -TypeName psobject -Property @{ name = $props[0]; version = $props[1] }
}
}
}
'@
$string | Set-Content 'C:\scripts\ConvertTo-ChocoObject.ps1'
#endregion
# Update Test Repo Job
$NewItem = Find-SeElement -Driver $driver -XPath '//*[@id="tasks"]/div[1]/a[2]'
Invoke-SeClick -Element $NewItem -Driver $driver
Start-Sleep -Seconds 3
# Fill in Name
$Name = Find-SeElement -Driver $driver -XPath '//*[@id="name"]'
Send-SeKeys -Element $Name -Keys 'Update Test repository from Chocolatey Community Repository'
Start-Sleep -Milliseconds 500
#Select Pipeline
$Pipeline = Find-SeElement -Driver $driver -XPath '//*[@id="j-add-item-type-standalone-projects"]/ul/li[2]'
Invoke-SeClick -Element $Pipeline -Driver $driver
#Click OK Button
$OKButton = Find-SeElement -Driver $driver -XPath '//*[@id="ok-button"]'
Invoke-SeClick -Element $OKButton -Driver $driver
# Fill in the description
$Description = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div/div/div/form/table/tbody/tr[2]/td[3]/textarea'
Send-SeKeys -Element $Description -Keys 'Automatically update any out of date packages in the test repository from the Community Repository.'
Start-Sleep -Milliseconds 500
# Disable Concurrent
$Concurrent = Find-SeElement -Driver $driver -XPath '//*[@id="cb3"]'
Invoke-SeClick -Element $Concurrent -Driver $driver
# Parameterize
$Parameter = Find-SeElement -Driver $driver -XPath '//*[@id="cb8"]'
Invoke-SeClick -Element $Parameter -Driver $driver
# Add Parameters
$AddParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen1-button"]'
Invoke-SeClick -Element $AddParam -Driver $driver
# String Parameter
$StringParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen16"]/a'
Invoke-SeClick -Element $StringParam -Driver $driver
# Fill in Parameter Details
$p = [pscustomobject]@{
Name = 'P_LOCAL_REPO_URL'
DefaultValue = "http://localhost:8081/repository/Private"
Description = "Internal test repository"
}
# Add Parameters
$AddParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen1-button"]'
Invoke-SeClick -Element $AddParam -Driver $driver
Start-Sleep -Milliseconds 500
# String Parameter
$StringParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen16"]/a'
Invoke-SeClick -Element $StringParam -Driver $driver
Start-Sleep -Milliseconds 500
$Name = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[1]/table/tbody/tr[3]/td[3]/input'
Send-SeKeys -Element $Name -Keys "$($p.Name)"
Start-Sleep -Milliseconds 500
$Defaultvalue = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[1]/table/tbody/tr[6]/td[3]/input'
Send-SeKeys -Element $Defaultvalue -Keys "$($p.DefaultValue)"
Start-Sleep -Milliseconds 500
$Description = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[1]/table/tbody/tr[9]/td[3]/textarea'
Send-SeKeys -Element $Description -Keys "$($p.Description)"
Start-Sleep -Milliseconds 500
$TrimString = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[1]/table/tbody/tr[12]/td[3]/input'
Invoke-SeClick -Element $TrimString -Driver $driver
Start-Sleep -Milliseconds 500
$p = [pscustomobject]@{
Name = 'P_REMOTE_REPO_URL'
DefaultValue = 'https://chocolatey.org/api/v2'
Description = 'Remote repository containing updated package versions'
}
$AddParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen1-button"]'
Invoke-SeClick -Element $AddParam -Driver $driver
Start-Sleep -Milliseconds 500
# String Parameter
$StringParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen16"]/a'
Invoke-SeClick -Element $StringParam -Driver $driver
Start-Sleep -Milliseconds 500
$Name = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[2]/table/tbody/tr[3]/td[3]/input'
Send-SeKeys -Element $Name -Keys "$($p.Name)"
Start-Sleep -Milliseconds 500
$Defaultvalue = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[2]/table/tbody/tr[6]/td[3]/input'
Send-SeKeys -Element $Defaultvalue -Keys "$($p.DefaultValue)"
Start-Sleep -Milliseconds 500
$Description = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[2]/table/tbody/tr[9]/td[3]/textarea'
Send-SeKeys -Element $Description -Keys "$($p.Description)"
Start-Sleep -Milliseconds 500
$TrimString = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[2]/table/tbody/tr[12]/td[3]/input'
Invoke-SeClick -Element $TrimString -Driver $driver
Start-Sleep -Milliseconds 500
$p = [pscustomobject]@{
Name = 'P_LOCAL_REPO_API_KEY'
DefaultValue = "$(Get-Content "$env:TEMP\NugetApiKey.txt")"
Description = 'API key for the internal test repository where updated packages will be pushed.'
}
$AddParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen1-button"]'
Invoke-SeClick -Element $AddParam -Driver $driver
Start-Sleep -Milliseconds 500
# String Parameter
$StringParam = Find-SeElement -Driver $driver -XPath '//*[@id="yui-gen16"]/a'
Invoke-SeClick -Element $StringParam -Driver $driver
Start-Sleep -Milliseconds 500
$Name = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[3]/table/tbody/tr[3]/td[3]/input'
Send-SeKeys -Element $Name -Keys "$($p.Name)"
Start-Sleep -Milliseconds 500
$Defaultvalue = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[3]/table/tbody/tr[6]/td[3]/input'
Send-SeKeys -Element $Defaultvalue -Keys "$($p.DefaultValue)"
Start-Sleep -Milliseconds 500
$Description = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[66]/td[2]/div/div[3]/table/tbody/tr[9]/td[3]/textarea'
Send-SeKeys -Element $Description -Keys "$($p.Description)"
Start-Sleep -Milliseconds 500
# Pipeline Tab Setup
$Pipeline = Find-SeElement -Driver $driver -XPath '//*[@id="main-panel"]/div/div/div/div[2]/div[3]/div/div[4]'
Invoke-SeClick -Element $Pipeline -Driver $driver
Start-Sleep -Milliseconds 500
$script = @'
node {
powershell '''
Set-Location (Join-Path -Path $env:SystemDrive -ChildPath 'scripts')
$params= @{
LocalRepo = $env:P_LOCAL_REPO_URL
LocalRepoApiKey = $env:P_LOCAL_REPO_API_KEY
RemoteRepo = $env:P_REMOTE_REPO_URL
Verbose = $true
}
.\\Get-UpdatedPackage.ps1 @params
'''
}
'@
$ScriptBody = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[149]/td[2]/table/tbody/tr[2]/td[3]/div/div/div[5]/textarea'
Send-SeKeys -Element $scriptBody -Keys $script
# Click Save Button
$SaveButton = Find-SeElement -Driver $driver -XPath '/html/body/div[6]/div/div/div/div/form/table/tbody/tr[150]/td/div[2]/div[2]/span[1]/span/button'
Invoke-SeClick -Element $SaveButton -Driver $driver
Start-Sleep -Milliseconds 500
# Back To Dashboard
$BackToDashboard = Find-SeElement -Driver $driver -XPath '//*[@id="tasks"]/div[1]/a[2]'
Invoke-SeClick -Element $BackToDashboard -Driver $driver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment