I haven’t published any blog posts about the 30 days of tools challenges for a couple of weeks. I did continue with the challenge, but only published my responses on the Ministry of Testing website. Even though the challenge is now over, I do have a few blog posts planned. In this blog post, I describe how I pushed a button.

What’s the most imaginative way you could click this button?

  1. Head to https://ministryoftesting.github.io/the-button/ 40
  2. Click the button (Sound on is recommended)

I don’t think I was particularly imaginative for this task (although this solution did beat just clicking it with a mouse). I decided to write a C# program that would click the button for me. Here is the code I wrote to click the button:

    public  static void Main(string[] args)
    {
        IWebDriver Driver = new ChromeDriver(Environment.CurrentDirectory);
        Driver.Navigate().GoToUrl("https://ministryoftesting.github.io/the-button/#");
        Driver.Manage().Window.Maximize();

        IWebElement button = Driver.FindElement(By.Id("buttonImage"));
        button.Click();
    }

Exploring the Application

After setting up my program to click the button, I had a little think about how I might test this button. Since I don’t yet know what the button does, scripted automated tests weren’t going to provide me with much information. Instead, I did a little exploratory testing to discover what the button actually did.

My observations showed that the button did the following:
– Appears in green when the website is first launched
– Turns red when it is clicked
– Remains red for approximately 4 seconds
– After 4 seconds, a sound is emitted and the button turns back to green.

Setting Up Some Automated Tests

Now I knew what the button acutally did, I was able to use this information to develop some automated tests.

I start out using the OneTimeSetUp and OneTimeTearDown attributes to start and stop the browser.

    [OneTimeSetUp]
    public void Setup()
    {
        Driver = new ChromeDriver(Environment.CurrentDirectory);
        Driver.Navigate().GoToUrl("https://ministryoftesting.github.io/the-button/#");
        Driver.Manage().Window.Maximize();
    }

    [OneTimeTearDown]
    public void Teardown()
    {
        Driver.Quit();
    }

Next I started creating the test. This was done using the TestCase attribute. This will contain the steps for the actual test. This will include the code for clicking the button, and checking the behaviour of the button after it has been clicked.

    [TestCase]
    public void CheckButtonWorksAsExpected()
    {

    }

To check the colour of the button before and after it was clicked, I needed to see what happened to the element when the button was clicked. When I inspected the button in dev tools, I saw that there was the attribute src which included the words ‘green-button’. When clicked, it would change to ‘red-button’. This meant I could use this in the test to determine if the button was green or red.

To confirm the colour was correct, I created an assertion statement which included the code that checked if the colour was red or green.

Assert.True(button.GetAttribute("src").Contains("green"));
Assert.True(button.GetAttribute("src").Contains("red"));

Due to the time taken for the button to change back to green after clicked, I needed to include a short wait in the test. Without this wait, it would check to see if the colour has reverted back to green too soon.

        Thread.Sleep(4000);
        Assert.True(button.GetAttribute("src").Contains("green"));

One limitation of the automated tests is that I can’t test the sound. There will always be some steps that will either be impossible to automated or too complicated to automate.

Final Thoughts

Even though this was a small application created for the Ministry of Testing 30 Days of Tools event, it really got me thinking about the process I follow for setting up an automated test. I don’t start writing code and hoping it will work. The process requires investigating and discovering what the application actually does. Whether we are involved in testing the application manually or developing automated tests we still need practical experience using the application. A high level of domain knowledge is required regardless of our role within the testing team.

Full Code Sample

For this code to work, you’ll need to install the following NuGet packages:

  • Microsoft.NET.Test.Sdk
  • NUnit
  • NUnit3TestAdapter
  • Selenium.WebDriver
  • Selenium.WebDriver.ChromeDriver
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;

namespace PushButton
{
    public class Program
    {
        IWebDriver Driver;

        public  static void Main(string[] args)
        {
            IWebDriver Driver = new ChromeDriver(Environment.CurrentDirectory);
            Driver.Navigate().GoToUrl("https://ministryoftesting.github.io/the-button/#");
            Driver.Manage().Window.Maximize();

            IWebElement button = Driver.FindElement(By.Id("buttonImage"));
            button.Click();

            Assert.True(button.GetAttribute("src").Contains("green"));
            button.Click();
            Assert.True(button.GetAttribute("src").Contains("red"));

            //The button should change back to green after a few seconds
            //so we wait before checking the button has changed back
            Thread.Sleep(4000);
            Assert.True(button.GetAttribute("src").Contains("green"));


        }

        [OneTimeSetUp]
        public void Setup()
        {
            Driver = new ChromeDriver(Environment.CurrentDirectory);
            Driver.Navigate().GoToUrl("https://ministryoftesting.github.io/the-button/#");
            Driver.Manage().Window.Maximize();
        }

        [OneTimeTearDown]
        public void Teardown()
        {
            Driver.Quit();
        }


        [TestCase]
        public void CheckButtonWorksAsExpected()
        {
            IWebElement button = Driver.FindElement(By.Id("buttonImage"));


            Assert.True(button.GetAttribute("src").Contains("green"));
            button.Click();
            Assert.True(button.GetAttribute("src").Contains("red"));

            //The button should change back to green after a few seconds
            //so we wait before checking the button has changed back
            Thread.Sleep(4000);
            Assert.True(button.GetAttribute("src").Contains("green"));
        }
    }
}

Here is a video of a version of program being run and the button pushed.

Further Reading

Ministry of Testing Club discussion page for Day 21
See how others handled this challenge