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");
}