0

Currently, in my company, we have many smoke tests, and there are many logins, This causes some logins to fail and so it was suggested that we should get AuthToken from the browser and pass this to other Scenarios. I can't find any example of how to do this. So far, from what I gathered, It should look something like

 [BeforeScenario]
public void BeforeScenario()
{
    // perform login and store authentication token in scenario context
    var token = PerformLogin();
    ScenarioContext.Current["AuthToken"] = token;
}

private string PerformLogin()
{
    // code to perform login and retrieve authentication token
    // ...
    return "some-authentication-token";
}

My issue is how do I get this "some-authentication-token" for this I have found.

[AuthorizeForScopes(Scopes = new[] { "user.read" })]
public async Task<IActionResult> Profile()
{
   // Acquire the access token.
   string[] scopes = new string[]{"user.read"};
   string accessToken = await tokenAcquisition.GetAccessTokenForUserAsync(scopes);
   // Use the access token to call a protected web API.
   HttpClient client = new HttpClient();
   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",accessToken);
   string json = await client.GetStringAsync(url);
}

But I need to understand how to use this information to adapt the code to how it works for our website. I how to get the cookie and use the cookie for other scenarios logins.

So in conclusion I would love any examples of how to get the cookie and use this information.

For information on our setup. We are using .NET6 and specflow as the test framework and Nunit for asserts. Please comment below on any other information you might need.

Thanks in advance for any information you can provide.

Edit, So we are using Microsoft Azure login, and when we do all the logins, we end up overloading the system we end up waiting longer than the "wait" limits are exceeded. We got a 60-sec wait to find an element, and the page will not load in time.

auth token gets used by your before step and used before anything else is done, and then check to see if we are on the right page.

A seconded update.

Looking a lot deeper into this, I see the way to achice to get the Token could but done.

            var result = webDriver.FindElement(By.XPath("//html"));
        if (result != null)
        {
            var token10 = result.GetProperty("localStorage.getItem('oidc.user: The URL for my company ')");
            // use the token variable here
        }

But I have issues getting the token correct because it always returns null.

Setting LocalStorage with Chrome and Selenium This post was really helpful.

oisin1min
  • 27
  • 2
  • 11
  • Does this auth token get used by your step definitions, or do you need to initialize a new web driver session with the same auth token? – Greg Burghardt Mar 01 '23 at 18:10
  • *"This causes some logins to fail..."* -- what do you mean by "fail"? Can you [edit] your question to provide more information about the problem? – Greg Burghardt Mar 01 '23 at 18:11
  • @GregBurghardt I updated the description to make it clear what is happening. – oisin1min Mar 02 '23 at 09:50
  • I'm still unclear about the scope of the problem. Am I correct in assuming that your step definitions are making web API calls, and in order to do that they need an auth token? And because these step definitions are logging in so frequently, you are encountering rate limits for these web API calls? – Greg Burghardt Mar 02 '23 at 13:14
  • Your question appears to propose a solution, but we need to understand the root cause of your problem. – Greg Burghardt Mar 02 '23 at 13:15
  • So currently, we use the login like a normal person, but our architect suggested using a single sign-on model where we get an authentication token and skip the login for all scenarios. The issue that occurs is that when we log in, the page does not fully load, and we do not control the login processes for this project, so we can't change anything here. So my question is, how to successfully collect the login AuthToken? This is where I am having the issue. – oisin1min Mar 03 '23 at 12:22

1 Answers1

1

I found the solution to get the authentication-token.

// Get all the keys in local storage

var script = "return JSON.stringify(localStorage);";
var localStorageData = (string)((IJavaScriptExecutor)webDriver).ExecuteScript(script);

// Deserialize the JSON string into a dictionary
var localStorageDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(localStorageData);

// Get the id_token value from the dictionary
var idToken = localStorageDict["oidc.user:https://...censored "].ToString();

Extract the actual token value from the id_token string:

var tokenValue = idToken.Split('.')[1];

This was the only way I could get the token, and I will be simplifying the solution, but this worked for me, and I Hope it works for anyone else that might have the issue in the future.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
oisin1min
  • 27
  • 2
  • 11