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.
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.