-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathRunTests.ps1
More file actions
920 lines (767 loc) · 39.6 KB
/
RunTests.ps1
File metadata and controls
920 lines (767 loc) · 39.6 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Script: RunTests.ps1
# This script runs tests for Copilot Studio Kit applications.
# It generates an Azure DevOps style HTML report by default
#
# Usage examples:
# .\RunTests.ps1 # Run all tests with Azure DevOps style report
# .\RunTests.ps1 -entityFilter "account" # Run only tests related to the account entity
# .\RunTests.ps1 -pageTypeFilter "entitylist" # Run only list view tests
# .\RunTests.ps1 -pageTypeFilter "entityrecord" # Run only details view tests
# .\RunTests.ps1 -pageTypeFilter "custom" # Run only custom page tests
# .\RunTests.ps1 -pageTypeFilter "entitylist","entityrecord" # Run both list and details tests (no custom pages)
# .\RunTests.ps1 -pageTypeFilter @("entitylist","custom") # Run list and custom page tests (no details)
# .\RunTests.ps1 -customPageFilter "dashboard" # Run only custom pages with "dashboard" in the name
# .\RunTests.ps1 -startTime "2025-05-20 09:00" # Run tests and show results since 2025-05-20 09:00
# .\RunTests.ps1 -startTime "2025-05-20 09:00" -endTime "2025-05-24 09:00" # Generate report from existing test runs between the specified dates without executing tests
# .\RunTests.ps1 -testEngineBranch "feature/my-branch" # Use a specific branch of PowerApps-TestEngine
# .\RunTests.ps1 -generateReportOnly # Generate report from existing test data without running tests
#
# Multiple filters can be combined:
# .\RunTests.ps1 -entityFilter "account" -pageTypeFilter "entityrecord"
# Check for optional command line arguments
param (
[string]$startTime, # Start time for the test results to include in the report
[string]$endTime, # End time for the test results to include in the report (when both startTime and endTime are provided, tests are not executed)
[string]$entityFilter, # Filter tests by entity name
[string[]]$pageTypeFilter, # Filter by page type(s) (list, details, custom) - can be multiple values
[string]$customPageFilter, # Filter by custom page name
[string]$testEngineBranch = "user/grant-archibald-ms/report-594", # Optional branch to use for PowerApps-TestEngine
[switch]$forceRebuild, # Force rebuild of PowerApps-TestEngine even if it exists
[switch]$generateReportOnly, # Only generate a report without running tests
[switch]$useStaticContext = $false, # Use static context for test execution
[switch]$usePacTest = $true, # Use 'pac test run' instead of direct PowerAppsTestEngine.dll execution
[switch]$ShowSensitiveValues = $false # Show actual values for sensitive data like email, tenant ID, environment ID
# (by default these are masked with ***)
)
# Helper function to mask sensitive values for screen recording safety
function Hide-SensitiveValue {
param(
[string]$Value,
[int]$VisibleChars = 4
)
if ([string]::IsNullOrEmpty($Value)) {
return $Value
}
# If ShowSensitiveValues is enabled, return the value as-is
if ($ShowSensitiveValues) {
return $Value
}
# For URLs, show protocol and domain structure but mask the subdomain
if ($Value -match '^(https?://)([^\.]+)\.(.+)$') {
$protocol = $matches[1] # e.g., "https://"
$subdomain = $matches[2] # the subdomain part to mask
$domain = $matches[3] # e.g., "dynamics.crm.com"
return "$protocol****.$domain"
}
# For file paths containing Users\username, mask the username
if ($Value -match '([A-Za-z]:\\Users\\)([^\\]+)(\\.*)?') {
$prefix = $matches[1] # e.g., "C:\Users\"
$username = $matches[2] # the username part
$suffix = $matches[3] # the rest of the path (if any)
if ([string]::IsNullOrEmpty($suffix)) {
$suffix = ""
}
return "$prefix***$suffix"
}
# For emails, show first few chars and domain
if ($Value -match '^[^@]+@[^@]+$') {
$parts = $Value.Split('@')
$username = $parts[0]
$domain = $parts[1]
if ($username.Length -le $VisibleChars) {
return "***@$domain"
} else {
return "$($username.Substring(0, $VisibleChars))***@$domain"
}
}
# Check if it's a GUID format (8-4-4-4-12 characters separated by hyphens)
if ($Value -match '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$') {
# For GUIDs, show the first section and mask the rest
$parts = $Value.Split('-')
$firstSection = $parts[0]
$maskedParts = @($firstSection)
# Mask remaining sections by replacing each alphanumeric character with *
for ($i = 1; $i -lt $parts.Length; $i++) {
$maskedSection = $parts[$i] -replace '[0-9a-fA-F]', '*'
$maskedParts += $maskedSection
}
return $maskedParts -join '-'
}
# Check if the value is a username (like from $env:username)
# This handles cases where the username appears directly as a value
if (-not [string]::IsNullOrEmpty($env:username) -and $Value -eq $env:username) {
return "***"
}
# Also check for common username patterns (alphanumeric, possibly with dots, hyphens, underscores)
# but exclude obvious non-usernames like GUIDs, URLs, file extensions
if ($Value -match '^[a-zA-Z][a-zA-Z0-9\.\-_]{2,19}$' -and
$Value -notmatch '\.' -and # Exclude things that look like file extensions or domains
$Value -notmatch '^https?:' -and # Exclude URLs
$Value -notmatch '[0-9a-fA-F]{8,}') { # Exclude long hex strings
return "***"
}
# For other long values, show first few chars
if ($Value.Length -le $VisibleChars) {
return "***"
} else {
return "$($Value.Substring(0, $VisibleChars))***"
}
}
# Function to execute a test, either using PowerAppsTestEngine.dll directly or pac test run
# Note: Report generation is always handled by PowerAppsTestEngine.dll regardless of execution mode
function Execute-Test {
param(
[string]$testScriptPath, # Path to the test script file
[string]$targetUrl, # URL to test
[string]$logLevel = "Debug", # Log level
[switch]$useStaticContextArg # Whether to use static context
)
$staticContextArgValue = if ($useStaticContextArg) { "TRUE" } else { "FALSE" }
$debugTestArgValue = if ($debugTests) { "TRUE" } else { "FALSE" }
if ($usePacTest) {
# Use pac test run command
Write-Host "Running test using pac test run..." -ForegroundColor Green
# Build the pac test run command
$pacArgs = @(
"test", "run",
"--test-plan-file", "$testScriptPath",
"--domain", "$targetUrl",
"--tenant", $tenantId,
"--environment-id", $environmentId,
"--provider", "mda"
)
# Add optional arguments
if ($useStaticContextArg) {
$pacArgs += "--use-static-context"
}
if ($debugTests) {
$pacArgs += "--debug"
} # Note: We won't use --run-id with pac test, as we'll update the generated TRX files later
# This gives us more control over the test result processing
# Create masked versions for logging
$maskedTenantId = Hide-SensitiveValue -Value $tenantId
$maskedEnvironmentId = Hide-SensitiveValue -Value $environmentId
$maskedTargetUrl = Hide-SensitiveValue -Value $targetUrl
$maskedTestScriptPath = Hide-SensitiveValue -Value $testScriptPath
# Log the command being executed (with masked sensitive values)
$maskedPacArgs = @(
"test", "run",
"--test-plan-file", "$maskedTestScriptPath",
"--domain", "$maskedTargetUrl",
"--tenant", $maskedTenantId,
"--environment-id", $maskedEnvironmentId,
"--provider", "mda"
)
if ($useStaticContextArg) {
$maskedPacArgs += "--use-static-context"
}
if ($debugTests) {
$maskedPacArgs += "--debug"
}
Write-Host "Executing: pac $($maskedPacArgs -join ' ')" -ForegroundColor DarkGray
# Execute pac command
& pac $pacArgs
if ($LASTEXITCODE -ne 0) {
Write-Host "Test execution failed with exit code: $LASTEXITCODE" -ForegroundColor Red
}
}
else {
# Use PowerAppsTestEngine.dll directly
Write-Host "Running test using PowerAppsTestEngine.dll directly..." -ForegroundColor Green
# Navigate to the test engine directory
Push-Location $testEnginePath
try {
# Create masked versions for logging
$maskedTenantId = Hide-SensitiveValue -Value $tenantId
$maskedEnvironmentId = Hide-SensitiveValue -Value $environmentId
$maskedTargetUrl = Hide-SensitiveValue -Value $targetUrl
$maskedTestScriptPath = Hide-SensitiveValue -Value $testScriptPath
# Log the command being executed (with masked sensitive values)
Write-Host "Executing: dotnet PowerAppsTestEngine.dll -c `"$staticContextArgValue`" -w `"$debugTestArgValue`" -u `"$userAuth`" -a `"$authType`" -p `"mda`" -a `"none`" -i `"$maskedTestScriptPath`" -t $maskedTenantId -e $maskedEnvironmentId -d `"$maskedTargetUrl`" -l `"$logLevel`" --run-name $runName" -ForegroundColor DarkGray
# Execute the test using dotnet command
dotnet PowerAppsTestEngine.dll -c "$staticContextArgValue" -w "$debugTestArgValue" -u "$userAuth" -a "$authType" -p "mda" -a "none" -i "$testScriptPath" -t $tenantId -e $environmentId -d "$targetUrl" -l "$logLevel" --run-name $runName
if ($LASTEXITCODE -ne 0) {
Write-Host "Test execution failed with exit code: $LASTEXITCODE" -ForegroundColor Red
}
}
finally {
Pop-Location
}
}
}
$runName = [Guid]::NewGuid().Guid.ToString()
# Get current directory so we can reset back to it after running the tests
$currentDirectory = Get-Location
# Define the PowerApps Test Engine repository information
$testEngineRepoUrl = "https://github.com/microsoft/PowerApps-TestEngine"
$testEngineDirectory = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerApps-TestEngine"
$testEngineBuildDir = Join-Path -Path $testEngineDirectory -ChildPath "src"
$testEngineBinDir = Join-Path -Path $testEngineDirectory -ChildPath "bin\Debug\PowerAppsTestEngine"
# Function to check if current directory is part of PowerApps-TestEngine
function Test-IsInTestEngineRepo {
try {
# Get the git root directory
$gitRootDir = git rev-parse --show-toplevel 2>$null
if ($LASTEXITCODE -ne 0) {
# Not in a git repo
return $false
}
# Check if the directory name indicates it's the PowerApps-TestEngine repo
$dirName = Split-Path -Path $gitRootDir -Leaf
if ($dirName -eq "PowerApps-TestEngine") {
return $true
}
# Check remote URLs for PowerApps-TestEngine
$remotes = git remote -v 2>$null
foreach ($remote in $remotes) {
if ($remote -like "*PowerApps-TestEngine*") {
return $true
}
}
return $false
}
catch {
return $false
}
}
# Function to setup the PowerApps Test Engine
# Function to build the PowerApps Test Engine
function Build-TestEngine {
param(
[string]$srcDir, # Source directory containing the code to build
[string]$message = "Building PowerApps Test Engine..." # Custom message for build
)
Write-Host $message -ForegroundColor Green
# Navigate to the src directory
Push-Location $srcDir
try {
# Build the Test Engine
dotnet build
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to build PowerApps Test Engine. Please check the build logs."
exit 1
}
Write-Host "PowerApps Test Engine built successfully!" -ForegroundColor Green
return $true
}
finally {
Pop-Location # Return from src directory
}
}
# Function to verify the PowerApps Test Engine binary exists
function Test-TestEngineBinary {
param(
[string]$binDir # Directory where the binary should exist
)
$dllPath = Join-Path -Path $binDir -ChildPath "PowerAppsTestEngine.dll"
if (-not (Test-Path -Path $dllPath)) {
Write-Error "PowerAppsTestEngine.dll not found at $dllPath. Please check the build process."
return $false
}
$maskedDllPath = Hide-SensitiveValue -Value $dllPath
Write-Host "Found PowerAppsTestEngine.dll at $maskedDllPath" -ForegroundColor Green
return $true
}
# Function to setup the PowerApps Test Engine
function Setup-TestEngine {
# Check if we're already in the PowerApps-TestEngine repository
$isInTestEngineRepo = Test-IsInTestEngineRepo
if ($isInTestEngineRepo) {
Write-Host "Detected current directory is part of PowerApps-TestEngine repository" -ForegroundColor Green
# Get the root directory of the repository
$repoRootDir = git rev-parse --show-toplevel 2>$null
# Use paths relative to the repository root
$relativeSrcDir = Join-Path -Path $repoRootDir -ChildPath "src"
$relativeBinDir = Join-Path -Path $repoRootDir -ChildPath "bin\Debug\PowerAppsTestEngine"
$binDebugDir = Join-Path -Path $repoRootDir -ChildPath "bin\Debug"
Write-Host "Using repository root directory: $(Hide-SensitiveValue $repoRootDir)" -ForegroundColor Green
# Check if build is needed
$needsBuild = $false
# Check if the bin\Debug directory exists
if (-not (Test-Path -Path $binDebugDir)) {
Write-Host "bin\Debug directory doesn't exist. Building the project..." -ForegroundColor Yellow
$needsBuild = $true
}
# Check if the PowerAppsTestEngine.dll exists
elseif (-not (Test-Path -Path "$relativeBinDir\PowerAppsTestEngine.dll")) {
Write-Host "PowerAppsTestEngine.dll not found. Building the project..." -ForegroundColor Yellow
$needsBuild = $true
}
# Honor forceRebuild if specified
elseif ($forceRebuild) {
Write-Host "Force rebuild requested. Building the project..." -ForegroundColor Yellow
$needsBuild = $true
} else {
Write-Host "Using existing build in $(Hide-SensitiveValue $relativeBinDir)" -ForegroundColor Green
}
# Build if needed
if ($needsBuild) {
Build-TestEngine -srcDir $relativeSrcDir -message "Building PowerApps Test Engine from local source..."
} # Verify binary exists
if (Test-TestEngineBinary -binDir $relativeBinDir) {
Write-Host "Binary verified at $(Hide-SensitiveValue $relativeBinDir)" -ForegroundColor Green
return $relativeBinDir
} else {
Write-Error "Failed to verify binary at $relativeBinDir"
exit 1
}
}
else {
# Check if the PowerApps-TestEngine directory exists
if (-not (Test-Path -Path $testEngineDirectory) -or $forceRebuild) {
Write-Host "Setting up PowerApps Test Engine..." -ForegroundColor Cyan
# Remove existing directory if it exists
if (Test-Path -Path $testEngineDirectory) {
Write-Host "Get latest changes..." -ForegroundColor Yellow
Set-Location $testEngineDirectory
git pull
} else { # Clone the repository
Write-Host "Cloning PowerApps Test Engine repository from $(Hide-SensitiveValue $testEngineRepoUrl)..." -ForegroundColor Green
git clone "$testEngineRepoUrl" "$testEngineDirectory"
}
# Navigate to the repository directory
Push-Location $testEngineDirectory
try {
# Check if a specific branch was specified
if ($testEngineBranch -ne "main") {
Write-Host "Switching to branch: $testEngineBranch" -ForegroundColor Green
git checkout $testEngineBranch
# Check if checkout was successful
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to switch to branch $testEngineBranch. Using main branch instead." -ForegroundColor Yellow
git checkout main
}
}
# Build the Test Engine using shared function
Build-TestEngine -srcDir $testEngineBuildDir
}
finally {
Pop-Location # Return from repository directory
}
} else {
Write-Host "PowerApps Test Engine directory already exists at $(Hide-SensitiveValue $testEngineDirectory)" -ForegroundColor Green
} # Verify binary exists
if (Test-TestEngineBinary -binDir $testEngineBinDir) {
Write-Host "Binary verified at $(Hide-SensitiveValue $testEngineBinDir)" -ForegroundColor Green
return $testEngineBinDir
} else {
Write-Error "Failed to verify binary at $testEngineBinDir"
exit 1
}
}
}
# This line was redundant, as we call Setup-TestEngine below
# Set up the Test Engine and get the path to the built binary
$testEnginePath = Setup-TestEngine
if ($testEnginePath -is [array]) {
Write-Host "Converting array to string for testEnginePath" -ForegroundColor Yellow
$testEnginePath = $testEnginePath[0]
}
Write-Host "Test Engine Path: $(Hide-SensitiveValue $testEnginePath)" -ForegroundColor Green
Set-Location $currentDirectory
$jsonContent = Get-Content -Path .\config.json -Raw
$config = $jsonContent | ConvertFrom-Json
$tenantId = $config.tenantId
$environmentId = $config.environmentId
$user1Email = $config.user1Email
$record = $config.record
# Extract pages and corresponding Test Scripts
$customPages = $config.pages.customPages
$entities = $config.pages.entities
$testScripts = $config.testScripts
# Initialize $runTests - default to true if not specified in config or overridden by testRunTime
$runTests = if ([bool]::TryParse($config.runTests, [ref]$null)) { [bool]$config.runTests } else { $true }
# Check if useStaticContext parameter was provided, otherwise get it from config
if (-not $PSBoundParameters.ContainsKey('useStaticContext')) {
$useStaticContext = if ($null -eq $config.useStaticContext) { $false } else { $config.useStaticContext }
}
# Otherwise, the useStaticContext parameter value will be used (already set from param block)
$appName = $config.appName
$debugTests = $config.debugTests
$userAuth = $config.userAuth
$authType = "default"
$environmentUrl = $config.environmentUrl
if ([string]::IsNullOrEmpty($userAuth)) {
$userAuth = "storagestate"
}
if ($userAuth -eq "dataverse") {
$authType = "storagestate"
}
# Define the folder paths for test outputs
$testEngineBasePath = "$env:USERPROFILE\AppData\Local\Temp\Microsoft\TestEngine"
$folderPath = "$testEngineBasePath\TestOutput"
$extraArgs = ""
$debugTestValue = "FALSE"
$staticContext = "FALSE"
# Check if useStaticContext is true and set staticContext accordingly
if ($useStaticContext -eq $true) {
Write-Host "Using static context: TRUE" -ForegroundColor Green
$staticContext = "TRUE"
} else {
Write-Host "Using static context: FALSE" -ForegroundColor Green
$staticContext = "FALSE"
}
# Display execution mode
if ($usePacTest) {
Write-Host "Test execution mode: pac test run (Power Platform CLI)" -ForegroundColor Cyan
Write-Host "Report generation will still use PowerAppsTestEngine.dll" -ForegroundColor Cyan
} else {
Write-Host "Test execution mode: PowerAppsTestEngine.dll (direct)" -ForegroundColor Cyan
}
if ($debugTests) {
$debugTestValue = "TRUE"
}
if ($getLatest) {
git pull
}
# Define start and end times for test reporting
$startTimeThreshold = $null
$endTimeThreshold = $null
# Process startTime parameter
if ($startTime) {
try { # Try parsing with multiple possible formats
try {
$startTimeThreshold = [DateTime]::ParseExact($startTime, "yyyy-MM-dd HH:mm", [System.Globalization.CultureInfo]::InvariantCulture)
# Format successfully parsed
} catch {
# Try general parsing as fallback
try {
$startTimeThreshold = [DateTime]::Parse($startTime)
} catch {
throw "Could not parse the startTime format"
}
}
Write-Host "Including test results from after $startTime" -ForegroundColor Yellow
} catch {
Write-Error "Invalid startTime format. Please use 'yyyy-MM-dd HH:mm'. Error: $($_.Exception.Message)"
return
}
} else {
# Default: use current time as the start time
$startTimeThreshold = Get-Date
Write-Host "No start time provided. Using current time: $($startTimeThreshold.ToString('yyyy-MM-dd HH:mm'))" -ForegroundColor Yellow
}
# Process endTime parameter
if ($endTime) {
try { # Try parsing with multiple possible formats
try {
$endTimeThreshold = [DateTime]::ParseExact($endTime, "yyyy-MM-dd HH:mm", [System.Globalization.CultureInfo]::InvariantCulture)
# Format successfully parsed
} catch {
# Try general parsing as fallback
try {
$endTimeThreshold = [DateTime]::Parse($endTime)
} catch {
throw "Could not parse the endTime format"
}
}
Write-Host "Including test results until $endTime" -ForegroundColor Yellow
} catch {
Write-Error "Invalid endTime format. Please use 'yyyy-MM-dd HH:mm'. Error: $($_.Exception.Message)"
return
}
} else {
# Default: use current time as the end time, will be updated after tests run
$endTimeThreshold = Get-Date
# Store the original parameter state to know if it was explicitly provided
$endTimeProvided = $false
}
# Decide whether to run tests or just generate a report
if ($generateReportOnly -or ($startTime -and $endTime)) {
# Don't run tests if generateReportOnly is specified or both start and end times are provided
$runTests = $false
if ($generateReportOnly) {
Write-Host "Generating report only from existing test results" -ForegroundColor Yellow
# If no specific time range was provided with -generateReportOnly, use a wide default range
if (-not $startTime) {
$startTimeThreshold = (Get-Date).AddDays(-7) # Default to last 7 days if no start time provided
Write-Host "Using default time range: last 7 days" -ForegroundColor Yellow
}
} else {
Write-Host "Generating report from existing test results between $startTime and $endTime" -ForegroundColor Yellow
}
Write-Host "Tests will not be executed" -ForegroundColor Yellow
} else {
# Keep runTests as initialized earlier (from config or default to true)
# Since we now default startTime to current time when not provided,
# we want to make it clear we're running tests from now
if (-not $startTime) {
Write-Host "Will run tests and include results from current run only" -ForegroundColor Yellow
}
}
if ([string]::IsNullOrEmpty($environmentId)) {
Write-Error "Environment not configured. Please update config.json" -ForegroundColor Red
return
}
$azTenantId = az account show --query tenantId --output tsv
if ($azTenantId -ne $tenantId) {
Write-Error "Tenant ID mismatch. Please check your Azure CLI context." -ForegroundColor Red
return
}
$token = (az account get-access-token --resource $environmentUrl | ConvertFrom-Json)
if ($token -eq $null) {
Write-Error "Failed to obtain access token. Please check your Azure CLI context."
return
}
$appId = ""
$lookup = "$environmentUrl/api/data/v9.2/appmodules?`$filter=name eq '$appName'`&`$select=appmoduleid"
$appResponse = Invoke-RestMethod -Uri $lookup -Method Get -Headers @{Authorization = "Bearer $($token.accessToken)"}
$appId = $appResponse.value.appmoduleid
$lookupApp = "$environmentUrl/api/data/v9.2/appmodules($appId)"
$appInfo = Invoke-RestMethod -Uri $lookupApp -Method Get -Headers @{Authorization = "Bearer $($token.accessToken)"}
$appDescriptor = $appInfo.descriptor | ConvertFrom-Json
$appEntities = $appDescriptor.appInfo.AppComponents.Entities | Measure-Object | Select-Object -ExpandProperty Count;
if ($runTests)
{
$appTotal = ($appDescriptor.appInfo.AppElements.Count + ($appEntities * 2))
if ([string]::IsNullOrEmpty($appId)) {
Write-Error "App id not found. Check that the Copilot Studio Kit has been installed"
return
}
if ($config.installPlaywright) {
# Get the absolute path to playwright.ps1
$playwrightScriptPath = Join-Path -Path $testEnginePath -ChildPath "playwright.ps1"
# Check if the file exists
if (Test-Path -Path $playwrightScriptPath) {
Write-Host "Running Playwright installer from: $(Hide-SensitiveValue $playwrightScriptPath)" -ForegroundColor Green
Start-Process -FilePath "pwsh" -ArgumentList "-Command `"$playwrightScriptPath install`"" -Wait
} else {
Write-Error "Playwright script not found at: $(Hide-SensitiveValue $playwrightScriptPath)"
}
} else {
Write-Host "Skipped playwright install"
}
$env:user1Email = $user1Email
Write-Host "========================================" -ForegroundColor Green
Write-Host "ENTITIES" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green # Loop through Entity (List and details) and Execute Tests
foreach ($entity in $entities) {
$formName = $entity.name
$entityName = $entity.entity
# Skip if entity filter is specified and doesn't match current entity
if (-not [string]::IsNullOrEmpty($entityFilter) -and $entityName -notlike "*$entityFilter*" -and $formName -notlike "*$entityFilter*") {
Write-Host "Skipping $formName ($entityName) - doesn't match entity filter: $entityFilter" -ForegroundColor Gray
continue
}
Write-Host "----------------------------------------" -ForegroundColor Yellow
Write-Host $entity.name -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow
if ($config.pages.list -and ([string]::IsNullOrEmpty($pageTypeFilter) -or $pageTypeFilter -contains "entitylist")) {
$matchingScript = "$formName-list.te.yaml"
if (-not (Test-Path -Path "$currentDirectory\$matchingScript") ) {
Write-Host "No matching test script found for: $matchingScript"
continue
}
# Query the default (isdefault = true) public (querytype = 0) saved query ID for the entity
$lookup = "$environmentUrl/api/data/v9.2/savedqueries?`$filter=returnedtypecode eq '$entityName' and isdefault eq true and querytype eq 0&`$select=savedqueryid"
$response = Invoke-RestMethod -Uri $lookup -Method Get -Headers @{Authorization = "Bearer $($token.accessToken)"}
$viewId = $response.value.savedqueryid
$testStart = Get-Date
$mdaUrl = "$environmentUrl/main.aspx?appid=$appId&pagetype=entitylist&etn=$entityName&viewid=$viewId&viewType=1039"
# Execute the test using our helper function
$testScriptPath = "$currentDirectory\$matchingScript"
Execute-Test -testScriptPath $testScriptPath -targetUrl $mdaUrl -useStaticContextArg:$useStaticContext
} else {
Write-Host "Skipped list test script"
}
if ($config.pages.details -and ([string]::IsNullOrEmpty($pageTypeFilter) -or $pageTypeFilter -contains "entityrecord")) {
$matchingScript = "$formName-details.te.yaml"
if (-not (Test-Path -Path "$currentDirectory\$matchingScript") ) {
Write-Host "No matching test script found for: $matchingScript"
continue
}
# Query the record ID for the entity
if ($entityName[-1] -ne 's') {
$entityNamePlural = $entityName + 's'
}
else {
$entityNamePlural = $entityName + 'es'
}
$idColumn = $entity.id
$lookup = "$environmentUrl/api/data/v9.2/$entityNamePlural`?`$top=1&`$select=$idColumn"
$entityResponse = Invoke-RestMethod -Uri $lookup -Method Get -Headers @{Authorization = "Bearer $($token.accessToken)"}
$recordId = $entityResponse.value | Select-Object -ExpandProperty $idColumn
$testStart = Get-Date
if ([string]::IsNullOrEmpty($recordId)) {
$mdaUrl = "$environmentUrl/main.aspx?appid=$appId&pagetype=entityrecord&etn=$entityName"
} else {
$mdaUrl = "$environmentUrl/main.aspx?appid=$appId&pagetype=entityrecord&etn=$entityName&id=$recordId"
}
Write-Host "Skipped recording"
# Run the tests for each user in the configuration file.
$testScriptPath = "$currentDirectory\$matchingScript"
Execute-Test -testScriptPath $testScriptPath -targetUrl $mdaUrl -useStaticContextArg:$useStaticContext
} else {
Write-Host "Skipped details test script"
}
}
if ($config.pages.customPage -and ([string]::IsNullOrEmpty($pageTypeFilter) -or $pageTypeFilter -contains "custom")) {
Write-Host "========================================" -ForegroundColor Green
Write-Host "CUSTOM PAGES" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
# Loop through Custom Pages and Execute Tests
foreach ($customPage in $customPages) {
# Skip if custom page filter is specified and doesn't match current custom page
if (-not [string]::IsNullOrEmpty($customPageFilter) -and $customPage -notlike "*$customPageFilter*") {
Write-Host "Skipping custom page $customPage - doesn't match filter: $customPageFilter" -ForegroundColor Gray
continue
}
# Ensure testScripts is an array
$testScriptList = @($testScripts.customPageTestScripts) # Extract values explicitly
function Split-CustomPageName {
param (
[string]$inputString
)
# Step 1: Split by underscore or hyphen
$words = $inputString -split "[-_]"
return $words
}
function Get-MatchingTestScript {
param (
[string[]]$wordList, # List of words extracted from the custom page name
[string[]]$testScriptList # List of available test script names
)
foreach ($script in $testScriptList) {
foreach ($word in $wordList) {
if ($script -match [regex]::Escape($word)) {
return $script # Return the first matching script
}
}
}
return $null # Return null if no match is found
}
$wordList= Split-CustomPageName -inputString $customPage
$matchingScript = Get-MatchingTestScript -wordList $wordList -testScriptList $testScriptList
if (-not $matchingScript) {
Write-Host "No matching test script found for custom page: $customPage" -ForegroundColor Red
continue # Skip this iteration if no matching script is found
}
Write-Host "----------------------------------------" -ForegroundColor Yellow
Write-Host $matchingScript -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow $testStart = Get-Date
$mdaUrl = "$environmentUrl/main.aspx?appid=$appId&pagetype=custom&name=$customPage"
Write-Host "Skipped recording"
# Run the tests for each user in the configuration file.
$testScriptPath = "$currentDirectory\$matchingScript"
Execute-Test -testScriptPath $testScriptPath -targetUrl $mdaUrl -useStaticContextArg:$useStaticContext
}
Write-Host "All custompages executed"
} # Reset the location back to the original directory.
Set-Location $currentDirectory
# Update the endTimeThreshold to current time after tests have run
# Only do this if endTime wasn't explicitly provided
if (-not $endTime) {
$endTimeThreshold = Get-Date
}
}
# Function to update the run name in TRX files to make them compatible with PowerAppsTestEngine.dll report generation
function Update-TrxFilesRunName {
param(
[string]$searchPath, # Path to search for TRX files
[DateTime]$startTime, # Only process files created after this time
[string]$newRunName # The run name to insert into the TRX files
)
Write-Host "Searching for TRX files in $searchPath created after $($startTime.ToString('yyyy-MM-dd HH:mm:ss'))..." -ForegroundColor Cyan
# Get all TRX files created after the start time
$trxFiles = Get-ChildItem -Path $searchPath -Filter "*.trx" -Recurse |
Where-Object { $_.CreationTime -ge $startTime }
if ($trxFiles.Count -eq 0) {
Write-Host "No TRX files found matching the criteria." -ForegroundColor Yellow
return
}
Write-Host "Found $($trxFiles.Count) TRX file(s) to process." -ForegroundColor Green
foreach ($file in $trxFiles) {
Write-Host "Processing $($file.FullName)..." -ForegroundColor Cyan
try {
# Load the TRX file as XML
[xml]$trxXml = Get-Content -Path $file.FullName
# Create a namespace manager to handle the XML namespaces
$nsManager = New-Object System.Xml.XmlNamespaceManager($trxXml.NameTable)
# Check if the document has a default namespace
$defaultNs = $trxXml.DocumentElement.NamespaceURI
if (-not [string]::IsNullOrEmpty($defaultNs)) {
# Add the default namespace with a prefix to use in XPath queries
$nsManager.AddNamespace("ns", $defaultNs)
Write-Host " Document has namespace: $defaultNs" -ForegroundColor DarkGray
# Use the namespace prefix in our XPath
$testRun = $trxXml.SelectSingleNode("//ns:TestRun", $nsManager)
} else {
# No namespace, use regular XPath
$testRun = $trxXml.SelectSingleNode("//TestRun")
}
if ($testRun -ne $null) {
$oldRunId = $testRun.id
$oldRunName = $testRun.name
Write-Host " Original Run ID: $oldRunId" -ForegroundColor DarkGray
Write-Host " Original Run Name: $oldRunName" -ForegroundColor DarkGray
# Update the run ID and name
$testRun.id = $newRunName
$testRun.name = "TestEngine Test Run $newRunName"
Write-Host " New Run ID: $newRunName" -ForegroundColor DarkGray
Write-Host " New Run Name: TestEngine Test Run $newRunName" -ForegroundColor DarkGray
# Also update any TestRunConfiguration element that might contain the run ID
if (-not [string]::IsNullOrEmpty($defaultNs)) {
$testRunConfig = $trxXml.SelectSingleNode("//ns:TestRunConfiguration", $nsManager)
} else {
$testRunConfig = $trxXml.SelectSingleNode("//TestRunConfiguration")
}
if ($testRunConfig -ne $null -and $testRunConfig.HasAttribute("id")) {
$testRunConfig.SetAttribute("id", $newRunName)
}
# Save the modified TRX file
$trxXml.Save($file.FullName)
Write-Host " Updated TRX file saved successfully." -ForegroundColor Green
}
else { Write-Host " Warning: TestRun element not found in TRX file." -ForegroundColor Yellow
}
}
catch {
Write-Host " Error processing TRX file: $($_.Exception.Message)" -ForegroundColor Red
Write-Host " Exception details: $($_.Exception.GetType().FullName)" -ForegroundColor Red
Write-Host " Error processing TRX file: $_" -ForegroundColor Red
}
}
}
if ($usePacTest) {
# When using pac test, we need to update the runName in all TRX files
# to ensure they're identified by the PowerAppsTestEngine.dll report generator
Write-Host "Processing test results from pac test run..." -ForegroundColor Cyan
# Get the start time of this script execution as a reference point
# Only process TRX files created after this script started running
$scriptStartTime = $startTimeThreshold
# Update the TRX files to use our runName - search in the entire TestEngine directory
Update-TrxFilesRunName -searchPath $testEngineBasePath -startTime $scriptStartTime -newRunName $runName
}
$reportPath = [System.IO.Path]::Combine($folderPath, "test_summary_$runName.html")
# Generate report using PowerAppsTestEngine.dll directly, regardless of test execution mode
Write-Host "Generating report using PowerAppsTestEngine.dll..." -ForegroundColor Green
Push-Location $testEnginePath
try {
# Create masked versions for logging
$maskedReportPath = Hide-SensitiveValue -Value $reportPath
# Log the command being executed (with masked sensitive values)
Write-Host "Executing: dotnet PowerAppsTestEngine.dll --run-name $runName --output-file `"$maskedReportPath`" --start-time $startTimeThreshold" -ForegroundColor DarkGray
dotnet PowerAppsTestEngine.dll --run-name $runName --output-file $reportPath --start-time $startTimeThreshold
}
finally {
Pop-Location
}
# Report was successfully generated (either Azure DevOps style or classic)
$maskedReportPath = Hide-SensitiveValue -Value $reportPath
Write-Host "HTML summary report available at $maskedReportPath." -ForegroundColor Green
# Open the report in the default browser
Write-Host "Opening report in browser..." -ForegroundColor Green
Start-Process $reportPath
# Add information about how to regenerate this report using the startTime and endTime parameters
# Always use the most current timestamp as the end time
$currentTime = (Get-Date).ToString("yyyy-MM-dd HH:mm")
# Format the start time that was used - this is either the provided startTime or the current time when the script started
$formattedStartTime = $startTimeThreshold.ToString("yyyy-MM-dd HH:mm")
Write-Host "=============================================" -ForegroundColor Magenta
Write-Host "REPORT REGENERATION INFORMATION" -ForegroundColor Magenta
Write-Host "=============================================" -ForegroundColor Magenta
Write-Host "To regenerate this exact report without running tests again, use:" -ForegroundColor Magenta
Write-Host "./RunTests.ps1 -runName `"$runName`"" -ForegroundColor Yellow
Write-Host "=============================================" -ForegroundColor Magenta