Two-factor authentication (2FA) and One Time Passwords (OTP)

To test 2FA usually one of the many Authentication Apps is required. TestResults.io has build a NuGet Package with the same functionality.

The Nuget can be found online:

Demo Testcase

Check out the Demo Testcase using our NuGet to generate Codes from a QR-Code.

https://github.com/TestResultsIO/DemoAutoScout24/blob/05a81c99261d1d65abf9a96f6bad0c0aba5960c9/TC006_Rev1/_2FA.cs

[TestStep(1, 
    TestInput = "Activate 2FA with a QR code displayed on the Screen.", 
    ExpectedResults ="2FA can be activated.")
]
public void Step1(ITester t)
{
    //make sure to add the NuGet Package "TestResults.AddOn.OTP" to this testcase project
    //in your application navigate to the screen where you enable 2FA
    //make sure the QR code is visible on the screen
    var code = OtpReader.GetOtpCode(t.Connections.Active.CurrentScreenAsImage, out string otpAuthUri); //reads the QR code from the screen and returns the code
    t.Report.PassStep($"Received code for 2FA: \"{code}\" and URI: \"{otpAuthUri}\"");

    //enter the code in your Application to activate 2FA
    //WARNING: if you activate 2FA, make sure you save the otpAuthUri string or you will later be locked out of your account

    //if the code is rejected, it is most likely already expired. Call "OtpReader.GetOtpCode(_otpAuthUri)" to get a new code
    //if you logout and login again you can also request a new code like this
    var newCode = OtpReader.GetOtpCode(otpAuthUri);
}

Step by Step Guide

  1. Navigate to "Manage NuGet Packages" in the context menu of the Testcase Project.

  1. Search and install the current version of the NuGet "TestResults.Addon.OTP".

  1. Navigate back to your test case. At the top add the using of the new Nuget.

using TRIO.AddOn.OTP;
  1. Inside a teststep make sure to navigate to the screen where you enable 2FA and make sure the QR code is visible on the screen.

  2. The following code can be used to generate a one time password with the QR-Code currently shown on the Screen:

//reads the QR code from the screen and returns the code
string code = OtpReader.GetOtpCode(t.Connections.Active.CurrentScreenAsImage, out string otpAuthUri); 
t.Report.PassStep($"Received code for 2FA: \"{code}\" and URI: \"{otpAuthUri}\"");
  1. WARNING: make sure to save the logged "otpAuthUri" string if you enable 2FA or you will be later locked out of this account

  2. If the password gets rejected, it is most likely already expired as a password is usally only valid for one minute. Use the following code to request a new one. The same code can be used to request a password if you logout and login again.

var newCode = OtpReader.GetOtpCode(otpAuthUri);

Last updated