With .NET and Selenium, I'm using Chrome drivers to navigate pages and retrieve their content.
Consider the following code where I use basic authentication to retrieve a page content. I'm targeting .NET 8.0 and Selenium.WebDriver 4.32.0.
var webDriver = new ChromeDriver();
webDriver.Manage().Network.AddAuthenticationHandler(
new NetworkAuthenticationHandler
{
UriMatcher = _ => true,
Credentials = new PasswordCredentials("someUsername", "somePassword"),
}
);
webDriver.Navigate().GoToUrl("https://someurlthatrequiresauth.com");
Where, in my use case, username, password and url are all external input. Which means that the authentication may succeed or fails depending on whether the credentials match, and I need a reliable way to know that. To my knowledge, the driver itself doesn't seem to have different behavior in both cases other than driver.PageSource content would be different.
If I was using something like HttpClient, I can easily verify the response StatusCode for this, but I haven't found anything similar on ChromDriver's properties. Verifying driver.PageSource content is unreliable since the source content can be anything.
So I'm looking for a way to verify whether the authentication worked or not, and handle failure cases. Ideally the way should be simple and straightforward, but I'm open to suggestions as long as we stay with ChromeDrivers. Thanks in advance.
driver.PageSourcedifferent before and after the authentication request? Wouldn't that suffice?HttpClientmay work, yes, but I really want to avoid sending double the call for each url I have to authenticate just to know if it works or not...