Writing Transactions

This example shows how to define a transaction class and write a simple transaction. When writing transactions, make sure that:

The type is defined (boolean, string, etc.).

A result is specified. Without a SetResult element, the transaction is not able to end.

A cleanup function is defined.

A SetException function is defined, to throw an exception from the Result property. If the transaction fails, an exception is thrown before the timeout period elapses.

public void SetException(Exception ex);

The following transaction opens a Java application, checks whether a checkbox in the SwingSet main window is selected, and returns a result.

Transaction Example

class CheckBoxTransaction : Transaction<bool>

{

    SwingSetMainWindow swingSetMainWindow;

    private ServiceBase srvc;

 

    public CheckBoxTransaction(ServiceBase srvc, TimeSpan timeout)

      : base(srvc, timeout)

    {

      this.srvc = srvc;

      this.srvc.WindowOpened += new EventHandler(srvc_WindowOpened);

    }

 

    void srvc_WindowOpened(object sender, EventArgs e)

    {

      try

      {

        if (sender is SwingSetMainWindow)

        {

          swingSetMainWindow = sender as SwingSetMainWindow;

          SleepBeforeExecuting();

          swingSetMainWindow.cbxResizable.Click();

          swingSetMainWindow.cbxClosable.Checked = false;

          SetResult(swingSetMainWindow.cbxClosable.Checked);

      }

    }

    catch(Exception ex)

    {

      SetException(ex);

    }

 

    public void Start()

    {

      StartJavaApp();

    }

 

    protected override void Cleanup()

    {

      srvc.WindowOpened -= new EventHandler(srvc_WindowOpened);

      swingSetMainWindow.CloseWindow();

    }

}

When calling the transaction, you need to call the transaction.Result, so the transaction can end. The following example shows the service implementation of the transaction class defined above:

public void CheckBox()

{

    using (CheckBoxTransaction transaction = new     CheckBoxTransaction(service, TimeSpan.FromSeconds(10)))

    {

      transaction.Start();

      Assert.IsFalse(transaction.Result);

    }

}