title: Selenium C# finding alert and verifying text
body: <p>I am looking for an example for a verification that a javascript alert has taken place. I have a script that goes to a login page, and attempts to login to the system. I have a test case for a successful login, and I am trying to work on a failed login case. Any suggestions for automation best practices would be appreciated. So to boil my question down it is simply "How to I verify that this alert has happened on my webpage?"</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;div class="alert"&gt;
  The username and/or password entered are invalid.&lt;/div&gt;
  </code></pre>
  
  <p>Test</p>
  
  <pre><code>    [Test]
      public void failedCustomerLogin()
      {
          //Finding the customer login link and clicking it
          var customerLogin = driver.FindElement(By.Id("Homepage_r2_c14"));
          customerLogin.Click();
  
          Assert.AreEqual("http://stage.blank.net/login", driver.Url);
  
          var userName = driver.FindElement(By.Id("username"));
          userName.SendKeys("badCustomerName");
  
          var Pasw = driver.FindElement(By.Id("password"));
          Pasw.SendKeys("BadPassword");
  
          var submit = driver.FindElement(By.Id("btnSubmit"));
          submit.Click();
  
          //TODO: need to find failed method
          string errorText = driver.SwitchTo().Alert().Text;
          Assert.IsTrue(errorText.Contains("The username and/or password entered are invalid."));
      }
  </code></pre>
  
  <p>Wait Selenium</p>
  
  <pre><code>        //Waiting for error to display
          driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
  </code></pre>