Skip to content

Commit

Permalink
Log updates and Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant-Archibald-MS committed Nov 20, 2024
1 parent 09a3422 commit 102db5d
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 42 deletions.
35 changes: 20 additions & 15 deletions samples/coe-kit-setup-wizard/Record.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@ $jsonContent = Get-Content -Path .\config.json -Raw
$config = $jsonContent | ConvertFrom-Json
$tenantId = $config.tenantId
$environmentId = $config.environmentId
$user1Email = $config.user1Email

if ([string]::IsNullOrEmpty($environmentId)) {
Write-Error "Environment not configured. Please update config.json"
return
}

$textResult = [string] (pac env list)

$foundEnvironment = $false
$textResult = [string] (pac env select --environment $environmentId)
$textResult = (pac env select --environment $environmentId)
$textResult = (pac env list)

try{
$textResult -match "'(https://[^\s']+)'"
$environmentMatch = $matches
$foundEnvironment = $true
} catch {

$environmentUrl = ""

Write-Host "Searching for $environmentId"

foreach ($line in $textResult) {
if ($line -match $environmentId) {
if ($line -match "(https://\S+/)") {
$environmentUrl = $matches[0].Substring(0,$matches[0].Length - 1)
$foundEnvironment = $true
break
}
}
}

# Extract the URL using a general regular expression
if ($foundEnvironment -and $environmentMatch.Count -ge 1) {
$environmentUrl = $environmentMatch[1].TrimEnd("/")
if ($foundEnvironment) {
Write-Output "Found matching Environment URL: $environmentUrl"
} else {
Write-Output "URL not found. Please create authentication and re-run script"
pac auth create --environment $environmentId
Write-Output "Environment ID not found."
return
}

Expand Down Expand Up @@ -62,7 +66,8 @@ if ($config.installPlaywright) {

Set-Location ..\bin\Debug\PowerAppsTestEngine
# Run the tests for each user in the configuration file.
dotnet PowerAppsTestEngine.dll -u "browser" -p "mda" -a "none" -r True -i "$currentDirectory\record.fx.yaml" -t $tenantId -e $environmentId -d "$mdaUrl" -w True
$env:user1Email = $user1Email
dotnet PowerAppsTestEngine.dll -u "storagestate" -p "mda" -a "none" -r True -i "$currentDirectory\record.fx.yaml" -t $tenantId -e $environmentId -d "$mdaUrl" -w True

# Reset the location back to the original directory.
Set-Location $currentDirectory
33 changes: 19 additions & 14 deletions samples/coe-kit-setup-wizard/RunTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@ $jsonContent = Get-Content -Path .\config.json -Raw
$config = $jsonContent | ConvertFrom-Json
$tenantId = $config.tenantId
$environmentId = $config.environmentId
$user1Email = $config.user1Email

if ([string]::IsNullOrEmpty($environmentId)) {
Write-Error "Environment not configured. Please update config.json"
return
}

$textResult = [string] (pac env list)

$foundEnvironment = $false
$textResult = [string] (pac env select --environment $environmentId)
$textResult = (pac env select --environment $environmentId)
$textResult = (pac env list)

try{
$textResult -match "'(https://[^\s']+)'"
$environmentMatch = $matches
$foundEnvironment = $true
} catch {

$environmentUrl = ""

Write-Host "Searching for $environmentId"

foreach ($line in $textResult) {
if ($line -match $environmentId) {
if ($line -match "(https://\S+/)") {
$environmentUrl = $matches[0].Substring(0,$matches[0].Length - 1)
$foundEnvironment = $true
break
}
}
}

# Extract the URL using a general regular expression
if ($foundEnvironment -and $environmentMatch.Count -ge 1) {
$environmentUrl = $environmentMatch[1].TrimEnd("/")
if ($foundEnvironment) {
Write-Output "Found matching Environment URL: $environmentUrl"
} else {
Write-Output "URL not found. Please create authentication and re-run script"
pac auth create --environment $environmentId
Write-Output "Environment ID not found."
return
}

Expand Down Expand Up @@ -60,6 +64,7 @@ if ($config.installPlaywright) {
}

Set-Location ..\bin\Debug\PowerAppsTestEngine
$env:user1Email = $user1Email
# Run the tests for each user in the configuration file.
dotnet PowerAppsTestEngine.dll -u "storagestate" -p "mda" -a "none" -i "$currentDirectory\testPlan.fx.yaml" -t $tenantId -e $environmentId -d "$mdaUrl" -l Debug

Expand Down
2 changes: 1 addition & 1 deletion samples/coe-kit-setup-wizard/record.fx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ testSettings:
environmentVariables:
users:
- personaName: User1
emailKey: NotNeeded
emailKey: user1Email
passwordKey: NotNeeded
8 changes: 4 additions & 4 deletions samples/coe-kit-setup-wizard/testPlan.fx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ testSuite:
testCaseDescription: Verify pre-requistes in place
testSteps: |
=
TestEngine.ConsentDialog(Table({Text: "Center of Excellence Setup Wizard"}));
TestEngine.Pause();
Experimental.ConsentDialog(Table({Text: "Center of Excellence Setup Wizard"}));
Experimental.Pause();
Set(configStep, 1);
Assert(configStep=1);
Select(btnNext);
Expand All @@ -21,7 +21,7 @@ testSuite:
Assert(configStep=2);
Assert(CountRows(colCommunicate)=3);
Experimental.SelectControl(Button3,1);
TestEngine.Pause();
Experimental.Pause();
testSettings:
headless: false
locale: "en-US"
Expand All @@ -35,5 +35,5 @@ testSettings:
environmentVariables:
users:
- personaName: User1
emailKey: NotNeeded
emailKey: user1Email
passwordKey: NotNeeded
25 changes: 25 additions & 0 deletions src/Microsoft.PowerApps.TestEngine.Tests/Reporting/TestLogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System;
using Microsoft.PowerApps.TestEngine.Reporting;
using Xunit;

namespace Microsoft.PowerApps.TestEngine.Tests.Reporting
{
public class TestLogTests
{
[Fact]
public void DateTest()
{
// Arrange
var test = new DateTime(2022, 11, 16);
var log = new TestLog() { TimeStamper = () => test };

// Act & Assert
Assert.Equal(test ,log.When);

// Assert
}
}
}
16 changes: 12 additions & 4 deletions src/Microsoft.PowerApps.TestEngine.Tests/TestEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ public async Task TestEngineWithDefaultParamsTest()
var outputDirectory = new DirectoryInfo("TestOutput");
var testRunId = Guid.NewGuid().ToString();
var expectedOutputDirectory = outputDirectory.FullName;
var testRunDirectory = Path.Combine(expectedOutputDirectory, testRunId.Substring(0, 6));
var testRunDirectory = Path.Combine(expectedOutputDirectory, "2024-11-20T00-00-00-0000000-" + testRunId.Substring(0, 6));
var domain = "apps.powerapps.com";

var expectedTestReportPath = "C:\\test.trx";

SetupMocks(expectedOutputDirectory, testSettings, testSuiteDefinition, testRunId, expectedTestReportPath);

var testEngine = new TestEngine(MockState.Object, ServiceProvider, MockTestReporter.Object, MockFileSystem.Object, MockLoggerFactory.Object, MockTestEngineEventHandler.Object);
testEngine.Timestamper = () => new DateTime(2024, 11, 20);

var testReportPath = await testEngine.RunTestAsync(testConfigFile, environmentId, tenantId, outputDirectory, domain, "");

Assert.Equal(expectedTestReportPath, testReportPath);
Expand Down Expand Up @@ -156,14 +158,16 @@ public async Task TestEngineWithUnspecifiedLocaleShowsWarning()
var outputDirectory = new DirectoryInfo("TestOutput");
var testRunId = Guid.NewGuid().ToString();
var expectedOutputDirectory = outputDirectory.FullName;
var testRunDirectory = Path.Combine(expectedOutputDirectory, testRunId.Substring(0, 6));
var testRunDirectory = Path.Combine(expectedOutputDirectory, "2024-11-20T00-00-00-0000000-" + testRunId.Substring(0, 6));
var domain = "apps.powerapps.com";

var expectedTestReportPath = "C:\\test.trx";

SetupMocks(expectedOutputDirectory, testSettings, testSuiteDefinition, testRunId, expectedTestReportPath);

var testEngine = new TestEngine(MockState.Object, ServiceProvider, MockTestReporter.Object, MockFileSystem.Object, MockLoggerFactory.Object, MockTestEngineEventHandler.Object);
testEngine.Timestamper = () => new DateTime(2024, 11, 20);

var testReportPath = await testEngine.RunTestAsync(testConfigFile, environmentId, tenantId, outputDirectory, domain, "");

Assert.Equal(expectedTestReportPath, testReportPath);
Expand Down Expand Up @@ -197,14 +201,16 @@ public async Task TestEngineWithMultipleBrowserConfigTest()
var outputDirectory = new DirectoryInfo("TestOutput");
var testRunId = Guid.NewGuid().ToString();
var expectedOutputDirectory = outputDirectory.FullName;
var testRunDirectory = Path.Combine(expectedOutputDirectory, testRunId.Substring(0, 6));
var testRunDirectory = Path.Combine(expectedOutputDirectory, "2024-11-20T00-00-00-0000000-" + testRunId.Substring(0, 6));
var domain = "apps.powerapps.com";

var expectedTestReportPath = "C:\\test.trx";

SetupMocks(expectedOutputDirectory, testSettings, testSuiteDefinition, testRunId, expectedTestReportPath);

var testEngine = new TestEngine(MockState.Object, ServiceProvider, MockTestReporter.Object, MockFileSystem.Object, MockLoggerFactory.Object, MockTestEngineEventHandler.Object);
testEngine.Timestamper = () => new DateTime(2024, 11, 20);

var testReportPath = await testEngine.RunTestAsync(testConfigFile, environmentId, tenantId, outputDirectory, domain, "");

Assert.Equal(expectedTestReportPath, testReportPath);
Expand Down Expand Up @@ -246,7 +252,7 @@ public async Task TestEngineTest(DirectoryInfo outputDirectory, string domain, T
{
expectedOutputDirectory = new DirectoryInfo("TestOutput");
}
var testRunDirectory = Path.Combine(expectedOutputDirectory.FullName, testRunId.Substring(0, 6));
var testRunDirectory = Path.Combine(expectedOutputDirectory.FullName, "2024-11-20T00-00-00-0000000-" + testRunId.Substring(0, 6));

if (string.IsNullOrEmpty(domain))
{
Expand All @@ -258,6 +264,8 @@ public async Task TestEngineTest(DirectoryInfo outputDirectory, string domain, T
SetupMocks(expectedOutputDirectory.FullName, testSettings, testSuiteDefinition, testRunId, expectedTestReportPath);

var testEngine = new TestEngine(MockState.Object, ServiceProvider, MockTestReporter.Object, MockFileSystem.Object, MockLoggerFactory.Object, MockTestEngineEventHandler.Object);
testEngine.Timestamper = () => new DateTime(2024, 11, 20);

var testReportPath = await testEngine.RunTestAsync(testConfigFile, environmentId, tenantId, outputDirectory, domain, "");

Assert.Equal(expectedTestReportPath, testReportPath);
Expand Down
19 changes: 19 additions & 0 deletions src/Microsoft.PowerApps.TestEngine/Reporting/TestLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ namespace Microsoft.PowerApps.TestEngine.Reporting
{
public class TestLog
{
private Func<DateTime> _timeStamper = () => DateTime.Now;
public Func<DateTime> TimeStamper {
get {
return _timeStamper;
}

set
{
_timeStamper = value;
When = _timeStamper();
}
}

public TestLog() {
When = TimeStamper();
}

public DateTime When { get; private set; }

public string ScopeFilter { get; set; }
public string LogMessage { get; set; }
}
Expand Down
9 changes: 6 additions & 3 deletions src/Microsoft.PowerApps.TestEngine/Reporting/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public class TestLogger : ITestLogger
public List<TestLog> DebugLogs { get; set; } = new List<TestLog>();
private TestLoggerScope currentScope = null;

public Func<DateTime> TimeStamper { get; set; }

public TestLogger(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
TimeStamper = new TestLog().TimeStamper;
}

public IDisposable BeginScope<TState>(TState state)
Expand Down Expand Up @@ -95,15 +98,15 @@ public void Log<TState>(LogLevel messageLevel, EventId eventId, TState state, Ex
}
}

logString += $"{formatter(state, exception)}{Environment.NewLine}";
logString += $"{TimeStamper().ToString("o")} - {formatter(state, exception)}{Environment.NewLine}";

var scopeFilter = currentScope != null ? currentScope.GetScopeString() : "";
if (messageLevel > LogLevel.Debug)
{
Logs.Add(new TestLog() { LogMessage = logString, ScopeFilter = scopeFilter });
Logs.Add(new TestLog() { TimeStamper = TimeStamper, LogMessage = logString, ScopeFilter = scopeFilter });
}

DebugLogs.Add(new TestLog() { LogMessage = logString, ScopeFilter = scopeFilter });
DebugLogs.Add(new TestLog() { TimeStamper = TimeStamper, LogMessage = logString, ScopeFilter = scopeFilter });
}
}
}
9 changes: 8 additions & 1 deletion src/Microsoft.PowerApps.TestEngine/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class TestEngine

public ILogger Logger { get; set; }

public Func<DateTime> Timestamper { get; set; }

public TestEngine(ITestState state,
IServiceProvider serviceProvider,
ITestReporter testReporter,
Expand All @@ -42,6 +44,7 @@ public TestEngine(ITestState state,
_fileSystem = fileSystem;
_loggerFactory = loggerFactory;
_eventHandler = eventHandler;
Timestamper = () => DateTime.UtcNow;
}

/// <summary>
Expand Down Expand Up @@ -113,7 +116,11 @@ public async Task<string> RunTestAsync(FileInfo testConfigFile, string environme

_state.SetTestConfigFile(testConfigFile);

testRunDirectory = Path.Combine(_state.GetOutputDirectory(), testRunId.Substring(0, 6));
var now = Timestamper().ToString("o")
.Replace(":", "-")
.Replace(".", "-");

testRunDirectory = Path.Combine(_state.GetOutputDirectory(), now + "-" + testRunId.Substring(0, 6));
_fileSystem.CreateDirectory(testRunDirectory);
Logger.LogInformation($"Test results will be stored in: {testRunDirectory}");

Expand Down

0 comments on commit 102db5d

Please sign in to comment.