1

I am using Reqnroll in an ASP.NET Core MVC app written in C#.

I have the following scenario:

Szenario: Passwort zurücksetzen und anmelden
Wenn ich auf Passwort vergessen klicke
Und meine Email eingebe
Dann wird die Erfolgsmeldung "Prüfe dein Email Postfach" angezeigt
Wenn ich den Link öffne
Und ich mein Passwort wie folgt neu setzte:
    | Label               | Eingabe          |
    | E-Mail              | (Benutzer_Email) |
    | Passwort            | Abc1234!         |
    | Passwort bestätigen | Abc1234!         |
Dann ist die Seite "Anmelden" offen

My problem is, that I can't find a way to replace the (Benutzer_Email) value with [StepArgumentTransformation]. Each scenario is run with a different email and username. The email this scenario gets is random, that is why I have to replace it.

I have done it successfully when just using a simple string:

[StepArgumentTransformation]
public string ErsetzteBenutzerDaten(string eingabe)
{
    var benutzer = this.testContext.GibBenutzer();
    return eingabe.Replace("(Benutzername)", benutzer.Benutzername)
                  .Replace("(Benutzer_Email)", benutzer.Email);
}

But within a DataTable, I just can't figure this one out.

This code doesn't work:

[StepArgumentTransformation]
public DataTable TransformEmail(DataTable table)
{
    var benutzer = this.testContext.GibBenutzer();

    foreach (var row in table.Rows)
    {
        for (int i = 0; i < row.Keys.Count; i++)
        {
            var value = row[i];

            table.Rows. = value.Replace("(Benutzername)", benutzer.Benutzername)
                                 .Replace("(Benutzer_Email)", benutzer.Email);
        }
    }

    return benutzer.Email;
}

Does anyone know how to do this correctly?

Thank you in advance
Simon

2 Answers 2

1

To be honest, the easiest answer is not handle the e-mail in the step definition. Furthermore, since the password is hard-coded in the feature file, don't even specify the password. Instead, make that step a quick one-liner:

And I reset my password

(side note: I guessed the scenario was written in German and the step translated to "And I reset my password as follows")

Then the step definition:

public class MySteps
{
    public MySteps(TestContext testContext)
    {
        this.testContext = testContext;
    }

    [When("I reset my password")]
    public void AndIResetMyPassword()
    {
        var benutzer = this.testContext.GibBenutzer();
        var newPassword = "Abc1234!";

        // do stuff to reset the password using newPassword and benutzer.Email
    }
}

Not only is this easier to implement, it actually follows behavior-driven development principles better; your scenario focuses on the workflow and requirements, rather than the fine details of the e-mail address and password.

If you really want to communicate that the password must be entered twice, then consider splitting things into multiple steps:

Scenario: Reset password and log in
    Given I have forgotten my password
    When I request to reset my password
    And I go to reset my password using the link sent to me in an e-mail
    And I enter "Abc1234!" for my new password
    And I confirm my new password is "Abc1234!"
    Then the "Login" page is open

The main point is to not specify a thing that varies: the e-mail address. This is taken from the test context and is handled in the step definition.

Sign up to request clarification or add additional context in comments.

Comments

0

This is just a guess, but try returning a copy instead of mutating the original. You can use table.Copy() to get one (docs here).

Generally library methods either cause effects, or provide information / data - it's rare for them to do both (outside of a "your desired effect was successful" message). So if you're returning something, try not to create a side-effect at the same time.

If that doesn't work, please post what actually happens when you run your code!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.