Skip to content

Commit c762b4b

Browse files
committed
multi file logs + remove both option
1 parent 86e9a94 commit c762b4b

File tree

4 files changed

+99
-63
lines changed

4 files changed

+99
-63
lines changed

win/common-utils.ps1

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ $script:PROJECT_FOLDER = "NOW"
66

77
$script:GLOBAL_DIR = Join-Path $WORKSPACE_DIR $PROJECT_FOLDER
88
$script:LOG_DIR = Join-Path $GLOBAL_DIR "logs"
9-
$script:GLOBAL_LOG = Join-Path $LOG_DIR "global.log"
10-
$script:WEB_LOG = Join-Path $LOG_DIR "web_run_result.log"
11-
$script:MOBILE_LOG = Join-Path $LOG_DIR "mobile_run_result.log"
9+
$script:GLOBAL_LOG = ""
10+
$script:WEB_LOG = ""
11+
$script:MOBILE_LOG = ""
1212

1313
# Script state
1414
$script:BROWSERSTACK_USERNAME = ""
1515
$script:BROWSERSTACK_ACCESS_KEY = ""
16-
$script:TEST_TYPE = "" # Web / App / Both
16+
$script:TEST_TYPE = "" # Web / App
1717
$script:TECH_STACK = "" # Java / Python / JS
1818
[double]$script:PARALLEL_PERCENTAGE = 1.00
1919

@@ -58,10 +58,16 @@ function Clear-OldLogs {
5858
if (!(Test-Path $LOG_DIR)) {
5959
New-Item -ItemType Directory -Path $LOG_DIR | Out-Null
6060
}
61-
'' | Out-File -FilePath $GLOBAL_LOG -Encoding UTF8
62-
'' | Out-File -FilePath $WEB_LOG -Encoding UTF8
63-
'' | Out-File -FilePath $MOBILE_LOG -Encoding UTF8
64-
Log-Line "✅ Logs cleared and fresh run initiated." $GLOBAL_LOG
61+
62+
$legacyLogs = @("global.log","web_run_result.log","mobile_run_result.log")
63+
foreach ($legacy in $legacyLogs) {
64+
$legacyPath = Join-Path $LOG_DIR $legacy
65+
if (Test-Path $legacyPath) {
66+
Remove-Item -Path $legacyPath -Force -ErrorAction SilentlyContinue
67+
}
68+
}
69+
70+
Log-Line "✅ Logs directory cleaned. Legacy files removed." $GLOBAL_LOG
6571
}
6672

