diff --git a/README.md b/README.md index 34786c4..937b629 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Configuration/TestConfiguration.cs b/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Configuration/TestConfiguration.cs index 6fdd499..d6b28e9 100644 --- a/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Configuration/TestConfiguration.cs +++ b/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Configuration/TestConfiguration.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Configuration; using System.Linq; + using System.Threading; using YamlDotNet.Serialization; /// @@ -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 currentUser = new ThreadLocal(); + private Dictionary> userEnumerators; private string profilesBasePath; /// @@ -64,6 +68,28 @@ public TestConfiguration() [YamlMember(Alias = "applicationUser")] public ClientCredentials ApplicationUser { get; set; } + [YamlIgnore] + private Dictionary> 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; + } + } + /// /// Gets the target URL. /// @@ -77,17 +103,36 @@ public Uri GetTestUrl() /// Retrieves the configuration for a user. /// /// The alias of the user. + /// Indicates whether to return the current user or get the next available. /// The user configuration. - 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; } } } diff --git a/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Steps/LoginSteps.cs b/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Steps/LoginSteps.cs index 0731d72..9a17eb4 100644 --- a/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Steps/LoginSteps.cs +++ b/bindings/src/Capgemini.PowerApps.SpecFlowBindings/Steps/LoginSteps.cs @@ -54,9 +54,9 @@ public static void Login(IWebDriver driver, Uri orgUrl, string username, string /// The name of the app. /// The alias of the user. [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()) { diff --git a/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/Capgemini.PowerApps.SpecFlowBindings.UiTests.csproj b/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/Capgemini.PowerApps.SpecFlowBindings.UiTests.csproj index d159135..e066de5 100644 --- a/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/Capgemini.PowerApps.SpecFlowBindings.UiTests.csproj +++ b/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/Capgemini.PowerApps.SpecFlowBindings.UiTests.csproj @@ -38,7 +38,7 @@ - + diff --git a/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/power-apps-bindings.yml b/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/power-apps-bindings.yml index bf0ddf6..b6950e2 100644 --- a/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/power-apps-bindings.yml +++ b/bindings/tests/Capgemini.PowerApps.SpecFlowBindings.UiTests/power-apps-bindings.yml @@ -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 \ No newline at end of file diff --git a/templates/include-build-and-test-steps.yml b/templates/include-build-and-test-steps.yml index 1b4e49b..4d844e4 100644 --- a/templates/include-build-and-test-steps.yml +++ b/templates/include-build-and-test-steps.yml @@ -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