0

I learn myself of writing Selenium C# automated tests. Now I try to go to the authentication page and send WRONG credentials, submit and check "Not Authorised" text on page. Seems to be simply, but the problem is that when I send credentials to the driver, authentication popup appears, but no user and password input. After all I get the message that OpenQA.Selenium.NoAlertPresentException : no such alert. Is there any simple way in C# to resolve this?

Here is my code:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.Support.UI;

namespace Selenium2.Authorisation
{
    public class Authorisation
    {
        IWebDriver driver;

        [SetUp]
        public void Setup()
        {
            driver = new ChromeDriver();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.Manage().Window.Maximize();
        }        

[Test]
        public void SendWrongUsernameToAuthenticationPopupTest()
        {
            String username = "abc";
            String password = "admin";

            String URL = "https://" + username + ":" + password + "@" + "the-internet.herokuapp.com/basic_auth";
            driver.Navigate().GoToUrl(URL);

            //tried this but received error: OpenQA.Selenium.NoAlertPresentException : no such alert
            IAlert alert = driver.SwitchTo().Alert();
            alert.SendKeys(username);
            alert.Accept();

            driver.Manage().Timeouts().Equals(TimeSpan.FromSeconds(5));

            String text = driver.FindElement(By.TagName("p")).Text;

            String expectedText = "Not authorized";
            IWebElement p2 = driver.FindElement(By.TagName("body"));
            Assert.AreEqual(expectedText, p2.Text, "The unauthorised texts are not the same");
        }
Mikolaj
  • 1
  • 1
  • Why are you switching to alert when you are passing the user name and password in URL. I think it is using an HTTP Basic Authentication mechanism. So you do not need the rest of code and you can check the response status code to verify if the login is successful or not. – Akzy Jan 21 '23 at 18:50
  • You are right, Akzy! I think checking status code is simpliest and best way to check if user is logged or not. – Mikolaj Jan 24 '23 at 18:08

1 Answers1

0

Through Basic Authentication the user does gets logged in. You don't require to authenticate the user anymore unless explicitly asked to login.

[Test]
    public void SendWrongUsernameToAuthenticationPopupTest()
    {
        String username = "abc";
        String password = "admin";

        String URL = "https://" + username + ":" + password + "@" + "the-internet.herokuapp.com/basic_auth";
        driver.Navigate().GoToUrl(URL);
        driver.Manage().Timeouts().Equals(TimeSpan.FromSeconds(5));
        String text = driver.FindElement(By.TagName("p")).Text;
        String expectedText = "Not authorized";
        IWebElement p2 = driver.FindElement(By.TagName("body"));
        Assert.AreEqual(expectedText, p2.Text, "The unauthorised texts are not the same");
    }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352