This is a migrated thread and some comments may be shown as answers.

Javascript dialog handling

1 Answer 79 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Saket
Top achievements
Rank 1
Saket asked on 15 Nov 2011, 01:51 PM
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

1 Answer, 1 is accepted

Sort by
0
Stoich
Telerik team
answered on 18 Nov 2011, 03:19 PM
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.
Tags
General Discussions
Asked by
Saket
Top achievements
Rank 1
Answers by
Stoich
Telerik team
Share this question
or