How to write a simple automation test for Asp.Net application using Selenium

In the past I have been using Visual Studio coded tests as well as recorded tests for test automation. When I installed VS2019 and looked at documentation of Coded UI tests, I found following announcement on the documentation page.

Coded UI Test for automated UI-driven functional testing is deprecated. Visual Studio 2019 is the last version where Coded UI Test will be available. We recommend using Selenium for testing web apps and Appium with WinAppDriver for testing desktop and UWP apps. Consider Xamarin.UITest for testing iOS and Android apps using the NUnit test framework.

In the past, I have used Selenium for some other web page automation related tasks. It was time to start using for Selenium for our Asp.Net application's automated testing. Following is an example of how our single Login page test automation looks like.

[TestMethod]
 public void Login_Success()
 {
            var loginWait = new WebDriverWait(_webDriver, System.TimeSpan.FromSeconds(5));
            _webDriver.Navigate().GoToUrl(_testOptions.IdamUrl);
            var userIdInputElement = loginWait.Until(w => w.FindElement(By.Id("userId")));
            var passwordInputElement = _webDriver.FindElement(By.Id("password"));
            var loginButton = _webDriver.FindElement(By.TagName("button"));
            Assert.AreEqual(loginButton.Text, "Login");

            userIdInputElement.SendKeys("mylogin@gmail.com");
            passwordInputElement.SendKeys("myPassword");

            loginButton.Click();

            var logoutLink = loginWait.Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Log out")));
            logoutLink.Click();

            _webDriver.Quit();
}

Above test performs following actions and corresponding checks.

  • Navigate to Login Url
  • Wait for text box that has id value of userId. I did not decide to apply wait for other elements because presence of this very element would mean other elements have loaded. You can argue that it may not be true. Applying a wait for every single element will just an overhead. One has to make some valid assumptions about how and when page elements will load.
  • Find text box for password and button that allows click action
  • Use SendKeys function to enter valid login and password
  • After login completed, user will be redirected to new page. Test waits for Logout link to appear on next page and wait for it be clickable
  • Perform logout action

That completes a very simple Asp.Net page automation using Selenium.

comments powered by Disqus

Blog Tags