How should data and application state be handled in UI automation



  • I have two big concerns about UI automation.

    1. If I am creating a data record as part of my test and verifying the creation, should I also delete the data in the same test? If yes what if the creation passes but deletion of data fails due to some reason? If deletion fails then I will end up having lots of test data.

    2. If I have to verify the state of some UI element as a result of some operation, should I reset the precondition in the test itself? The steps are:

      1. perform some a action
      2. verify the state of an element .
      3. undo the action so that next time when the same test runs it is in the correct condition.

    My concern is what if resetting the action fails. Should resetting the action also be part of the test? If yes, when the resetting fails it fails the entire test, but my actual test of setting and verifying the state has passed.

    How should my tests handle situations where I create or change data and undoing my actions is the failure point?



  • It depends

    Now for the longer answer.

    If you have full control over your test database, you can have your tests configured in a way that allows them to always start from the same known point. One way to do this is to restore your test database at the start of each test run. Another is to use containers.

    If restoring the test database isn't possible, you are better off having designated test setup and test cleanup routines. Exactly how you configure them will depend on your testing framework, but for MSTest it would look a bit like this:

    [ClassSetup]
    public void RunClassInitialization()
    {
        // do whatever you need to have everything in the correct state to start your test run, for this class 
    }
    
    [TestSetup]
    public void RunSetup()
    {
        // do whatever configuration is common for each test in this class.
    }
    
    // a series of test methods follow - these should all be for a single part of the UI,
    // like one page that you're working with.
    
    [TestCleanup]
    public void RunCleanup()
    {
        // do whatever cleanup is common to all your tests
    }
    
    [ClassCleanup]
    public void RunClassCleanup() 
    {
        // do whatever cleanup you need to after running all the tests in this class
    }
    

    The exact annotation style and scope options will depend on what framework and language you use. I may not have correctly remembered the C# and MSTest annotations, but you get the idea.

    If before every test you go to the login page and log in, that goes in the before test/test setup method. Your cleanup goes in the cleanup method.

    Another option is to use a try-finally block:

    try 
    {
        //do your actions and assertions here
    }
    finally
    {
        //do your cleanup here
    }
    
    

    It all comes down to what works best for your situation.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2