Automating Basic Authentication in Selenium (C#)

Automating Basic Authentication in Selenium (C#)

When testing web applications that require Basic Authentication, it's essential to bypass the login prompt during automated tests. Selenium WebDriver makes this process simple with a small trick — passing the credentials directly in the URL.


Steps to Automate Basic Authentication:

Format the URL: To avoid the authentication prompt, use this format: https://username:password@url

Example: https://admin:admin@the-internet.herokuapp.com/basic_auth

Automate Login: Once the browser navigates to the page, Selenium automatically handles the login for you.

Verify Successful Authentication: After authentication, we check for specific elements on the page (like a success message) to confirm the login was successful.


Selenium C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;

namespace SauceLabsAutomationPOM.Selenium_Tutorials
{
    public class BasicAuth_Test
    {
        private IWebDriver driver;


        private static string Url = "the-internet.herokuapp.com/basic_auth";
        private static string username= "admin",password="admin";
        [SetUp]
        public void SetUp()
        {


            if (driver == null)
            {
                driver = new ChromeDriver();
                driver.Manage().Window.Maximize(); // Maximize the Browser Window
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); //Adding implicit wait for page loading
                                                                                 
                driver.Navigate().GoToUrl("https://"+username+":"+password+"@"+Url);  //Navigate to URL like this for Basic Auth By pass

                Console.WriteLine($"Test execution started for URL: {Url}");

            }
        }


        [TearDown]
        public void TearDown()
        {
            // Clean up the WebDriver instance after each test
            if (driver != null)
            {
                Console.WriteLine("Closing the browser after each test.");
                driver.Close(); //Closing the browser window
            }
        }

        [Test]
        public void BasicAuthTest() {

            Thread.Sleep(3000); // Static sleep to see page actions  - if you know explicit wait then use it instead which is given in below commented code

            IWebElement text = driver.FindElement(By.XPath("//p[contains(text(),'Congratulations! You must have the proper credenti')]"));

            /*WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
            IWebElement text = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//p[contains(text(),'Congratulations! You must have the proper credenti')]")));*/
           
            if (text.Displayed)
            {
                Console.WriteLine("Basic Auth - Passed.");
            }
            

        }
    }
}
        

Key Takeaways:

  • Bypassing Basic Auth via URL ensures smooth test automation.
  • Using explicit waits (instead of Thread.Sleep()) is a best practice for handling page load synchronization.

This approach is perfect for automating tests on websites that require basic credentials for access. 🚀

Happy Testing!

#Selenium #AutomationTesting #CSharp #BasicAuth #WebTesting #TestAutomation

Vipul Patel

QA Lead Engineer,Salesforce,Selenium,Java,python,API Testing,Cypress,JavaScript,TypeScript,JAWS,Parallel Execution with TestNG,Jenkins,CI/CD, Cucumber, Junit, Maven, Product Training,Team Building, Git, Github,Playwright

6mo

Not secure to use the Automation script this way.Automation script should use the encrypted user name and password.

To view or add a comment, sign in

Others also viewed

Explore topics