Forum Discussion

JohnSnikers's avatar
JohnSnikers
Occasional Contributor
6 years ago

Dynamically changing UnexpectedWindows handling

Hi,

There are a lot of unexpected windows/dialogboxes with different messages poping up in my application during test runs.

Since the text of the popup windows changes dynamically  I'm not able to tell which appears when so I'm not able to tell if it's an actual error or just a popup window.

I figured out the following solution:

function ProjectEvents_OnUnexpectedWindow(Sender, Window, LogParams) 
{
  Window.Activate();
  myApp = Aliases.MyApp;
  var String1 = "The last ";
  var String2 = "Number of";  //..orders/days/items
  var String3 = "There are";  //..1/2/3/4/5/6..orders! or Validation failed! There are ....orders!
switch(Window.MappedName) {
        case "Aliases.MyApp.frmMain":
              Log.Warning("MyApp was handled as 'Unexpected' window");
              break;
        case "Aliases.MyApp.dlgApp":
              var message = myApp.dlgMyApp.Child(myApp.dlgMyApp.ChildCount - 1).Name;
              if(message.includes(String1)) {
                    DevLogMessage("The text of the popped up window: " + TrimMessage(message),true);
                    myApp.dlgMyApp.btnYes.ClickButton();
                    break;
              } else if(message.includes(String2)) {
                    DevLogMessage("The text of the popped up window: " + TrimMessage(message),false);
                    myApp.dlgMyApp.btnYes.ClickButton();
                    break;
              } else if(message.includes(String3)) {
                    DevLogMessage("The text of the popped up window: " + TrimMessage(message),true);
                    myApp.dlgMyApp.btnYes.ClickButton();
                    break;
} }

This is not the whole script just a snippet but you can imagine more else if cases..

This is a working solution and unexpected/popup windows will be handled as I want them.

BUT it is a bit slow and since there are a tons of times this event is going to be activated this slowness adds up and the test execution time grows.

I know it is mostly because of the (message.includes(String)) statement, everytime it has to check the whole Message if it includes the given string even if the first part of it is different. But it is kind of a must have  way because I don't know which part of the dialogbox's message will contain the string which helps me to decide which button to Click on.

I hope you can understand the problem and why it is solved like this.

 

If you have a better idea/way to solve this problem please feel free to tell me

Anything useful is truly appriciated

Thank you

 

 

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Your second case statement seems to contain a lot of unnecessary logic.  In the code to be executed with each "if" statement, there really is no major difference in the logic with the exception of a difference in one parameter of your DevLogMessage call.  I would reorganize your code a little bit to reduce the number of calls and if logic statements.  This reduces the number of "includes(Stringx)" calls and simplifies the code a bit.  Not sure how much performance improvement this will bring but the less logic included, usually the better the performance.

     

    function ProjectEvents_OnUnexpectedWindow(Sender, Window, LogParams) 
    {
      Window.Activate();
      myApp = Aliases.MyApp;
      var String1 = "The last ";
      var String2 = "Number of";  //..orders/days/items
      var String3 = "There are";  //..1/2/3/4/5/6..orders! or Validation failed! There are ....orders!
    switch(Window.MappedName) {
            case "Aliases.MyApp.frmMain":
                  Log.Warning("MyApp was handled as 'Unexpected' window");
                  break;
            case "Aliases.MyApp.dlgApp":
                  var message = myApp.dlgMyApp.Child(myApp.dlgMyApp.ChildCount - 1).Name;
                  if(message.includes(String2)) 
    				  DevLogMessage("The text of the popped up window: " + TrimMessage(message),false);
    			  else 
    				  DevLogMessage("The text of the popped up window: " + TrimMessage(message),true);
                  myApp.dlgMyApp.btnYes.ClickButton();
                  break;
            }
    }
    • JohnSnikers's avatar
      JohnSnikers
      Occasional Contributor

      Sorry, my example was not correct, please check the .ClickButton() methods :

       

       

      function ProjectEvents_OnUnexpectedWindow(Sender, Window, LogParams) 
      {
        Window.Activate();
        myApp = Aliases.MyApp;
        var String1 = "The last ";
        var String2 = "Number of";  //..orders/days/items
        var String3 = "There are";  //..1/2/3/4/5/6..orders! or Validation failed! There are ....orders!
      switch(Window.MappedName) {
              case "Aliases.MyApp.frmMain":
                    Log.Warning("MyApp was handled as 'Unexpected' window");
                    break;
              case "Aliases.MyApp.dlgApp":
                    var message = myApp.dlgMyApp.Child(myApp.dlgMyApp.ChildCount - 1).Name;
                    if(message.includes(String1)) {
                          DevLogMessage("The text of the popped up window: " + TrimMessage(message),true);
                          myApp.dlgMyApp.btnYes.ClickButton();
                          break;
                    } else if(message.includes(String2)) {
                          DevLogMessage("The text of the popped up window: " + TrimMessage(message),false);
                          myApp.dlgMyApp.btnYes.ClickButton();
                          break;
                    } else if(message.includes(String3)) {
                          DevLogMessage("The text of the popped up window: " + TrimMessage(message),true);
                          myApp.dlgMyApp.btnNO.ClickButton();
                          break;
                    }
      } else if(message.includes(String3)) {
      DevLogMessage("The text of the popped up window: " + TrimMessage(message),true);
      myApp.dlgMyApp.btnOK.ClickButton();
      break;
      }
      }

      As you can see, I have to handle them differently because each of them has different button as well... :(

      Therefore your reorganized code would cause failure. Sorry for giving bad example.

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Again, just checking... there are two "if" statements including String3... is that correct? And your brackets are off somewhere in there, too... so... 

        Without having accurate code, I can't give an accurate suggestion.  But... thinking "out loud" here...

        There might be a way of doing a logical check of the string once without having to check it 4 different times or there might be a methodology that is better, performance wide, than "includes".  

         

        Perhaps you could also create an array or matrix or dictionary of the three string segments and associated "parameters".  Something like

         

        "String1", "true", "btnYes"

        "String2", "false", "btnYes"

        "String3", "true, "btnNo"

         

        And so on... then you can just call one routine that checks which row to reference and then utilize the expected parameters.