Skip to content

Commit

Permalink
feat: multiple users per alias (#88)
Browse files Browse the repository at this point in the history
Allows multiple users to be configured for a given alias which will result in tests being evenly distributed across the users. This is required for running large numbers of tests in parallel to avoid API limit issues.

Co-authored-by: Max Ewing <[email protected]>
Co-authored-by: Alex Bance <[email protected]>
  • Loading branch information
3 people authored Jun 24, 2021
1 parent 1ece3d6 commit 26ed97f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ users: # mandatory
The URL, driversPath, usernames, passwords, and application user details will be set from environment variable (if found). Otherwise, the value from the config file will be used. The browserOptions node supports anything in the EasyRepro `BrowserOptions` class.

Tests will be distributed evenly between the users if multiple users are configured with the same alias. This can be helpful when you run a large number of tests in parallel and are encountering errors due to user-level platform API limits.

#### User profiles

Setting the `useProfiles` property to true causes the solution to create and use a unique [profile](https://support.google.com/chrome/answer/2364824?co=GENIE.Platform%3DDesktop&hl=en) for each user listed in the config file. This currently only works in Chrome & Firefox and attempting to use it with Edge or IE will cause an exception to be thrown. By using profiles test runs for the same user will not be required to re-authenticate, this saves time during test runs. [To take full advantage of this you will need to have the "Stay Signed In" prompt enabled.](https://docs.microsoft.com/en-us/azure/active-directory/fundamentals/keep-me-signed-in)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using YamlDotNet.Serialization;

/// <summary>
Expand All @@ -18,6 +19,9 @@ public class TestConfiguration

private const string GetUserException = "Unable to retrieve user configuration. Please ensure a user with the given alias exists in the config.";

private readonly object userEnumeratorsLock = new object();
private readonly ThreadLocal<UserConfiguration> currentUser = new ThreadLocal<UserConfiguration>();
private Dictionary<string, IEnumerator<UserConfiguration>> userEnumerators;
private string profilesBasePath;

/// <summary>
Expand Down Expand Up @@ -64,6 +68,28 @@ public TestConfiguration()
[YamlMember(Alias = "applicationUser")]
public ClientCredentials ApplicationUser { get; set; }

[YamlIgnore]
private Dictionary<string, IEnumerator<UserConfiguration>> UserEnumerators
{
get
{
lock (this.userEnumeratorsLock)
{
if (this.userEnumerators == null)
{
this.userEnumerators = this.Users
.Select(user => user.Alias)
.Distinct()
.ToDictionary(
alias => alias,
alias => this.Users.Where(u => u.Alias == alias).GetEnumerator());
}
}

return this.userEnumerators;
}
}

/// <summary>
/// Gets the target URL.
/// </summary>
Expand All @@ -77,17 +103,36 @@ public Uri GetTestUrl()
/// Retrieves the configuration for a user.
/// </summary>
/// <param name="userAlias">The alias of the user.</param>
/// <param name="useCurrentUser">Indicates whether to return the current user or get the next available.</param>
/// <returns>The user configuration.</returns>
public UserConfiguration GetUser(string userAlias)
public UserConfiguration GetUser(string userAlias, bool useCurrentUser = true)
{
if (useCurrentUser && this.currentUser.Value != null)
{
return this.currentUser.Value;
}

try
{
return this.Users.First(u => u.Alias == userAlias);
lock (this.userEnumeratorsLock)
{
var aliasEnumerator = this.UserEnumerators[userAlias];
if (!aliasEnumerator.MoveNext())
{
this.UserEnumerators[userAlias] = this.Users.Where(u => u.Alias == userAlias).GetEnumerator();
aliasEnumerator = this.UserEnumerators[userAlias];
aliasEnumerator.MoveNext();
}

this.currentUser.Value = aliasEnumerator.Current;
}
}
catch (Exception ex)
{
throw new ConfigurationErrorsException($"{GetUserException} User: {userAlias}", ex);
}

return this.currentUser.Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public static void Login(IWebDriver driver, Uri orgUrl, string username, string
/// <param name="appName">The name of the app.</param>
/// <param name="userAlias">The alias of the user.</param>
[Given("I am logged in to the '(.*)' app as '(.*)'")]
public void GivenIAmLoggedInToTheAppAs(string appName, string userAlias)
public static void GivenIAmLoggedInToTheAppAs(string appName, string userAlias)
{
var user = TestConfig.GetUser(userAlias);
var user = TestConfig.GetUser(userAlias, useCurrentUser: false);

if (TestConfig.UseProfiles && TestConfig.BrowserOptions.BrowserType.SupportsProfiles())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="Microsoft.CrmSdk.XrmTooling.CoreAssembly" Version="9.1.0.64" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="87.0.4280.8800" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="91.0.4472.10100" />
<PackageReference Include="SpecFlow" Version="3.5.14" />
<PackageReference Include="SpecFlow.MsTest" Version="3.5.14" />
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.5.14" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ users:
- username: POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_USERNAME
password: POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_PASSWORD
alias: an admin
- username: POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_USERNAME2
password: POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_PASSWORD2
alias: an admin
- username: POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_USERNAME
alias: an aliased user
2 changes: 2 additions & 0 deletions templates/include-build-and-test-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ jobs:
POWERAPPS_SPECFLOW_BINDINGS_TEST_CLIENTSECRET: $(Application User Client Secret)
POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_USERNAME: $(User ADO Integration Username)
POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_PASSWORD: $(User ADO Integration Password)
POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_USERNAME2: $(Extra Admin User Username)
POWERAPPS_SPECFLOW_BINDINGS_TEST_ADMIN_PASSWORD2: $(Extra Admin User Password)
POWERAPPS_SPECFLOW_BINDINGS_TEST_URL: $(URL)
- task: SonarCloudAnalyze@1
displayName: Analyse with SonarCloud
Expand Down

0 comments on commit 26ed97f

Please sign in to comment.