Javascript dialog handling

2 posts, 0 answers
  1. Saket
    Saket avatar
    22 posts
    Member since:
    Sep 2011

    Posted 15 Nov 2011 Link to this post

    Hi,
    I am writing a test script using MS test and Webaii in which i have to fill in a application form.On the save of the form the system checks for the mandatory feilds if not prompts a javascript alert message that the mandatory feilds a,b,c are not filled.
    I need to capture and log this step in my code test
    I did the floowing to try capture the script alert

               Settings.Current.UnexpectedDialogAction = UnexpectedDialogAction.HandleAndFailTest;
               manager.DialogMonitor.Start();
    // Attempted to check if the line below captures the dialog
               int counter= manager.DialogMonitor.Dialogs.Count;
    //This actually adds a dialog --- not what is needed

                manager.DialogMonitor.AddDialog(new AlertDialog(manager.ActiveBrowser,DialogButton.OK));


    Any help appreciated

  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 18 Nov 2011 Link to this post

    Hello Saket,
        here's a code sample that demonstrates how to handle a Javascript dialog in code:
    // Launch an instance of the browser
                Manager.LaunchNewBrowser();
     
                // Please make sure you call the current dialog handler before the dialog actually appears on the screen!
                // Handle 'Alert' dialog.
                AlertDialog alertDialog = new AlertDialog(ActiveBrowser, DialogButton.OK);
                Manager.DialogMonitor.AddDialog(alertDialog);
     
     
                // Navigate to : 'http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert'
                ActiveBrowser.NavigateTo("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
     
                // Click 'Button'
                FrameInfo fInf = new FrameInfo("", "view", "http://www.w3schools.com/js/tryit_view.asp", "filename=tryjs_alert", 1, false, false);
                ArtOfTest.WebAii.Core.Browser t1_frame = ActiveBrowser.Frames[fInf];
                t1_frame.Find.ByExpression<HtmlInputButton>("type=button", "tagname=input").Click();
     
                // Wait Until Dialog is Handled.
                alertDialog.WaitUntilHandled(5000);

    This code sample handles the dialog in the following demo page:
    http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert

    The sample should give you a good idea of how to handle the dialog in code.

    Greetings,
    Stoich
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top