Thursday, November 30, 2017

Write Unit/Regression test case using Selenium



In this post, I am going to explain you how to automate the testing of web application using the selenium.


There are 2 ways of doing this.
  1. Use Selenium IDE plugin. Only available in Mozilla.
  2. Write the custom test cases using selenium


Let me explain you both the options in detail.


Selenium IDE


                The plugin is available on Mozilla. You can add it and start recording the test cases. It is very simple and easy to understand.




  1. Type in the website URL for which you are recording the test cases.
  2. Click on the RED button on top right of the screen.
  3. Perform the test action on the site. In the background, selenium IDE will record your test and when you complete the test cases, click on the RED button again to stop recording.
  4. You can test the test case by clicking on the “Play” button and you have the leverage to slow and fast the execution of the test case.


Custom Test Case Writing using Selenium


                To write the custom test cases using selenium, you need to build the environment on your machine by installing few dll’s. Details of each dll is provided as below


  1. Selenium Web Driver
  2. Download the browser driver. Use http://www.seleniumhq.org/download/ URL to get the right driver.


Let’s get started


In this example, I am going to use the internet explorer browser to test the test cases. I have downloaded the “IEDriverServer.exe” driver from selenium site and placed on my desktop.
  1. Add new unit test project in Microsoft Visual Studio 2013.
  2. Add “Selenium WebDriver” using NuGet package.
  3. In your unit test class, add the following namespaces
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;


      4. Declare a webdriver variable and create the object.


static IWebDriver driverIE;
 
                [TestInitialize]
public void Initilization()
{
            driverIE = new InternetExplorerDriver(@"C:\Users\UserName\Desktop");
}


      5. Write your first test case. Here I am writing the test case for the login


     [TestMethod]
            public void TestMethodIELogin()
            {       
                                 driverIE.Url = "http://localhost:8181/web pages/login.aspx";
                                 driverIE.FindElement(By.Id("txtUserName")).SendKeys("UserName");
                                 driverIE.FindElement(By.Id("txtPwd")).SendKeys("test123");
                                 driverIE.FindElement(By.Id("btnLogin")).SendKeys(Keys.Enter);
            }


      6. Run the test case by pressing the Ctrl+R+T or you can just right click on any test case and select “Run Tests”

No comments: