Forum Discussion

royd's avatar
royd
Regular Contributor
8 years ago

Switch statement troubles!

I have a situation where user login process involves EULA agreement. The user need to agree to the EULA to continue with the login process. This is configurable. So there are times when the EULA does not show up, on the other hand it does show up when it is configured.

 

Currently, I am stuck at the switch statement. What I’m trying to do is to bypass clicking the agreed button and log message stating that the EULA was not present, when it is configured to not show up. When the EULA is configured to show up, log a message stating that the EULA was loaded. Then click the “Agreed” button.

 

In either case, continue with the rest of the test steps.

 

Here is my code –

 

…
var panelButtons = page.FindChildEx("Name", "Panel(3)", 10, true, 10000);  //thanks to Alex for suggesting ‘FindChildEx’                                                                      
var btnAccept = panelButtons.FindChild("idStr", "ctl00_MainContent_btnAcceptEula", 10);

page.Wait(); switch (btnAccept) { case btnAccept.Exists: Log.Message("EULA page loaded."); btnAccept.scrollIntoView(false); btnAccept.ClickButton(); break; case !btnAccept.Exists: Log.Message("EULA page did not load."); break; }

The entire switch statement is ignored by TestComplete and I get an error at following step after EULA. I have never used switch statement before and I’m pretty sure there are some issues with logic/syntax.

 

I really appreciate any help.

 

Thanks in advance.

 

Dave

  • I don't think you need a switch statement for this at all.  It doesn't make sense to use a switch statement to make a decision between two boolean values. I'd write the code like this:

     

    var panelButtons = page.FindChildEx("Name", "Panel(3)", 10, true, 10000);  //thanks to Alex for suggesting ‘FindChildEx’                                                                      
    var btnAccept = panelButtons.FindChild("idStr", "ctl00_MainContent_btnAcceptEula", 10);
    
    page.Wait();
    if (btnAccept.Exists){     
            Log.Message("EULA page loaded."); 
            btnAccept.scrollIntoView(false);                                            
            btnAccept.ClickButton();   
        }
        else {
           Log.Message("EULA page did not load."); 
        }
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I don't think you need a switch statement for this at all.  It doesn't make sense to use a switch statement to make a decision between two boolean values. I'd write the code like this:

     

    var panelButtons = page.FindChildEx("Name", "Panel(3)", 10, true, 10000);  //thanks to Alex for suggesting ‘FindChildEx’                                                                      
    var btnAccept = panelButtons.FindChild("idStr", "ctl00_MainContent_btnAcceptEula", 10);
    
    page.Wait();
    if (btnAccept.Exists){     
            Log.Message("EULA page loaded."); 
            btnAccept.scrollIntoView(false);                                            
            btnAccept.ClickButton();   
        }
        else {
           Log.Message("EULA page did not load."); 
        }
    • royd's avatar
      royd
      Regular Contributor

      Hi Robert

      To the rescue again!! :D  I did think about if - else, but I thought the switch was better suited. Couldn't make it work!

       

      I don't know how to thank you!

       

      Regards.

       

      Dave 

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi Dave,

         

        Usually, Switch statement evaluates the expression in its main 'switch' clause and compares the result to the options provided in the 'case' clauses. So your Switch block should look like this:

          switch (btnAccept.Exists)
            {
              case true:
                Log.Message("EULA page loaded.");
                btnAccept.scrollIntoView(false);
                btnAccept.ClickButton();
                break;
        
              case false:
               Log.Message("EULA page did not load.");
               break;
            }