0

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.

3
  • Do all tests depend on the credentials being valid? If so, use the HttpClient to verify the credentials work before executing the tests. If you have scenarios where only certain pages need authentication, is the driver.PageSource different before and after the authentication request? Wouldn't that suffice? Commented May 30 at 18:11
  • @bryanbcook first thanks your your reply, but this is not a test scenario, rather a feature. Also I did mention in my question that verifying the page source does not work since the content can be anything. HttpClient may 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... Commented May 30 at 18:58
  • Sending two calls is going to be WAY faster and less complicated than waiting for each site to load and find something that indicates the page is loaded successfully. Commented May 31 at 2:27

0

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.