Search code examples
seleniumselenium-webdriverselenium-chromedrivernunit

Selenium: Best way to test app-features with 2 different users (NUnit, C#)


Im looking for a clean way to test all features on the webpage with 2 different users. One user is the admin, the second one a normal user.

Here is the overview of my selenium tests: enter image description here

As you can see, we have 3 different features on the webpage:

  • UnlockInstruction
  • Tac
  • UploadCodes

Each of these features has its own Test class with its own webDriver so im able to run the tests in parallel.

Each of these test files, is calling the Login Class inside the SetUp.

What the Login Class is doing is:

  1. Open Website with goToUrl
  2. Gets the username and password which is stored in a Password Manager tool
  3. Use selenium to enter username, password and click on login
  4. Wait until page after login is loaded and go back to test methods

Everything works perfectly when i test it for one user. All the test run in parallel.

Now i want to test all the same features with the admin user.

The only way which comes into my mind is, to just create another Login class, which gets the other users credentials and copy also the 3 test classes, so all the 6 tests run in parallel.

But in my opinion its not clean, because i would copy 4 files which would have nearly 1:1 the same code.


Solution

  • I'd make the user id and password arguments to the fixtures.

    1. Use a parameterized TestFixture (one with arguments)
    2. Give the same argument types to the constructor so that NUnit can pass them to you.
    3. Your four files will then result in six instances being constructed.
    [TestFixture("normalUser", "normalUserPassword")]
    [TestFixture("adminUser", "adminUserPassword")]
    public class SomeFixture
    {
        private string User { get; }
        private string Password { get; }
    
        public SomeFixture(string user, string password)
        {
            User = user;
            Password = password;
        }
    
        [OneTimeSetUp]
        public void SetUpTheFixture()
        {
            // Create the driver
        }
    
        ...
    }
    

    If you prefer to lookup the password, then just make the user id the only argument and look up the password when you need it.

    [TestFixture("normalUser")]
    [TestFixture("adminUser")]
    public class SomeFixture
    {
        private string User { get; }
    
        public SomeFixture(string user)
        {
            User = user;
        }
    
        [OneTimeSetUp]
        public void SetUpTheFixture()
        {
            // Look up password
            // Create driver
        }
    
        ...
    }