MS Dynamics CRM – Automation Testing: Alerts Handling

Handling Alerts through Automation Testing is always tricky. This task becomes more challenging if the alerts are dynamic in nature. Dynamic Alerts is a term which is used here in the following context:

  1. Loading time of Alert is always different.
  2. Content of Alert is based on some business logic that is implemented which might be unknown to the tester.
  3. Occurrence of Alert is based on business logic that is implemented but might not be known to the tester.

Treating such Alerts through Selenium code is challenging, and we must play with Alert related classes along with Wait methodologies to handle such type of alerts.

As Selenium Waits are discussed in the previous article, we will build the existing information on top of that.

Working:

While testing the flow of application through manual steps, we encounter a scenario in which a CRM Alert is generated because of some actions. e.g. we click on a button that results in loading a page, but before loading that page, the system shows an alert that the user either needs to Accept or Dismiss. (Programmatically, we can perform more actions other than only Accept or Dismiss but, in this article, scope is only defined for these two (2)).

Implementation:

In building the automation framework, it is a good practice to create a Helper Class which contains all the common methods used within the framework for different test case implementations.

 Helper Class contains the reference to “Selenium library” by “using OpenQA.Selenium;”. This library contains the interface IAlert which we will be using in our implementation.

Logically, as a Tester we cannot gauge how much time it takes to load an Alert and that is why we design our solution in such a way that Automation Framework waits for the maximum possible amount of time that the system will take to show the Alert.

In this case, we have created a method called as HandleAlert() and its implementation is as follows:

public static void AlertAccept()
       {
            try
            {
                IAlert alert = new WebDriverWait(Browser.Driver, TimeSpan.FromMilliseconds(delayAvg))
                    .Until(drv => ExpectedConditions.AlertIsPresent().Invoke(drv));
                if (alert != null)
                {
                    Browser.Driver.SwitchTo().Alert();
                    alert.Accept();     // To Accept the Alert
                   alert.Dismiss();    // To Dismiss the Alert              
                }
            }
            catch (WebDriverTimeoutException e) { }
       }
//delayAvg is globally defined variable contains the value of time interval in milliseconds.

Explanation:

The above written method can be summarized in following points:

  1. Function waits for the defined amount of time (var delayAvg) for the Alert to appear.
  2. If the Alert appears within the designated time, then it switches to the Alert windows and performs the required action (Accept/ Dismiss).
  3. If the Alert doesn’t appear within defined time, then it catches the Exception and doesn’t perform the desired steps in a test case to proceed any further.