6773
# ===== Git Clone =====
@@ -304,39 +310,49 @@ function Get-BasicAuthHeader {
304310
function Fetch-Plan-Details {
305311
param([string]$TestType)
306312

307-
Log-Line "ℹ️ Fetching BrowserStack plan for $TestType" $GLOBAL_LOG
313+
if ([string]::IsNullOrWhiteSpace($TestType)) {
314+
throw "Test type is required to fetch plan details."
315+
}
316+
317+
$normalized = $TestType.ToLowerInvariant()
318+
Log-Line "ℹ️ Fetching BrowserStack plan for $normalized" $GLOBAL_LOG
319+
308320
$auth = Get-BasicAuthHeader -User $BROWSERSTACK_USERNAME -Key $BROWSERSTACK_ACCESS_KEY
309321
$headers = @{ Authorization = $auth }
310322

311-
if ($TestType -in @("Web","Both","web","both")) {
312-
try {
313-
$resp = Invoke-RestMethod -Method Get -Uri "https://api.browserstack.com/automate/plan.json" -Headers $headers
314-
$script:WEB_PLAN_FETCHED = $true
315-
$script:TEAM_PARALLELS_MAX_ALLOWED_WEB = [int]$resp.parallel_sessions_max_allowed
316-
Log-Line "✅ Web Testing Plan fetched: Team max parallel sessions = $TEAM_PARALLELS_MAX_ALLOWED_WEB" $GLOBAL_LOG
317-
} catch {
318-
Log-Line "❌ Web Testing Plan fetch failed ($($_.Exception.Message))" $GLOBAL_LOG
323+
switch ($normalized) {
324+
"web" {
325+
try {
326+
$resp = Invoke-RestMethod -Method Get -Uri "https://api.browserstack.com/automate/plan.json" -Headers $headers
327+
$script:WEB_PLAN_FETCHED = $true
328+
$script:TEAM_PARALLELS_MAX_ALLOWED_WEB = [int]$resp.parallel_sessions_max_allowed
329+
Log-Line "✅ Web Testing Plan fetched: Team max parallel sessions = $TEAM_PARALLELS_MAX_ALLOWED_WEB" $GLOBAL_LOG
330+
} catch {
331+
Log-Line "❌ Web Testing Plan fetch failed ($($_.Exception.Message))" $GLOBAL_LOG
332+
}
333+
if (-not $WEB_PLAN_FETCHED) {
334+
throw "Unable to fetch Web Testing plan details."
335+
}
319336
}
320-
}
321-
if ($TestType -in @("App","Both","app","both")) {
322-
try {
323-
$resp2 = Invoke-RestMethod -Method Get -Uri "https://api-cloud.browserstack.com/app-automate/plan.json" -Headers $headers
324-
$script:MOBILE_PLAN_FETCHED = $true
325-
$script:TEAM_PARALLELS_MAX_ALLOWED_MOBILE = [int]$resp2.parallel_sessions_max_allowed
326-
Log-Line "✅ Mobile App Testing Plan fetched: Team max parallel sessions = $TEAM_PARALLELS_MAX_ALLOWED_MOBILE" $GLOBAL_LOG
327-
} catch {
328-
Log-Line "❌ Mobile App Testing Plan fetch failed ($($_.Exception.Message))" $GLOBAL_LOG
337+
"app" {
338+
try {
339+
$resp2 = Invoke-RestMethod -Method Get -Uri "https://api-cloud.browserstack.com/app-automate/plan.json" -Headers $headers
340+
$script:MOBILE_PLAN_FETCHED = $true
341+
$script:TEAM_PARALLELS_MAX_ALLOWED_MOBILE = [int]$resp2.parallel_sessions_max_allowed
342+
Log-Line "✅ Mobile App Testing Plan fetched: Team max parallel sessions = $TEAM_PARALLELS_MAX_ALLOWED_MOBILE" $GLOBAL_LOG
343+
} catch {
344+
Log-Line "❌ Mobile App Testing Plan fetch failed ($($_.Exception.Message))" $GLOBAL_LOG
345+
}
346+
if (-not $MOBILE_PLAN_FETCHED) {
347+
throw "Unable to fetch Mobile App Testing plan details."
348+
}
349+
}
350+
default {
351+
throw "Unsupported TEST_TYPE: $TestType. Allowed values: Web, App."
329352
}
330353
}
331354

332-
if ( ($TestType -match "^Web$|^web$" -and -not $WEB_PLAN_FETCHED) -or
333-
($TestType -match "^App$|^app$" -and -not $MOBILE_PLAN_FETCHED) -or
334-
($TestType -match "^Both$|^both$" -and -not ($WEB_PLAN_FETCHED -or $MOBILE_PLAN_FETCHED)) ) {
335-
Log-Line "❌ Unauthorized to fetch required plan(s) or failed request(s). Exiting." $GLOBAL_LOG
336-
throw "Plan fetch failed"
337-
}
338-
339-
Log-Line "ℹ️ Plan summary: Web $WEB_PLAN_FETCHED ($TEAM_PARALLELS_MAX_ALLOWED_WEB max), Mobile $MOBILE_PLAN_FETCHED ($TEAM_PARALLELS_MAX_ALLOWED_MOBILE max)" $GLOBAL_LOG
355+
Log-Line "ℹ️ Plan summary: Web fetched=$WEB_PLAN_FETCHED (team max=$TEAM_PARALLELS_MAX_ALLOWED_WEB), Mobile fetched=$MOBILE_PLAN_FETCHED (team max=$TEAM_PARALLELS_MAX_ALLOWED_MOBILE)" $GLOBAL_LOG
340356
}
341357

342358

win/env-setup-run.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Setup-Web-Java {
1414
}
1515

1616
Log-Line "ℹ️ Cloning repository: $REPO" $GLOBAL_LOG
17-
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile $WEB_LOG
17+
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile (Get-RunLogFile)
1818

1919
Push-Location $TARGET
2020
try {
@@ -86,7 +86,7 @@ function Setup-Web-Python {
8686
}
8787

8888
Log-Line "ℹ️ Cloning repository: $REPO" $GLOBAL_LOG
89-
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile $WEB_LOG
89+
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile (Get-RunLogFile)
9090

9191
Push-Location $TARGET
9292
try {
@@ -165,7 +165,7 @@ function Setup-Web-NodeJS {
165165
New-Item -ItemType Directory -Path $GLOBAL_DIR -Force | Out-Null
166166

167167
Log-Line "ℹ️ Cloning repository: $REPO" $GLOBAL_LOG
168-
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile $WEB_LOG
168+
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile (Get-RunLogFile)
169169

170170
Push-Location $TARGET
171171
try {
@@ -225,7 +225,7 @@ function Setup-Mobile-Java {
225225
}
226226

227227
Log-Line "ℹ️ Cloning repository: $REPO" $GLOBAL_LOG
228-
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile $MOBILE_LOG
228+
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile (Get-RunLogFile)
229229

230230
Push-Location $TARGET
231231
try {
@@ -302,7 +302,7 @@ function Setup-Mobile-Python {
302302
}
303303

304304
Log-Line "ℹ️ Cloning repository: $REPO" $GLOBAL_LOG
305-
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile $MOBILE_LOG
305+
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile (Get-RunLogFile)
306306

307307
Push-Location $TARGET
308308
try {
@@ -407,7 +407,7 @@ function Setup-Mobile-NodeJS {
407407
}
408408

409409
Log-Line "ℹ️ Cloning repository: $REPO" $GLOBAL_LOG
410-
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile $MOBILE_LOG
410+
Invoke-GitClone -Url "https://github.com/BrowserStackCE/$REPO.git" -Target $TARGET -LogFile (Get-RunLogFile)
411411

412412
$testDir = Join-Path $TARGET "test"
413413
Push-Location $testDir
@@ -525,7 +525,7 @@ function Setup-Environment {
525525
Log-Line "Total parallels allocated: $totalParallels" $GLOBAL_LOG
526526

527527
$success = $false
528-
$logFile = if ($SetupType -match "web") { $WEB_LOG } else { $MOBILE_LOG }
528+
$logFile = Get-RunLogFile
529529

530530
switch ($TechStack) {
531531
"Java" {

win/run.ps1

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,41 @@ $script:PSScriptRootResolved = Split-Path -Parent $MyInvocation.MyCommand.Path
3232

3333
# ===== Main flow (baseline steps then run) =====
3434
try {
35-
# Setup Summary Header
36-
Log-Section "🧭 Setup Summary – BrowserStack NOW" $GLOBAL_LOG
37-
Log-Line "ℹ️ Timestamp: $((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))" $GLOBAL_LOG
38-
39-
# Get test type and tech stack FIRST
35+
# Get test type and tech stack before logging
4036
if ($RunMode -match "--silent|--debug") {
41-
$script:TEST_TYPE = if ($TT) { (Get-Culture).TextInfo.ToTitleCase($TT.ToLowerInvariant()) } else { $env:TEST_TYPE }
42-
$script:TECH_STACK = if ($TSTACK) { (Get-Culture).TextInfo.ToTitleCase($TSTACK.ToLowerInvariant()) } else { $env:TECH_STACK }
43-
Log-Line "ℹ️ Run Mode: $RunMode" $GLOBAL_LOG
37+
$textInfo = (Get-Culture).TextInfo
38+
$ttCandidate = if ($TT) { $TT } else { $env:TEST_TYPE }
39+
if ([string]::IsNullOrWhiteSpace($ttCandidate)) { throw "TEST_TYPE is required in silent/debug mode." }
40+
$tsCandidate = if ($TSTACK) { $TSTACK } else { $env:TECH_STACK }
41+
if ([string]::IsNullOrWhiteSpace($tsCandidate)) { throw "TECH_STACK is required in silent/debug mode." }
42+
$script:TEST_TYPE = $textInfo.ToTitleCase($ttCandidate.ToLowerInvariant())
43+
$script:TECH_STACK = $textInfo.ToTitleCase($tsCandidate.ToLowerInvariant())
44+
if ($TEST_TYPE -notin @("Web","App")) { throw "TEST_TYPE must be either 'Web' or 'App'." }
45+
if ($TECH_STACK -notin @("Java","Python","NodeJS")) { throw "TECH_STACK must be one of: Java, Python, NodeJS." }
4446
} else {
4547
Resolve-Test-Type -RunMode $RunMode -CliValue $TT
4648
Resolve-Tech-Stack -RunMode $RunMode -CliValue $TSTACK
4749
}
4850

49-
# Setup log file path
50-
$logFile = Join-Path $LOG_DIR ("{0}_{1}_run_result.log" -f $TEST_TYPE.ToLowerInvariant(), $TECH_STACK.ToLowerInvariant())
51-
Log-Line "ℹ️ Log file path: $logFile" $GLOBAL_LOG
51+
# Setup log file path AFTER selections
52+
$logFileName = "{0}_{1}_run_result.log" -f $TEST_TYPE.ToLowerInvariant(), $TECH_STACK.ToLowerInvariant()
53+
$logFile = Join-Path $LOG_DIR $logFileName
54+
if (!(Test-Path $LOG_DIR)) {
55+
New-Item -ItemType Directory -Path $LOG_DIR -Force | Out-Null
56+
}
57+
'' | Out-File -FilePath $logFile -Encoding UTF8
5258
Set-RunLogFile $logFile
59+
$script:GLOBAL_LOG = $logFile
60+
$script:WEB_LOG = $logFile
61+
$script:MOBILE_LOG = $logFile
62+
Log-Line "ℹ️ Log file path: $logFile" $GLOBAL_LOG
63+
64+
# Setup Summary Header
65+
Log-Section "🧭 Setup Summary – BrowserStack NOW" $GLOBAL_LOG
66+
Log-Line "ℹ️ Timestamp: $((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))" $GLOBAL_LOG
67+
Log-Line "ℹ️ Run Mode: $RunMode" $GLOBAL_LOG
68+
Log-Line "ℹ️ Selected Testing Type: $TEST_TYPE" $GLOBAL_LOG
69+
Log-Line "ℹ️ Selected Tech Stack: $TECH_STACK" $GLOBAL_LOG
5370

5471
# Setup workspace and get credentials BEFORE app upload
5572
Setup-Workspace
@@ -93,9 +110,7 @@ try {
93110
Log-Line "========================================" $GLOBAL_LOG
94111
Log-Line "Error: $($_.Exception.Message)" $GLOBAL_LOG
95112
Log-Line "Check logs for details:" $GLOBAL_LOG
96-
Log-Line " Global: $GLOBAL_LOG" $GLOBAL_LOG
97-
Log-Line " Web: $WEB_LOG" $GLOBAL_LOG
98-
Log-Line " Mobile: $MOBILE_LOG" $GLOBAL_LOG
113+
Log-Line (" Run Log: {0}" -f (Get-RunLogFile)) $GLOBAL_LOG
99114
Log-Line "========================================" $GLOBAL_LOG
100115
throw
101116
}

win/user-interaction.ps1

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,20 @@ function Resolve-Test-Type {
187187
if ($RunMode -match "--silent" -or $RunMode -match "--debug") {
188188
if (-not $CliValue) { $CliValue = $env:TEST_TYPE }
189189
if ([string]::IsNullOrWhiteSpace($CliValue)) { throw "TEST_TYPE is required in silent/debug mode." }
190-
$script:TEST_TYPE = (Get-Culture).TextInfo.ToTitleCase($CliValue.ToLowerInvariant())
190+
$candidate = (Get-Culture).TextInfo.ToTitleCase($CliValue.ToLowerInvariant())
191+
if ($candidate -notin @("Web","App")) {
192+
throw "TEST_TYPE must be either 'Web' or 'App'."
193+
}
194+
$script:TEST_TYPE = $candidate
191195
return
192196
}
193197

194198
$choice = Show-ClickChoice -Title "Testing Type" `
195199
-Prompt "What do you want to run?" `
196-
-Choices @("Web","App","Both") `
200+
-Choices @("Web","App") `
197201
-DefaultChoice "Web"
198202
if ([string]::IsNullOrWhiteSpace($choice)) { throw "No testing type selected" }
199203
$script:TEST_TYPE = $choice
200-
Log-Line "✅ Selected Testing Type: $script:TEST_TYPE" $GLOBAL_LOG
201204
}
202205

203206
function Resolve-Tech-Stack {
@@ -209,7 +212,11 @@ function Resolve-Tech-Stack {
209212
if (-not $CliValue) { $CliValue = $env:TECH_STACK }
210213
if ([string]::IsNullOrWhiteSpace($CliValue)) { throw "TECH_STACK is required in silent/debug mode." }
211214
$textInfo = (Get-Culture).TextInfo
212-
$script:TECH_STACK = $textInfo.ToTitleCase($CliValue.ToLowerInvariant())
215+
$candidate = $textInfo.ToTitleCase($CliValue.ToLowerInvariant())
216+
if ($candidate -notin @("Java","Python","NodeJS")) {
217+
throw "TECH_STACK must be one of: Java, Python, NodeJS."
218+
}
219+
$script:TECH_STACK = $candidate
213220
return
214221
}
215222

@@ -219,7 +226,6 @@ function Resolve-Tech-Stack {
219226
-DefaultChoice "Java"
220227
if ([string]::IsNullOrWhiteSpace($choice)) { throw "No tech stack selected" }
221228
$script:TECH_STACK = $choice
222-
Log-Line "✅ Selected Tech Stack: $script:TECH_STACK" $GLOBAL_LOG
223229
}
224230

225231
function Ask-User-TestUrl {
@@ -368,9 +374,8 @@ function Perform-NextSteps-BasedOnTestType {
368374
"^App$|^app$" {
369375
Ask-And-Upload-App -RunMode $RunMode -CliPath $AppPath -CliPlatform $AppPlatform
370376
}
371-
"^Both$|^both$" {
372-
Ask-User-TestUrl -RunMode $RunMode -CliValue $TestUrl
373-
Ask-And-Upload-App -RunMode $RunMode -CliPath $AppPath -CliPlatform $AppPlatform
377+
default {
378+
throw "Unsupported TEST_TYPE: $TestType. Allowed values: Web, App."
374379
}
375380
}
376381
}

0 commit comments

Comments
 (0)