-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ps1
More file actions
213 lines (195 loc) · 10.2 KB
/
Copy pathrun.ps1
File metadata and controls
213 lines (195 loc) · 10.2 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Run Cucumber with BrowserStack javaagent using an args file (PowerShell)
# Usage: Open PowerShell, cd to project root and run: .\run.ps1
# Usage:
# .\run.ps1 # runs both no-agent (sanity) and agent runs (default)
# .\run.ps1 -SkipNoAgent # run only the agent-enabled JVM
param(
[switch]$SkipNoAgent
)
$ErrorActionPreference = 'Stop'
Write-Host "==== run.ps1 starting ===="
Write-Host "Current dir: $(Get-Location)"
Write-Host "USERPROFILE: $env:USERPROFILE"
# find java on PATH or common install locations
$java = $null
# prefer JAVA_HOME if provided
if ($env:JAVA_HOME) {
$candidate = Join-Path $env:JAVA_HOME 'bin\java.exe'
if (Test-Path $candidate) {
$java = $candidate
Write-Host "Using java from JAVA_HOME: $java"
} else {
Write-Host "JAVA_HOME is set but $candidate not found"
}
}
# next, try java on PATH
if (-not $java) {
$cmd = Get-Command java -ErrorAction SilentlyContinue
if ($cmd) { $java = $cmd.Source; Write-Host "Found java on PATH: $java" }
}
# fallback to common install locations if still not found
if (-not $java) {
$possible = @("C:\\Program Files\\Java\\jdk-17\\bin\\java.exe","C:\\Program Files\\OpenJDK\\jdk-17\\bin\\java.exe","C:\\Users\\Administrator\\Downloads\\openjdk-17.0.0.1+2_windows-x64_bin\\jdk-17.0.0.1\\bin\\java.exe")
$java = $possible | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($java) { Write-Host "Using java from known location: $java" }
}
if (-not $java) { Write-Error "java not found via JAVA_HOME, PATH or common locations. Install JDK17 or set java on PATH or set JAVA_HOME."; exit 1 }
Write-Host "Using java: $java"
# find BrowserStack SDK jar: prefer ./lib next to app, then fallback to local Maven repo
$localLibPath = Join-Path (Get-Location) 'lib'
$localBs = $null
if (Test-Path $localLibPath) {
$localBs = Get-ChildItem -Path $localLibPath -Filter '*browserstack-java-sdk*.jar' -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 1
}
if ($localBs) {
$BROWSERSTACK_JAR = $localBs.FullName
Write-Host "Found BROWSERSTACK_JAR in ./lib: $BROWSERSTACK_JAR"
} else {
$repo = Join-Path $env:USERPROFILE ".m2\repository"
$bsJar = Get-ChildItem -Path $repo -Filter "*browserstack-java-sdk*.jar" -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $bsJar) { Write-Error "BrowserStack JAR not found under $repo and no ./lib copy present. Place browserstack-java-sdk-<ver>.jar in ./lib or install via Maven."; exit 1 }
$BROWSERSTACK_JAR = $bsJar.FullName
Write-Host "Found BROWSERSTACK_JAR in local Maven repo: $BROWSERSTACK_JAR"
}
# Discover application JAR in common deployment locations and decide jar-mode early
$useJarMode = $false
$appJar = $null
$searchCandidates = @()
$searchCandidates += (Join-Path (Get-Location) 'dist\app.jar')
$searchCandidates += (Join-Path (Get-Location) 'app.jar')
# any jar-with-dependencies at repo root
$rootShaded = Get-ChildItem -Path (Get-Location) -Filter '*-jar-with-dependencies.jar' -File -ErrorAction SilentlyContinue | Select-Object -First 1
if ($rootShaded) { $searchCandidates += $rootShaded.FullName }
# shaded under target
$targetShaded = Get-ChildItem -Path (Join-Path (Get-Location) 'target') -Filter '*-jar-with-dependencies.jar' -File -ErrorAction SilentlyContinue | Select-Object -First 1
if ($targetShaded) { $searchCandidates += $targetShaded.FullName }
# pick first existing candidate
$appJar = $searchCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($appJar) {
Write-Host "Found application JAR candidate: $appJar"
# If there's no pom.xml (deployed package), use jar-mode and skip Maven
if (-not (Test-Path 'pom.xml')) {
$useJarMode = $true
$cpLine = $appJar
Write-Host "No pom.xml detected and application JAR present; switching to jar-mode and skipping mvn. cpLine=$cpLine"
} else {
Write-Host "pom.xml detected; will build explicit classpath via Maven unless overridden."
}
} else {
Write-Host "No application JAR found in deployment locations; script will attempt to build classpath via Maven (requires pom.xml)."
}
# ensure fresh cp.txt and build classpath only if not in jar-mode
if (-not $useJarMode) {
if (-not (Test-Path 'pom.xml')) {
Write-Error "No pom.xml found and no application JAR detected. The script requires either a shaded app JAR (dist/app.jar or target/*-jar-with-dependencies.jar) or a pom.xml to build the classpath."; exit 1
}
if (Test-Path cp.txt) { Remove-Item -Force cp.txt; Write-Host "Deleted previous cp.txt" }
# run mvn dependency:build-classpath safely using Start-Process
$mvnCmd = (Get-Command mvn -ErrorAction SilentlyContinue).Source
if (-not $mvnCmd) { Write-Error "mvn not found on PATH"; exit 1 }
Write-Host "Running: mvn dependency:build-classpath -Dmdep.outputFile=cp.txt"
$proc = Start-Process -FilePath $mvnCmd -ArgumentList 'dependency:build-classpath','-Dmdep.outputFile=cp.txt' -NoNewWindow -Wait -PassThru
if ($proc.ExitCode -ne 0) { Write-Error "mvn dependency:build-classpath failed with exit code $($proc.ExitCode)"; exit $proc.ExitCode }
if (-not (Test-Path cp.txt)) { Write-Error "cp.txt not created by Maven"; exit 1 }
$cp = Get-Content -Raw -Path cp.txt
Write-Host "--- cp.txt length: $($cp.Length) ---"
# use explicit mvn-produced classpath (match mac behavior) instead of wildcard to avoid ordering/duplicate issues
$cp = $cp.Trim()
$cpLine = "target\classes;$cp"
Write-Host "Using explicit classpath length: $($cpLine.Length)"
# warn if browserstack sdk appears multiple times on the classpath
$bsEntries = $cpLine -split ';' | Where-Object { $_ -match 'browserstack-java-sdk' }
if ($bsEntries.Count -gt 1) { Write-Host "WARNING: browserstack-java-sdk appears multiple times on classpath:`n$($bsEntries -join "`n")" }
} else {
Write-Host "Jar-mode active; using cpLine: $cpLine"
}
# create ASCII args.txt
$argsPath = Join-Path (Get-Location) 'args.txt'
if (Test-Path $argsPath) { Remove-Item -Force $argsPath }
$lines = @()
$lines += "-javaagent:`"$BROWSERSTACK_JAR`""
$lines += "-Dbrowserstack.config=`"$(Join-Path (Get-Location) 'browserstack.yml')`""
$lines += "-Dbrowserstack.framework=selenium"
$lines += "-Dbrowserstack.accessibility=true"
# temporary stability flag — remove when CLI fixed
#$lines += "-Dbrowserstack.disableCli=true"
$lines += "-Dcucumber.publish.quiet=true"
$lines += "-cp `"$cpLine`""
$lines += "com.browserstack.tests.RunCucumberTest"
Set-Content -Path $argsPath -Value $lines -Encoding ascii
Write-Host "Wrote $argsPath (len $((Get-Content $argsPath -Raw).Length))"
# run no-agent test to validate classpath & main
if (-not $SkipNoAgent) {
$noAgentPath = Join-Path (Get-Location) 'args-noagent.txt'
(Get-Content $argsPath) | Where-Object { $_ -notmatch '^-javaagent' } | Set-Content $noAgentPath -Encoding ascii
Write-Host "Running no-agent test... (logs -> run-noagent.log)"
# temporarily allow non-terminating native command failures so Java stderr doesn't stop the script
$oldErrorAction = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
if ($useJarMode) {
# run fat-jar on the classpath (avoids needing Main-Class in manifest)
Write-Host "Jar-mode: running app jar on -cp with main class (avoids missing Main-Class manifest)"
# Warn about possible duplicate SDK bundled inside shaded jar
Write-Host "NOTE: shaded jar may contain bundled dependencies (including BrowserStack SDK). If you see duplicate SLF4J bindings, rebuild the shaded jar excluding browserstack-java-sdk or remove duplicate copies from ~/.m2."
$noAgentArgs = @('-Dcucumber.publish.quiet=true','-cp', $appJar, 'com.browserstack.tests.RunCucumberTest')
} else {
$noAgentArgs = @('-cp', $cpLine, 'com.browserstack.tests.RunCucumberTest')
}
Write-Host "Invoking Java (no-agent) with args: $($noAgentArgs -join ' ')"
& $java @noAgentArgs 2>&1 | Tee-Object run-noagent.log
} catch [System.Exception] {
# log the exception but continue so we can inspect logs and exit code
Write-Host "Java process raised an exception: $($_.Exception.Message)"
} finally {
$ErrorActionPreference = $oldErrorAction
}
$noAgentExit = $LASTEXITCODE
Write-Host "no-agent exit code: $noAgentExit"
} else {
Write-Host "Skipping no-agent run (SkipNoAgent specified)"
}
# run agent-enabled JVM
Write-Host "Running agent-enabled JVM... (logs -> run-agent.log)"
# run directly with explicit -javaagent and classpath (keeps behavior consistent)
# temporarily allow non-terminating native command failures for the agent run as well
$oldErrorAction = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$argsConfigPath = (Join-Path (Get-Location) 'browserstack.yml')
# build explicit argument array to avoid PowerShell token-splitting issues
if ($useJarMode) {
$agentArgs = @(
"-javaagent:$BROWSERSTACK_JAR",
"-Dbrowserstack.config=$argsConfigPath",
"-Dbrowserstack.framework=selenium",
"-Dbrowserstack.accessibility=true",
"-Dcucumber.publish.quiet=true",
'-cp', $appJar,
'com.browserstack.tests.RunCucumberTest'
)
} else {
$agentArgs = @(
"-javaagent:$BROWSERSTACK_JAR",
"-Dbrowserstack.config=$argsConfigPath",
"-Dbrowserstack.framework=selenium",
"-Dbrowserstack.accessibility=true",
"-Dcucumber.publish.quiet=true",
'-cp', $cpLine,
'com.browserstack.tests.RunCucumberTest'
)
}
Write-Host "Invoking Java (agent) with args: $($agentArgs -join ' ')"
& $java @agentArgs 2>&1 | Tee-Object run-agent.log
} catch [System.Exception] {
Write-Host "Java agent run raised an exception: $($_.Exception.Message)"
} finally {
$ErrorActionPreference = $oldErrorAction
}
$agentExit = $LASTEXITCODE
Write-Host "agent exit code: $agentExit"
# helpful hints if agent fails due to CLI
if ($agentExit -ne 0) {
Write-Host "If you see SdkCLI initialization errors, try: (1) delete $env:USERPROFILE\\.browserstack\\cli to force re-download, or (2) keep -Dbrowserstack.disableCli=true as a temporary workaround."
}
exit $agentExit