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");
        }
2
  • 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. Commented Jan 21, 2023 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. Commented Jan 24, 2023 at 18:08

1 Answer 1

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");
    }
Sign up to request clarification or add additional context in comments.

Comments

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.