Automated Testing with Selenium

By Nick Kwiecien | Software Engineer

Introduction

About a month ago someone at work turned me on to testing with Selenium. I was blown away with the functionality and I couldn’t believe what I was seeing. Selenium automates the actions that you write in your tests. It gives you a great visual picture of what is happening. I quickly tried to implement some of the Selenium concepts in my own work. Using Selenium is about as easy as getting started with Nunit which was my previous blog here.

Automated Testing

Automated testing is the process of taking an outside tool like Selenium and integrating it to process tests automatically instead of manually. Automated testing with tools such as Selenium has its pros and cons. One thing that it does allow you to do is visually see the results of the test. This can help you find bugs and issues before another testing. The tests are also reusable as you will see later some of the tests can be easily taken and implemented other places in your project.

Some of the cons include the fact that they can’t replace manual testing as they can only do what exactly they are programmed to do. It is also very hard to implement automated testing when the requirements for the project are consistently changing. This is because once you write the test and the requirements change you will have to then change the test.

Installation

The installation in Visual Studio is a simple one. Make sure you already have the unit testing package you wish to use installed. For this, I will use Nunit but you can use the unit testing framework of your choosing. You will need to install Selenium.WebDriver, Selenium.Support, and web drivers. Currently, drivers are available for Chrome, Firefox, and IE. For my examples, I’m using the Chrome web driver but you can use just the one or multiple for your testing.

Examples of Testing with Selenium

To start testing you will want to set up some initial tests and set up the web driver. Below is the set-up code I have implemented for my FireDrill test project.

public class FIreDrillSelenium
    {
        protected IWebDriver driver;


        [SetUp]
        public void Initialize()
        {
            driver = new ChromeDriver();
        }

        [Test]
        public void OpenAppTest()
        {
            driver.Url = "http://firedrill-local.com/";
        }

	[TearDown]
	public void EndTest()
	{
		driver.Close();
		this.driver.Quit();
	}

	}

As you can see the point of this class is to set up the web driver and let the driver know what URL to use. The TearDown portion of this code can be easily removed if you would like to see what the result is but if you are going to use this to verify tests against a review or staging environment you will want the TearDown to be there. All tests classes that are to use Selenium will now inherit this class.

Testing specific pages

Selenium allows page specific testing with web elements. These web elements could be anything thing from class names, id, tag name, etc. When using the web elements, the test will target those certain elements on the page based on the logic you wrote for it. Take for example my code for the bobblehead index page below. In this class, I’m also specifying what URL the page is so that we can navigate to it.

    public class BobbleIndex : PageBase
    {
        [FindsBy(How = How.ClassName, Using = ".bobble")]
        private IWebElement TotalBobbleheads { get; set; }

        public BobbleIndex(IWebDriver driver) : base(driver)
        {
        }

        public override string PageUrl
        {
            get { return "bobble/index"; }
        }

        public override void Navigate()
        {
            base.Navigate();
        }

        public int NumberOfBobbleheadResults
        {
            get
            {
                int numBobble = int.Parse(this.TotalBobbleheads.Text);
                return numBobble;
            }
        }
    }

This class helps not only navigate to the page but it will also help read the total number of bobbleheads. The code is looking for the bobble class so if the bobbleheads are wrapped in the class the class should be able to read the number of total bobbleheads.

Testing the navigation and number of bobbleheads

The following two examples test the navigation functionality and the number of bobbleheads. The navigation will make sure the bobblehead index page can be hit. The next test navigates to the page while making sure that the number of bobbleheads on the page is greater than or equal to one.

        [Test]
        public void TestNavigation()
        {
            BobbleIndex bobble = new BobbleIndex(this.driver);
            bobble.Navigate();

            Assert.IsTrue(bobble.IsCurrentPage());
        }

        [Test]
        public void NumberOfBobbleHeads()
        {
            BobbleIndex bobble = new BobbleIndex(this.driver);
            bobble.Navigate();

            int bobbleCount = bobble.NumberOfBobbleheadResults;

            Assert.IsTrue(bobbleCount >= 1);

Conclusion

In conclusion, automated testing with Selenium requires more setup then manual testing but gives you a better idea of how the code is working and allows you to visualize the results. If you’re considering adding unit testing to your projects I would consider going automated over manual. To me being able to visualize the results is a huge plus and it will allow you to find errors that occur before you smoke test. Automated testing could be a huge advantage to you if you choose to adopt it.

Written on September 29, 2017