Handle message dialog from webpage

5 posts, 0 answers
  1. Chunks
    Chunks avatar
    17 posts
    Member since:
    Nov 2011

    Posted 11 Jun 2013 Link to this post

    Hi All,

    In our application, we have a dialog which pops up when a user tries to close the browser without logging out using the official link. We seems to have no control over it and you can't see this in DOm. It just locks the browser control and give you two options :

    Leave this Page

    Stay on this page


    how should I go about handling this use-case, if suppose I intend to click on "Leave this Page"

    Thanks in advance!

  2. Mario
    Admin
    Mario avatar
    8 posts

    Posted 14 Jun 2013 Link to this post

    Hello Aakash,

    Thank you for your question.

    This type of dialog is considered to be an OnBeforeUnload event (public example site).

    You will first need to add a using statement for 'ArtOfTest.WebAii.Win32.Dialogs'.

    Please see code sample below which works against the public site mentioned above:

    OnBeforeUnloadDialog dialog = OnBeforeUnloadDialog.CreateOnBeforeUnloadDialog(ActiveBrowser, DialogButton.OK);
    Manager.DialogMonitor.AddDialog(dialog);
    HtmlAnchor link = Find.ByExpression<HtmlAnchor>("innertext=Click here to navigate to www.microsoft.com.");
    link.MouseClick();
    dialog.WaitUntilHandled(10000);

    Please let me know whether you have any additional questions.

    Regards,
    Mario
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  3. Chunks
    Chunks avatar
    17 posts
    Member since:
    Nov 2011

    Posted 18 Jun 2013 Link to this post

    This works, Thank you, Mario!!
  4. James
    James avatar
    26 posts
    Member since:
    Oct 2008

    Posted 09 Oct 2013 Link to this post

    Hi Mario,

    We are Telerik's customer for last 4 years and use Rad Controls in our web application. We also use WebAii's testing framework for test automation.

    When automated test case load application in the browser and then attempt to close the browser after completing test, browser shows dialog with 2 options "Leave this page" and "Stay on this page". 

    I want automated test case to choose "Leave this page" option. 

    I use below code, however it does not seem to choose "Leave this page" and close the active browser. 

    OnBeforeUnloadDialog dialog = OnBeforeUnloadDialog.CreateOnBeforeUnloadDialog(ActiveBrowser, DialogButton.OK);
    Manager.DialogMonitor.AddDialog(dialog);
    Manager.DialogMonitor.Start();
    ActiveBrowser.Close();
    dialog.WaitUntilHandled(10000);


    Could you please advice me how to handle this dialog and choose "Leave this page" to close the active browser?
  5. Ivaylo
    Admin
    Ivaylo avatar
    750 posts

    Posted 14 Oct 2013 Link to this post

    Hello James,

    Please let me interject into this ticket since Mario is out of the office today.
    Could you please try placing the following code where this dialog appears:

    this.Manager.Settings.UnexpectedDialogAction = UnexpectedDialogAction.HandleAndContinue;

    Hope this helps.

    Regards,
    Ivaylo
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
Back to Top